【数学】XMU 1597 GCD

题目链接:

  http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1597

题目大意

  求(am-bm, an-bn),结果取模1000000007,a,b互质(1<=b < a<= 1018,1<=m,n<=1018)

题目思路:

  【数论】

  gcd(am-bm,an-bn) mod p=(agcd(m,n)-bgcd(m,n))mod p=(a mod p)gcd(m,n) mod(p-1)-(b mod p)gcd(m,n) mod(p-1)

  快速幂。

 

 1 //
 2 //by coolxxx
 3 //
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<memory.h>
 9 #include<time.h>
10 #include<stdio.h>
11 #include<stdlib.h>
12 #include<string.h>
13 #include<stdbool.h>
14 #include<math.h>
15 #define min(a,b) ((a)<(b)?(a):(b))
16 #define max(a,b) ((a)>(b)?(a):(b))
17 #define abs(a) ((a)>0?(a):(-(a)))
18 #define lowbit(a) (a&(-a))
19 #define sqr(a) ((a)*(a))
20 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
21 #define eps 1e-8
22 #define J 10
23 #define MAX 0x7f7f7f7f
24 #define PI 3.1415926535897
25 #define mod 1000000007
26 using namespace std;
27 long long n,m,lll,ans,cas;
28 long long a,b,aa,bb;
29 long long gcd(long long a,long long b)
30 {
31     if(!b)return a;
32     return gcd(b,a%b);
33 }
34 long long quickpow(long long a,long long n)
35 {
36     long long c=a,t=1;
37     while(n)
38     {
39         if(n&1)t=(t*c)%mod;
40         c=(c*c)%mod;
41         n>>=1;
42     }
43     return t;
44 }
45 int main()
46 {
47     #ifndef ONLINE_JUDGE
48 //    freopen("1.txt","r",stdin);
49 //    freopen("2.txt","w",stdout);
50     #endif
51     int i,j,k;
52 //    while(~scanf("%s",s1))
53 //    while(~scanf("%d",&n))
54 //    for(scanf("%d",&cas),l=1;l<=cas;l++)
55     while(~scanf("%lld%lld%lld%lld",&a,&b,&m,&n))
56     {
57         a=(a-1)%mod+1;
58         b=(b-1)%mod+1;
59         lll=gcd(m,n)%(mod-1);
60         aa=quickpow(a,lll);
61         bb=quickpow(b,lll);
62         ans=(aa-bb+mod)%mod;
63         printf("%lld\n",ans);
64     }
65     return 0;
66 }
View Code

 

posted @ 2016-05-17 10:33  Cool639zhu  阅读(243)  评论(0编辑  收藏  举报