CF_327C_MagicFive

来源:互联网 发布:婴儿被子品牌知乎 编辑:程序博客网 时间:2024/06/05 07:38

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 by5. Note that, the resulting number may contain leading zeros.

Now Iahub wants to count the number of ways he can obtain magic number, modulo1000000007 (109 + 7). Two ways are different, if the set of deleted positions ins 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 plates is formed by concatenatingk copies of a together. That isn = |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 are26 - 1 = 63 possible ways to delete digits.


这个题目是一道快速幂的问题

注意题中重复后是一个等比数列求和问题

因为MO是质数可以用费马小定理处理除法

#include <iostream>#include <stdio.h>#include <string.h>using namespace std;typedef long long LL;const int MO=1000000007; //注意这是一个质数const int M=100005;          //这里要看明白给的是串的长度char a[M];LL qp(LL m,LL n){    LL r=1;    while(n>0)    {        if(n&1)           r=(r*m)%MO;        m=(m*m)%MO;        n>>=1;    }    return r;}LL db(LL w,LL k){    LL tmp=qp(2,w);    LL re=qp(tmp,k);    re=(1-re+MO)%MO;    tmp=(1-tmp+MO)%MO;    return re*qp(tmp,MO-2)%MO;  //这里使用了费马小定理因为题中MO是个质数}int main(){    int k;    int nd,w;    LL tot;    while(scanf("%s%d",a,&k)!=EOF)    {        tot=0;        w=strlen(a);nd=0;        for(int i=0;i<w;i++)        {            if(a[i]=='0'||a[i]=='5')            {                nd++;                tot=(tot+qp(2,i))%MO;            }        }        printf("%I64d\n",tot*db(w,k)%MO);    }    return 0;}

或者用分治来处理

 

int dfs(ll cur){    if(cur==1)return x+1;    if(cur==0)return 1;int m=(cur-1)/2;    int temp=dfs(m);    int ret=(ll)temp*Pow(x,m+1)%Mod;;    ret=(ret+temp)%Mod;    if(!(cur&1LL)){        ret=ret+Pow(x,cur);        ret%=Mod;    }    return ret;}



0 0
原创粉丝点击