Multiplicative Inverse + Fast Power

Ugh...

 1 #include <iostream>
 2 #include <string.h>
 3 #include <cstdio>
 4 #include <queue>
 5 #include <map>
 6 #include <vector>
 7 #include <string>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <math.h>
11 
12 #define SIGMA_SIZE 26
13 #pragma warning ( disable : 4996 )
14 
15 using namespace std;
16 typedef long long LL;
17 
18 inline LL LMax(LL a,LL b)    { return a>b?a:b; }
19 inline LL LMin(LL a,LL b)    { return a>b?b:a; }
20 inline int Max(int a,int b) { return a>b?a:b; }
21 inline int Min(int a,int b) { return a>b?b:a; }
22 inline int gcd( int a, int b ) { return b==0?a:gcd(b,a%b); }
23 inline int lcm( int a, int b ) { return a/gcd(a,b)*b; }  //a*b = gcd*lcm
24 const LL INF = 0x3f3f3f3f3f3f3f3f;
25 const LL mod  = 1000000007;
26 const int inf  = 0x3f3f3f3f;
27 const int maxk = 1e5+5;
28 const int maxn = 1e5+5;
29 
30 int K;
31 charstr[maxn];
 32  
33  // Calculate a's b power modulo 
34  LL getm( LL a, LL b, LL m )
 35  {
 36      LL ans = 1 , base = a;
 37      while ( b )  
 38      {  
 39          if ( b & 1 )
 40              ans = (ans * base ) % m;
 41          base = ( base * base ) % m;
 42          b >>= 1 ;  
 43      }  
 44      return ans;
45 }
46 
47 // 求a关于p的逆元
48 LL getn( LL a, LL p )
49 { return getm( a, p-2, p ); }
50 
51 int main()
52 {
53     cin >> K;
54     scanf( "%s", str );
55 
56     LL ans = 0;
57     int len = strlen(str);
58 
59     LL inv, a1 = 0;
60     LL p = 1, tmp = getm( 2, len, mod );
61     LL sum = 0;
62 
63 
64 
65     p = getm( tmp, K, mod );
66     inv = getn( tmp-1, mod );
67     sum = ( ((p-1)%mod) * (inv%mod) ) %mod;
68 
69     for (int i = len - 1; i >= 0; i--)
70     {
71         if (str[i] == '0' || str[i] == '5')
72         {
73             a1 = getm( 2, i, mod );
74             ans += (a1*sum)%mod;
75             ans %= mod;
76         }
77     }
78 
79     printf( "%lld\n", ans );
80 
81     return 0;
82 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325114212&siteId=291194637