您的位置:首页 > 其它

UVa11582 Colossal Fibonacci Numbers!

2015-09-06 19:39 281 查看
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 10005; //如果是1005就会RE,当不确定时,最好取大点。
int A[maxn];
#define LL unsigned long long // 注意不能用long long, 因为long long
// 最大值只能取到2 ^63 - 1
LL a, b;
int n, mod, T, t;

int Period()
{
A[0] = 0, A[1] = 1 % mod; //这里的取值也是要细心的。
int i = 2;
while(1)
{
A[i] = (A[i-1]+A[i-2]) % mod;
if(A[i] == A[0] && (A[i-1]+A[i])%mod == A[1]) return i;
i ++;
}
return 1;
}

int Pow(int x,LL n)
{
int ans = 1;
while(n)
{
if(n & 1) ans *= x;
x *= x, n >>= 1;
ans %= mod, x %= mod;
}
return ans;
}

int main()
{
scanf("%d", &t);
while(t --)
{
//scanf("%I64d %I64d %d", &a, &b, &n); 这两种方式在UVa中交都是WA,
//scanf("%lld %lld %d", &a, &b, &n);  特别注意当不确定时,最后用cin;
cin >> a >> b >> n;
mod = n;
T = Period();    //求出周期
mod = T;
int x = Pow(a%mod, b); //x为第一个周期中对应值的下标
printf("%d\n", A[x]);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: