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

由题知:

(1+x/1!+x^2/2!+``+x^n/n!)^2*(1+x^2/2!+```)^2

由e^x=1+x/1!+x^2/2!+```知

原式=e^(2*x)*((e^x+e^(-x))/2)^2

  =(1/4)*(e^(2*x)+1)^2

  =(1/4)*(e^(4*x)+2*e^(2*x)+1)

  =(1/4)*(sia(4^n)*(x^n/n!)+2*sia(2^n)*(x^n/n!)+1)

  由以上式子可知:

  x^n/n!的系数为(4^n+2*2^n+1)/4=4^(n-1)+2^(n-1)+1/4

对于本题只需要计算(4^(n-1)+2^(n-1))%100即可。

其中设计大数取余,就不多说了哦,利用了指数的二进制哦~~

/*
* hdu2065.c
*
* Created on: 2011-9-10
* Author: bjfuwangzhu
*/

#include<stdio.h>
#define LL long long
#define nmax 100
int modular_exp(int a, LL b) {
int res;
res = 1;
while (b) {
if (b & 1) {
res = res * a % nmax;
}
a = a * a % nmax;
b >>= 1;
}
return res;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int t, i;
LL n;
while (scanf("%d", &t) != EOF,t) {
for (i = 1; i <= t; i++) {
scanf("%I64d", &n);
printf("Case %d: %d\n", i,
(modular_exp(4, n - 1) + modular_exp(2, n - 1)) % nmax);
}
printf("\n");
}
return 0;

你可能感兴趣的:(HDU)