HDU 4704 Sum

题意

  给一个n , s(k):=(x1,x2,....,xk)的排列数(x1+x2+.....+xk=n)

  求[s(1)+s(2)+.....+s(n)] mod 1e9+7

思路

  用隔板法s(i)=C(n-1,i-1);∑s(i)(1<=i<=n)=2^n;

  接下来就是算2^N,N是一个很大的数。根据(2^123)^10*2^4(mod m)=2^1234(mod m)就可以递推出结果了;

#include<bits/stdc++.h>
using namespace std;
int power(int a,int n,int mod){//快速幂
    long long ret=1;
    long long k=a;
    while(n){
        if(n&1){
            ret*=k;
            ret%=mod;    
        }
        k=(k*k)%mod;
        n>>=1;
    }    
    return (int)ret;
}
int main(){
    char c;
    const int mod=1e9+7;
    int num=2;
    while(cin>>c){
        num=power(2,c-'0',mod);
        while(scanf("%c",&c)){
            if(c=='\n')break;
                num=power(num,10,mod);
                num=(1LL*num*power(2,c-'0',mod))%mod;        
        }
        if(num%2==0)cout<<num/2<<endl;
        else cout<<(num+mod)/2<<endl;
    }    
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Gsimt/p/10056112.html