uva 11582

 1                                       #include <iostream> 
 2                                       #include <map> 
 3                                       #include <cmath>
 4                                       #include <vector>
 5                                       #include <cstdio>
 6                                       #include <string>
 7                                       #include <cstring> 
 8                                       #include <algorithm>    
 9                                       using namespace std; 
10                                       #define fir first
11                                       #define sec second
12                                       #define pb(x) push_back(x) 
13                                       #define mem(A, X) memset(A, X, sizeof A)
14                                       #define REP(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
15                                       #define rep(i,l,u) for(int (i)=(int)(l);(i)>=(int)(u);--(i)) 
16                                       #define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e) 
17                                           
18                                       typedef pair<long,long>  pll;     
19                                       
20                                       
21                                       int T,n;
22                                       const int mod=1e9+7; 
23                                       const int maxn=1e3+10; 
24    typedef unsigned  long long ULL;  
25    ULL qsm(ULL a, ULL b, ULL mod)  // 0<=a<=ULL  0<=b<=ULL   
26   {   
27       a=a%mod;
28       ULL res=1;  
29       while(b)  
30       {  
31           //a=a%mod;(有时候n的值太大了会超出long long的储存,所以要先取余)  
32           if(b&1)//&位运算:判断二进制最后一位是0还是1,&的运算规则为前后都是1的时候才是1;  
33               res=res*a%mod;  
34           b=b>>1;//相当于除以2;  
35           a=a*a%mod;  
36       }  
37       return res;  
38   }        
39 ULL f[maxn*maxn+1000];
40                                       int main()
41                                       {
42                                            freopen("in.txt","r",stdin); 
43                                            //while(cin>>n)
44                                            while(cin>>T&&T)
45                                            { 
46                                              REP(kase,1,T)  {
47                                                  ULL ans;
48                                                  ULL a,b,n;
49                                                  cin>>a>>b>>n;
50                                                  if(a==0||n==1) ans=0;
51                                                  else
52                                                  {
53 
54                                                  f[1]=1;
55                                                  f[0]=0;
56 
57                                                  int M;
58                                                  REP(i,2,n*n+10)
59                                                  {
60                                                      f[i]=(f[i-1]+f[i-2])%n;
61                                                      if(f[i]==f[1]&&f[i-1]==f[0])
62                                                      {
63                                                          M=i-1;
64                                                          break;
65                                                      }
66                                                  } 
67                                                  ULL t=qsm(a,b,M);
68                                                  ans=f[t];
69                                              }
70                                                  cout<<ans<<endl;
71                                               }  
72                                            }
73                                         return 0;
74                                       }
75                           
76                                      /*
77                                         note    :  
78                                                     对于循环节类的问题,只需要找到刚开始的起始项,之后找到周期,利用周期的一致性,将查询到
79                                                     项直接进行周期M取模即可,注意f[0]是否进行了赋值,如果没有的话还需要f[0]=f[M].
80                                         debug   :   范围比较大需要用到ull
81                                                     re的原因有可能是因为数据类型不一致,(返回 runtime error )
82                                                      for(int i=1;i<=(unsigned long long)100;i++)
83                                                     
84                                         optimize:   分析出n的范围,预处理出来,可以避免大量的查询.分析出来了所有的基本可能情况.
85                                                     改进快速幂取模, 
86                                                     命名 进行程序变量的命名的时候,最好直接指代原来的变量例如 预处理n的时候,直接就是n就可以.
87                                                       REP(n,2,maxn)
88                                       */                                 

 

posted @ 2016-11-09 19:27  TechIsOnlyTool  阅读(137)  评论(0编辑  收藏  举报