K - Magic Five 逆元

K - Magic Five
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status
Appoint description: 

Description

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 Input

Input
1256
1
Output
4
Input
13990
2
Output
528
Input
555
2
Output
63
const int INF = 1000000000 + 7;
const double eps = 1e-8;
const int maxn = 300000;

LL Pow(LL x,LL y,LL c)
{
    if(y == 0)
        return 1;
    LL ans = Pow(x,y/2,c);
    ans = ans*ans%c;
    if(y%2)
        ans = ans*x%c;
    return ans;
}
char str[maxn];
int main() 
{
    //freopen("in.txt","r",stdin);
    while(scanf("%s",str) == 1)
    {
        int re;
        scanf("%d",&re);
        LL ans = 0;
        int len = strlen(str);
        int cnt = 0;
        rep(i,0,len)
        {
            if(str[i] == '0' || str[i] == '5')
            {
                LL b = (Pow(2,len,INF) - 1);
                b = Pow(b,INF-2,INF);
                ans  = (ans + b*(Pow(2,cnt,INF)*(Pow(2,(LL)len*re,INF)- 1)%INF)%INF)%INF;
            }
            cnt++;
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

posted on 2013-11-22 15:56  keep trying  阅读(240)  评论(0编辑  收藏  举报

导航