Description
Rivest 是密码学专家。近日他正在研究一种数列E = {E[1],E[2],……,E[n]}，

d = E[n] mod q。现在Rivest 想对一组数据进行加密，但他对程序设计不太感兴趣，请你帮

Input

Output

6 输入样例2 4 7 2 4 7 1 6 5 9 3
Sample Input

2 7
4 5
4 6

4 7
2 4
7 1
6 5
9 3
Sample Output

31

3011

solution

d=p^(fibonaqi[n]) mod q

a^b%k = a^(b%φ(k)+φ(k)) % k          gcd(a,k)!=1

 1 ll oula(ll t)
2 {
3   ll kk=t;
4   for(ll i=2;i*i<=t;++i)
5   if(t%i==0)
6   {
7     kk=kk-kk/i;
8     while(t%i==0)
9       t/=i;
10   }
11   if(t>1)kk=kk-kk/t;
12   return kk;
13 }

 1 #include<cstdio>
2 #include<cstring>
3 #include<iostream>
4 #define ll long long
5 #define mem(a,b) memset(a,b,sizeof(a))
6 using namespace std;
7
8 int m,p,n;
9 ll q;
10 ll f[6][6],a[6][6],temp[6][6];
11 ll mod;
12
13 ll oula(ll t)
14 {
15     ll kk=t;
16     for(ll i=2;i*i<=t;++i)
17       if(t%i==0)
18       {
19             kk=kk-kk/i;
20             while(t%i==0)
21               t/=i;
22         }
23     if(t>1)kk=kk-kk/t;
24     return kk;
25 }
26
27 inline void chu()
28 {
29     mem(f,0);
30     mem(a,0);
31     f[1][1]=1;f[2][2]=1;
32     a[1][1]=1;a[1][2]=1;a[2][1]=1;
33 }
34
35 inline ll cheng()
36 {
37     --n;
38     while(n)
39     {
40         if(n&1)
41         {
42             for(int i=1;i<=2;++i)
43               for(int j=1;j<=2;++j)
44               {
45                     temp[i][j]=0;
46                     for(int k=1;k<=2;++k)
47                       temp[i][j]=(temp[i][j]+f[i][k]*a[k][j]%mod)%mod;
48                 }
49             for(int i=1;i<=2;++i)
50               for(int j=1;j<=2;++j)
51                 f[i][j]=temp[i][j];
52         }
53         for(int i=1;i<=2;++i)
54           for(int j=1;j<=2;++j)
55           {
56                 temp[i][j]=0;
57                 for(int k=1;k<=2;++k)
58                   temp[i][j]=(temp[i][j]+a[i][k]*a[k][j]%mod)%mod;
59             }
60         for(int i=1;i<=2;++i)
61           for(int j=1;j<=2;++j)
62              a[i][j]=temp[i][j];
63
64         n>>=1;
65     }
66     return f[1][1];
67 }
68
69 ll mi(ll x,ll c,ll qw)
70 {
71     ll ans=1;
72     while(c)
73     {
74         if(c&1)
75           ans=(ans*x)%qw;
76         x=(x*x)%qw;
77         c>>=1;
78     }
79     return ans;
80 }
81
82 int main(){
83     scanf("%d%d",&m,&p);
84     for(int i=1;i<=m;++i)
85     {
86         scanf("%d%lld",&n,&q);
87         chu();
88         mod=oula(q);
89         ll temp=cheng();
90         printf("%lld\n",mi(p,temp,q)%q);
91
92     }
93     return 0;
94 }
code

posted @ 2017-07-30 18:33  A_LEAF  阅读(355)  评论(0编辑  收藏  举报