Magic Five CodeForces

来源:互联网 发布:网络黄金egd裴蕾抓了吗 编辑:程序博客网 时间:2024/05/16 06:44

题目链接:点我


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).

Example
Input

12561Output4Input139902Output528Input5552Output63

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.

题意:

给你一段重复k次的数字, 问你它有多少种方式使得从中删除一些数字和能使得这个新的数字能整除5.

思路:

仔细分析我们发现如果那串数字的第 i 为是 5 或者 0,那么它前面的i - 1 个数字有删或者不删除两种情况 即: 2i1,于是我们可以算出重复一次的情况,至于重复k 次我们发现就是一个等比数列求和,由于有除法取模,于是还要计算逆元.

代码:

#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<cmath>using namespace std;char ss[100000+10];typedef long long LL;const int mod = 1e9 + 7;LL qpow(LL x,LL n){    LL ans = 1;    while(n){        if(n&1) ans = ans * x % mod;        x = x* x% mod;        n >>= 1;    }    return ans;}int main(){    LL k;    scanf("%s %I64d", ss,&k);    int l = 0;    int len =strlen(ss);    LL cnt = 0;   for(int  i = 0; i<len;++i)    if(ss[i]=='0'||ss[i] == '5')            cnt =(cnt + qpow(2,i)) % mod; //首项    LL x = qpow(2,len); //公比    LL y = qpow(x,k);    x=(1-x + mod) % mod;    y= (1-y + mod ) % mod;   LL  ans = cnt * (y * qpow(x, mod-2) %mod) %mod;    printf("%I64d\n",(ans + mod) % mod);    return 0;}
原创粉丝点击