标签:blank mat printf ble logs als tar lov ace
http://poj.org/problem?id=2417
求b^l = n%p;
已知b,n,p求l的一个算法
参考:http://
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <map> 5 using namespace std; 6 #define ll long long 7 8 ll b,n,p,l,x; 9 bool flag; 10 11 12 map<ll,ll>s; 13 14 15 long long qsm(long long x) //快速幂 16 { 17 long long sum=1; long long aa=b; 18 while (x>0) 19 { 20 if (x&1) 21 sum=(sum*aa)%p; 22 x=x>>1; 23 aa=(aa*aa)%p; 24 } 25 return sum; 26 } 27 28 void bs() 29 { 30 long long ans; 31 for (ll i=0;i<=x;i++) 32 { 33 if (i==0) 34 { 35 ans=n%p; s[ans]=i; continue; 36 } 37 ans=(ans*b)%p; 38 s[ans]=i; 39 } 40 } 41 42 void gs() 43 { 44 long long t=qsm(x), ans=1; 45 for (ll i=1;i<=x;i++) 46 { 47 ans=(ans*t)%p; 48 if (s[ans]) 49 { 50 int t=i*x-s[ans]; 51 printf("%d\n",(t%p+p)%p); 52 flag=true; 53 break; 54 } 55 } 56 } 57 58 int main() 59 { 60 //freopen("in.txt","r",stdin); 61 while(~scanf("%lld%lld%lld",&p,&b,&n)) 62 { 63 64 s.clear(); 65 flag = false; 66 x = sqrt(p)+1; 67 bs(); 68 gs(); 69 if(!flag) 70 printf("no solution\n"); 71 } 72 return 0; 73 }
blog.csdn.net/clover_hxy/article/details/50683832
标签:blank mat printf ble logs als tar lov ace
原文地址:http://www.cnblogs.com/Tree-dream/p/6714471.html