C. Magic Five

来源:互联网 发布:怎么装修企业淘宝店铺 编辑:程序博客网 时间:2024/06/05 07:00

C. Magic Five
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 = |ak.

Output

Print a single integer — the required number of ways modulo 1000000007 (109 + 7).

Sample test(s)
input
12561
output
4
input
139902
output
528
input
5552
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.


code:

#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <iostream>using namespace std;int vis[100020]; char f[100020];__int64 powe_m(__int64 a,__int64 b,__int64 c)    //注意要int64接收变量  否则会溢出{__int64 ans=1; __int64 tmp=a;while(b!=0){ if (b&1) ans=ans*tmp%c;    //???????? ans=ans*ans%c ??????tmp=tmp*tmp%c;b=b>>1;}return ans; }int main(){int  i,k;    注意要int64 变量  否则k在计算k*strlen(f)的时候会溢出   ,或者给k*strlen(f)来个强制转换最好直接在k定义的时候__int64,也是提醒我们该变量不超过int范围不一定就绝对得定义为int,还要考虑计算的过程是否超过int。__int64 ans=0; scanf("%s",f);scanf("%d",&k); for (i=strlen(f)-1;i>=0;i--){if (f[i]=='0'||f[i]=='5') vis[i]++;}int qiandao0=0;i=0;while(f[i]==0){qiandao0++;i++;}                    __int64 ccd= powe_m(2,strlen(f),1000000007) -1;__int64 ab= powe_m(2,(__int64)(k)*strlen(f),1000000007)-1;__int64 big= (ab*(powe_m(ccd,(1000000007-2),1000000007 ) ) )%1000000007;   //神一样的公式
////除法取余方法的一种,(a/b)%mod,等价于(a*b^(mod-2))%mod,mod为素数        //如果不这样的话。a%mod / b%mod 已经和 (a/b)%mod 不相同了。
//这里有点乱。。其实按原来的话是 下面的for循环每次都要计算k次(k最大10^9) 必然超时,刚好发现能化简,最后是等比数列,所以就有了上面的乱七八糟的计算。for (i=strlen(f)-1;i>=0;i--){if (vis[i]){                ans+= powe_m (2,i,1000000007) ;ans%=1000000007;     //自己原来是每次*big   其实可以再最后才*big}} printf("%I64d\n",ans*big%1000000007);return 0;} 



0 0
原创粉丝点击