CF Magic Five 快速幂
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/*There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros. Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs. Look at the input part of the statement, s is given in a special form. Input In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|·k. Output Print a single integer — the required number of ways modulo 1000000007 (109 + 7). Sample test(s) input 1256 1 output 4 input 13990 2 output 528 input 555 2 output 63 Note In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125. In the second case, remember to concatenate the copies of a. The actual plate is 1399013990. In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits. 这道题应该用快速幂来求,若是对于项数很多的等比数列,应为求和公式中包含了除号,所以不能直接取mod,应该进行快速幂的转化 例如求sum=2^1+2^2+2^3+2^4+2^5+2^6+2^7 ....... 共有n项 这公式就为 若n%2==0 T(n)=T(n/2)+T(n/2)*2^(n/2); 若n%2==1 T(n)=T(n/2)+T(n/2)*2^(n/2)+ 2^n; 对于此题来讲 先把所给的循环位上的和求出来,做为基底,然后利用快速幂上面的公式求解接可以了 代码如下:*/ #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<math.h> #include<stdlib.h> using namespace std; const __int64 mod=1000000007; __int64 all; int Pow(__int64 n,int num) { //快速幂求 num^n if(n==0)return 1; if(n==1)return num; __int64 tmp=Pow(n/2,num)%mod; tmp=tmp*tmp%mod; if(n%2==1) tmp=tmp*num%mod; return (int)tmp; } __int64 len; int q; int fun(int n) { //上文所说的核心公式 if(n==1)return all; __int64 tmp=fun(n/2); tmp=(tmp+tmp*Pow(n/2,q))%mod; if(n%2==1) tmp=( tmp+Pow((n-1)*len,2)*all%mod )%mod; return (int)tmp; } char a[2000000]; int main() { int n,ans; while(scanf("%s",a)!=EOF) { cin>>n; ans=0; len=strlen(a); q=Pow(len,2)%mod; all=0; for(int i=0;i<len;i++) { if(a[i]=='0'||a[i]=='5') { all=(all+Pow(i,2))%mod; } //cout<<i<<" "<<ans<<endl; } //cout<<"all="<<all<<endl; ans=fun(n); cout<<ans<<endl; } return 0; }