• 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;
    }
  • 相关阅读:
    attr与prop
    Django框架学习
    库的操作
    javascript 基础知识
    进程
    正则表达式
    模块( collections , time , random , os , sys)
    内置函数
    生成器
    迭代器
  • 原文地址:https://www.cnblogs.com/vongang/p/2264972.html
Copyright © 2020-2023  润新知