题目链接:http://codeforces.com/contest/327/problem/C
首先先算出一个周期里面的值,保存在ans里面,就是平常的快速幂模m做法.
然后要计算一个公式,比如有k个部分,那么对于没一个位置i, 都有2^i + 2^(i+n) + ... + 2^(i+(k-1)*n) = 2^i(1 + 2^n + ... + 2^((k-1)*n)) = 2^i * (1-2^(n*k))/(1-2^n)
所以结果就是ans * (1-2^(n*k))/(1-2^n) % MOD;
然后就是关键计算(1-2^(n*k))/(1-2^n) % MOD;
用到费马小定理a^(p-1)同余于1(mod 1).p是一个质数,那么a^(p-2) * a 同余于1(mod 1) ,所以a 的逆元就是 a^(p-2)
MOD是一个质数,所以(1-2^(n*k))/(1-2^n) % MOD = (2^(n*k)-1)/(2^n-1) % MOD = (2^(n*k)-1)%MOD * ((2^n-1)^(MOD-2))%MOD
1 /* 2 * ===================================================================================== 3 * Filename: magic.cpp 4 * Created: 19/07/2013 12:27:18 5 * Author: liuxueyang (lxy), 1459917536@qq.com 6 * Organization: Hunan University 7 * 8 * ===================================================================================== 9 */ 10 11 /* 12 ID: zypz4571 13 LANG: C++ 14 TASK: magic 15 */ 16 #include <cstdlib> 17 #include <iostream> 18 #include <cstdio> 19 #include <cstring> 20 #include <cmath> 21 #include <cctype> 22 #include <algorithm> 23 #include <queue> 24 #include <set> 25 #include <stack> 26 #include <map> 27 #include <list> 28 using namespace std; 29 #define INF 0x3f3f3f3f 30 const double eps=1e-9; 31 char a[100010]; 32 const int MOD=1000000007; 33 #define LL long long 34 int k; 35 LL quick(LL a, LL b) { 36 LL ans=1; 37 while (b) { 38 if(b&1) ans=(ans*a)%MOD; b/=2; a*=a; a%=MOD; 39 } 40 return ans; 41 } 42 int main ( int argc, char *argv[] ) 43 { 44 #ifndef ONLINE_JUDGE 45 freopen("in.txt", "r", stdin); 46 #endif 47 int k; string s; cin>>s>>k; LL n=s.size(); 48 LL a=quick(2,n*k)-1+MOD; a=(a+MOD)%MOD; 49 LL b=quick((quick(2,n)-1+MOD)%MOD,MOD-2); b=(b+MOD)%MOD; 50 LL ans=0; 51 for(size_t i = 0; i < n; ++i) if (s[i]=='0'||s[i]=='5') ans=(ans+quick(2,i))%MOD; 52 cout<<a*b%MOD*ans%MOD<<endl; 53 return EXIT_SUCCESS; 54 } /* ---------- end of function main ---------- */
搞定,收工