bzoj 3208 食物(生成函数)

先推一下生成函数
承德汉堡:偶数个
1 + x 2 + x 4 + x 6 + . . . . . . = 1 1 x 2
可乐:0个或1个
1 + x = 1 x 2 1 x
鸡腿:0个或1个或2个
1 + x + x 2 = 1 x 3 1 x
蜜桃多:奇数个
x + x 3 + x 5 + . . . . . . = x 1 x 2
鸡块:四的倍数个
1 + x 4 + x 8 + x 12 + . . . . . . = 1 1 x 4
包子:0个或1个或2个或3个
1 + x + x 2 + x 3 = 1 x 4 1 x
土豆片炒肉:不超过一个
1 + x = 1 x 2 1 x
面包:三的倍数个
1 + x 3 + x 6 + x 9 + . . . . . . = 1 1 x 3
总生成函数:
1 1 x 2 × 1 x 2 1 x × 1 x 3 1 x × x 1 x 2 × 1 1 x 4 × 1 x 4 1 x × 1 x 2 1 x × 1 1 x 3 = x ( 1 x ) 4
跟据广义二项式定理答案即为 x n 1 的系数
a n s = C n 1 + 3 n 1 = n ( n + 1 ) ( n + 2 ) 6

所以可以边读边膜,代码如下:

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define mod 10007
using namespace std;

string tmp;
long long n;

long long inv(long long a)
{
    int b=mod-2,ans=1;
    while(b)
    {
        if(b&1) ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}

int main()
{
    cin>>tmp;
    for(int i=0;i<tmp.length();i++)
    {
        n=(n*10+tmp[i]-'0')%mod;
    }
    printf("%lld\n",((n*(n+1)%mod)*(n+2)%mod)*inv(6)%mod);
}

猜你喜欢

转载自blog.csdn.net/qq_21829533/article/details/82560786