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).
cf题目
题意:
给一个数n和一个数k,组成k个n(首位拼接)称为C
问通过删除C中的数有多少种方法使之后的C可被5整除
题解:取可以保留的最后一位,设位置为i,则此情况下有2^(i-1)种方法删除(i后面的全部删除),快速幂取每个可行位置提供的方法数,因为数据大小的缘故,还要考虑copyK次之后的幂,详见代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7, maxn = 100001;
int mod_pow(int a, int b)
{
int ans = 1;
while (b)
{
if (b & 1)ans = (ll)ans * a % mod;
a = (ll)a * a % mod;
b >>= 1;
}
return ans;
}
char s[maxn];
int k, f2[maxn];
int main()
{
f2[0] = 1;
//打表求幂
for (int i = 1; i < maxn; i++)
{
f2[i] = f2[i - 1] << 1;
if (f2[i] >= mod)f2[i] -= mod;
}
while (~scanf("%s%d", s, &k))
{
int ans = 0, len = strlen(s);
for (int i = 0; i < len; i++)
if (s[i] == '0' || s[i] == '5')
{
ans += f2[i];
if (ans >= mod)ans -= mod;
}
int a = f2[len];
ans = (ll)ans * (mod_pow(a, k) - 1) % mod * mod_pow(a - 1, mod - 2) % mod;
printf("%d\n", ans);
}
return 0;
}