HDU_2065 红色病毒问题(指数型生成函数)
證明:從題目可以知道 A: (1 + x2/1! + x4/2! + ....); B:? (1 + x/1! + x2/2! + x3/3! + ...); C:(1 + x2/1! + x4/2! + ....); D: (1 + x/1! + x2/2! + x3/3! + ...);
所以有: G(x) = (1 + x2/1! + x4/2! + ....)2 * (1 + x/1! + x2/2! + x3/3! + ...)2;
由于泰勒展開式:
ex = 1 + x/1! + x2/2! + x3/3! + ...
e-x = 1 - x/1! + x2/2! - x3/3! + ...
所以
G(x) = e2x + ((ex + e-x)/2)2;
?= (1/4) * (e2x + 1)2
?= (1/4) * (e4x? + 2*e2x + 1);
又因為:
e4x = 1 + (4x)/1! + (4x)2/2! + (4x)3/3! + ... + (4x)n/n!;
e2x = 1 + (2x)/1! + (2x)2/2! + (2x)3/3! + ... + (2x)n/n!;
所以:
n次冪的排列數為 (1/4)(4n + 2*2n)
即得所求的解為(1/4)(4n + 2*2n)%100 = (4n-1 + 2n-1)%100;
由組合數學公式 (a + b) % c = (a%c + b%c)%c;
所以可以用快速冪取模求解。
ps:注意精度,用64位。這里WA了一次。
My Code:
#include <iostream>#include <cstring>
#include <cstdio>
using namespace std;
const int mod = 100;
__int64 exp_mod(int a, __int64 n) {
__int64 t;
if(n == 0) return 1%mod;
if(n == 1) return a%mod;
t = exp_mod(a, n/2);
t = t*t%mod;
if((n&1) == 1) t = t*a%mod;
return t;
}
int main() {
//freopen("data.in", "r", stdin);
int t, cas;
__int64 n;
while(scanf("%d", &t), t) {
cas = 0;
while(t--) {
scanf("%I64d", &n);
printf("Case %d: %I64d\n", ++cas, (exp_mod(4, n-1) + exp_mod(2, n-1))%mod);
}
cout << endl;
}
return 0;
}
?
轉載于:https://www.cnblogs.com/vongang/archive/2011/11/27/2264972.html
總結
以上是生活随笔為你收集整理的HDU_2065 红色病毒问题(指数型生成函数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机网页 右边的空白区
- 下一篇: BTree,B-Tree,B+Tree,