题意:
给了一个数字串s,和正整数k。k是s重复的遍数。
求有多少种不同的删除元素的方法,使得删除后的数字时5的倍数。
答案mod 1e9+7
思路:
(PS:此题有优化的DP解法, 但是看不懂怎么用矩阵搞的。。)
先解决不存在周期的情况。
考虑s[i]=0 或 5,将它右边的数全部删去,即将它变成末位。
则答案增加
2cnt[i]
, cnt[i]是其左边的位数。
答案则是
∑s[i]=0,52i(modM)
然后考虑k个周期,可以发现是一个等比数列求和,公比是
q=2|s|
答案写成
Q∑s[i]=0,52i(modM)
Q=∑k−1i=0qi
因为M是素数,所以可以把等比数列求和公式写出来然后用费马小定理解决a/b Mod M
但是,等比数列是可以用类似快速幂的方法计算的。
#include<bits/stdc++.h>
using namespace std;
#define SPEED_UP iostream::sync_with_stdio(false);
#define FIXED_FLOAT cout.setf(ios::fixed, ios::floatfield);
#define rep(i, s, t) for(int (i)=(s);(i)<=(t);++(i))
#define urep(i, s, t) for(int (i)=(s);(i)>=(t);--(i))
#define in_bound(l, r, i) (l)<=(i)&&(i)<(r)
#define pb push_back
typedef long long LL;
const int inf = INT_MAX/2;
const int Maxn = 4005;
const LL Mod = 1000000007;
string s;
LL k, p;
LL opt_pow_n(LL x, unsigned int n)
{
LL pw = 1;
while(n > 0){
if (n&1)
pw = pw*x%Mod;
x = x*x%Mod;
n = n >> 1;
}
return pw;
}
LL go(LL n) {
if (n == 1) return 1;
LL tmp = go(n/2);
if (n&1) {
return ((tmp+opt_pow_n(p, n/2)*tmp%Mod)%Mod+opt_pow_n(p, n-1))%Mod;
}
else {
return (tmp+opt_pow_n(p, n/2)*tmp%Mod)%Mod;
}
}
LL solve() {
LL ans = 0, n = s.length();
p = opt_pow_n(2, n);
LL base = go(k);
//cout << "sum of gp: " << base << endl;
rep(i, 0, n-1)
if (s[i] == '5' || s[i] == '0') {
LL a0 = opt_pow_n(2, i);
ans = (ans+a0*base%Mod)%Mod;
}
return ans;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
#endif
SPEED_UP
cin >> s >> k;
cout << solve() << endl;
return 0;
}