码迷,mamicode.com
首页 > 其他好文 > 详细

乘法逆元+快速幂

时间:2018-04-30 18:10:54      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:print   mes   isa   math.h   algorithm   main   namespace   include   +=   

唉...

 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 char str[maxn];
32 
33 //计算a的b次方取模
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 }

 

乘法逆元+快速幂

标签:print   mes   isa   math.h   algorithm   main   namespace   include   +=   

原文地址:https://www.cnblogs.com/chaoswr/p/8973836.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!