[HDU2065]"红色病毒"问题

· · 个人记录

题目

        点这里看题目。

分析

        母函数入门题。
        不难想到如下的函数:

G(x)=\left(\sum_{k=0}^{\infty}\frac{x^{2k}}{(2k)!}\right)^2\left(\sum_{k=0}^{\infty}\frac{x^k}{k!}\right)^2

        左边是A,C的取值,右边是B,D的取值。然后化式子——

G(x)=\left(\frac{e^x+e^{-x}}{2}\right)^2e^{2x} G(x)=\frac{e^{2x}(e^{2x}+e^{-2x}+2)}{4} G(x)=\frac{e^{4x}+2e^{2x}+1}{4} G(x)=\frac14\left(1+\sum_{k=0}^{\infty}(4^k+2\times2^k)\frac{x^k}{k!}\right)

        所以[x^n]G(x)=\frac{4^n+2\times2^n}{4}=4^{n-1}+2^{n-1}。打点表之后会发现这个数在\mod 100的意义下是有周期的,直接打表就好了。

代码

#include <cstdio>

typedef unsigned long long ull;

const int mod = 100;
const int MAXSIZ = 105;

template<typename _T>
void read( _T &x )
{
    x = 0;char s = getchar(); int f = 1;
    while( s < '0' || '9' < s ){ f = 1; if( s == '-' ) f = -1; s = getchar(); }
    while( '0' <= s && s <= '9' ){ x = ( x << 3 ) + ( x << 1 ) + s - '0', s = getchar(); }
    x *= f;
}

template<typename _T>
void write( _T x )
{
    if( x < 0 ){ putchar( '-' ), x = -x; }
    if( 9 < x ){ write( x / 10 ); }
    putchar( x % 10 + '0' );
}

int a[MAXSIZ];
int tot;

int qkpow( int base, int indx )
{
    int ret = 1;
    while( indx )
    {
        if( indx & 1 ) ( ret *= base ) %= mod;
        ( base *= base ) %= mod, indx >>= 1;
    }
    return ret;
}

int main()
{
    a[1] = 2, tot = 1;
    while( true )
    {
        tot ++, a[tot] = ( qkpow( 4, tot - 1 ) + qkpow( 2, tot - 1 ) ) % mod;
        if( a[tot] == a[3] && tot > 3 ) break;
    }
    tot --; 
    int T; ull N;
    while( true )
    {
        read( T );
        if( ! T ) break;
        for( int t = 1 ; t <= T ; t ++ )
        {
            read( N );
            if( N <= 2 ) printf( "Case %d: %d\n", t, a[N] );
            else printf( "Case %d: %d\n", t, a[( N - 3 ) % ( tot - 2 ) + 3] );
        }
        puts( "" );
    }
    return 0;
}