uva 11582
斐波那契数论 求第 a^b 项 %c
听说有循环结
#include<stdio.h> #include<algorithm> #include<cstring> #include<string> #include<cmath> using namespace std; #define MAXN 60010 #define inf 1000000000.0 unsigned long long z[10010]; unsigned long long quick(unsigned long long a,unsigned long long b,unsigned long long c) { unsigned long long ans=1; a = a % c; while(b>0) { if(b&1) ans = (ans * a) % c; b>>=1; a = (a * a) % c; } return ans; } int main() { int t; scanf("%d",&t); while(t--) { unsigned long long a,b,n; scanf("%llu%llu%llu",&a,&b,&n); if(a==0 || n== 1) { printf("0\n"); continue; } z[0]=0; z[1]=1; unsigned long long c; for(int i=2;;i++) { z[i]=(z[i-1]+z[i-2])%n; // printf("%llu ",z[i]); if(z[i]==1&&z[i-1]==0) { c=i-1; break; } } // printf("\n"); // printf("%llu\n",c); unsigned long long ans; ans = quick(a,b,c); printf("%llu\n",z[ans]); } return 0; }
posted on 2017-03-06 08:55 HelloWorld!--By-MJY 阅读(120) 评论(0) 编辑 收藏 举报