I have done this kind of problem a long time ago .. that's because it's been too long .. I have been thinking about this question for a long time .. the sum formula of the Equality ratio is introduced, and Division operations are involved, so it is impossible to quickly modulo the power...
Just think of the Rapid power modulo of the proportional series ....
Returns the sum of the proportional series T [n] of k. when n is an even number .. T [n] = T [n/2] + pow (k, n/2) * T [n/2]
N is an odd number... T [n] = T [n/2] + pow (k, n/2) * T [n/2] + value of the nth Number of the proportional Series
For example, 1 + 2 + 4 + 8 = (1 + 2) + 4*(1 + 2)
1 + 2 + 4 + 8 + 16 = (1 + 2) + 4*(1 + 2) + 16
Program:
# Include <iostream> # include <stdio. h> # include <string. h> # include <set> # include <algorithm> # include <cmath> # define oo 1000000007 # define ll long # define pi acos (-1.0) # define MAXN 505 using namespace std; char s [100004]; ll m; ll POW (ll a, ll k) {ll x, ans = 1; x =; while (k) {if (k % 2) ans = (ans * x) % oo; x = (x * x) % oo; k/= 2 ;} return ans;} ll T (ll n, ll t) {if (n = 1) return t; ll data = T (n/2, t ); data = (data + data * POW (m, N/2) % oo; if (n % 2) data = (data + POW (m, (n-1) * t) % oo; return data ;} int main () {int k, I; ll ans, x, len; while (~ Scanf ("% s", s) {scanf ("% d", & k); len = strlen (s); m = POW (2, len ); ans = 0; x = 1; for (I = 0; I <len; I ++) {if (s [I] = '0' | s [I] = '5') ans = (ans + x) % oo; // do not do it every time .. add up x = (x * 2) % oo;} ans = T (k, ans); // only make one printf ("% I64d \ n", ans );} return 0 ;}