G

086戴澎峰 - 数论入门.随堂练习.7 - 5的倍数的个数

public
Guest May 13, 2025 Never 16
Clone
C++ 086戴澎峰 - 数论入门.随堂练习.7 - 5的倍数的个数 52 lines (44 loc) | 887 Bytes
1
#include <bits/stdc++.h>
2
using namespace std;
3
#define LL long long
4
#define int LL
5
#define INF 0x3f3f3f3f
6
const int N = 2e6+5, mod = 1e9+7;
7
8
LL qpow(LL a, LL x) {
9
LL ans = 1;
10
while(x) {
11
if(x&1) ans *= a;
12
a *= a;
13
x >>= 1;
14
a %= mod;
15
ans %= mod;
16
}
17
return ans;
18
}
19
20
LL inv(LL a) { //也可以用 费马小定理
21
if(a==1) return 1ll;
22
return (mod - mod/a * inv(mod%a)) %mod;
23
}
24
25
int p[N], cnt;
26
27
void solve() {
28
string a;
29
LL k;
30
cin >> a >> k;
31
int len = a.length();
32
33
LL qsum = ( qpow(2ll,len*k) - 1 ) * inv( qpow(2LL,(LL)len) - 1 ) % mod;
34
35
for(int i=0;i<len;i++) {
36
if(a[i]=='0'||a[i]=='5')
37
p[cnt++] = i;
38
}
39
40
LL ans = 0;
41
for(int i=cnt-1;~i;i--) {
42
ans += qpow(2,p[i]) * qsum;
43
ans %= mod;
44
}
45
cout << (ans%mod+mod)%mod;
46
}
47
48
signed main() {
49
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
50
solve();
51
return 0;
52
}