指数型母函数&&hdu 2065 "红色病毒"问题

常用的指数型母函数:e^x=1+x/1!+x^2/2!+x^3/3!+……    e^(-x)=1-x/1!+x^2/2!-x^3/3!+……
指数型母函数用于解决多重集合的排列问题,g(x)=p(n,0)+p(n,1)x+p(n,2)x^2/2!+p(n,3)x^3/3!+……+P(n,n)x^n/n!=C(n,0)+C(n,1)x+C(n,2)x^2+C(n,3)x^3+……+C(n,n)x^n=(1+x)^n.所以(1+x)^n是n的各个排列的母函数。比如用red,white,black给1*n的棋盘涂色,方案数:g(x)=(1+x+x^2/2!+x^3/3!+……)(1+x+x^2/2!+x^3/3!+……)(1+x+x^2/2/3!+……)=e^(3x)=∑(0,∞)3^n*x^n/n!.-->h(n)=3^n。
如果要求red出现偶数次,总的方案数应该是:g(x)=(1+x+x^2/2!+x^4/4!+……)(1+x+x^2/2!+x^3/3!+……)(1+x+x^2/2!+x^3/3!+……)=1/2*(e^x+e^(-x))*e^x*e^(-x)=1/2*(e^3x+e^x)=1/2*(∑(0,∞)(3^n+1)*x^n/n!. --> h(n)=(3^n+1)/2.
hdu 2065 "新红色病毒"问题: http://acm.hdu.edu.cn/showproblem.php?pid=2065
A,C可以出现偶数次,B,D可以是任意次数。g(x)=(1+x+x^2/2!+x^4/4!+……)^2*(1+x+x^2/2!+x^3/3!+……)^2=[1/2*(e^x+e^-x)]^2*[e^x]^2=[1/2*(e^2x+1)]^2=1/4*(e^4x+2e^2x+1)=1/4*[ ∑(0,∞)4^n*x^n/n!+2*∑(0,∞)2^n*x^n/n!+1]=∑(0,∞)4^(n-1)*x^n/n!+∑(0,∞)2^(n-1)*x^n/n!+1/4  h(n)=4^(n-1)+2^(n-1). 最后对这个结果取模即可(用到快速幂)。
#include <iostream>
#include<cstdio>
using namespace std;
typedef long long LL;
LL powmod(LL n){
    LL q1=n-1,q2=n-1,res1=1,t1=4,res2=1,t2=2;
    while(q1){
        if(q1&1){
            res1=(res1*t1)%100;
            q1--;
        }
        q1>>=1;
        t1=t1*t1%100;
    }
    while(q2){
        if(q2&1){
            res2=(res2*t2)%100;
            q2--;
        }
        q2>>=1;
        t2=t2*t2%100;
    }
    return (res1+res2)%100;
}
int main()
{
    //freopen("cin.txt","r",stdin);
    int t;
    while(cin>>t&&t){
        LL n;
        for(int i=1;i<=t;i++){
            scanf("%I64d",&n);
            printf("Case %d: %I64d\n",i,powmod(n));
        }
        putchar('\n');
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值