Codeforces Round #191 (Div. 2) C. Magic Five(数学,逆元取膜模板)

来源:互联网 发布:淘宝店哪家大衣好看 编辑:程序博客网 时间:2024/06/05 08:44
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).

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


题目大意:给一个数字s然后重复k次,现在可以删除一些位置上的数字,但是要求删除后的s'满足能够被5整除的要求,问一共有多少种操作方法

解题思路:遍历数字s,对于i位置要是5或者0来说就有2的i次方的操作方法,同时要重复k次,对于该位置来说就是一个等比数列,用等比数列前n项和可以解决,但是在求的时候需要逆元取膜,要特别注意

#include<iostream>  #include<cstdio>#include<stdio.h>#include<cstring>#include<cstdio>#include<climits>   #include<cmath>#include<stack>#include<vector>  #include <bitset>  #include<algorithm>    #include <queue>  #include<map> #define inf 9999999; using namespace std;long long int pt=1e9+7;string str;int k,n;long long int cnt,sum;long long int fast_pow(long long int x,long long int y){long long int res;res=1;while(y!=0){if(y&1) res=(res*x)%pt;y>>=1;x=(x*x)%pt;}return res;}int main(){long long int i,j;cin>>str;cin>>k;n=str.length();cnt=0;for(i=0;i<n;i++){if(str[i]=='0'||str[i]=='5'){long long int a=fast_pow(2,i);long long int qn=fast_pow(fast_pow(2,n),k);a=a*(1-qn+pt)%pt;long long int c=fast_pow((1-fast_pow(2,n)+pt),pt-2)%pt;sum=(sum+a*c)%pt;}}cout<<sum<<endl;}


阅读全文
0 0
原创粉丝点击