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
相关文章推荐
- &&在goto语句中的用法
- WebRTC手记之本地音频采集
- 数据库连接池Druid
- LightOJ 1427 Substring Frequency (II)(AC自动机裸)
- HDU 5422 Rikka with Graph(水题)——BestCoder Round #53(div.2)
- 百度天气api初体验
- BZOJ 2788 Poi2012 Festival 差分约束+Tarjan+Floyd
- postgreps:The database cluster initialisation failed
- redis 教程命令
- beanstalkd消息队列在生产环境的应用
- 浅谈css中的position
- JAVA随笔(一)
- 复旦考研信息整理
- 欧拉工程第66题:Diophantine equation
- linux日志切割
- c指针详解
- Android Fragment 真正的完全解析
- leetcode刷题,总结,记录,备忘 117
- MyBatis学习总结——Mybatis3.x与Spring4.x整合
- Linux 命令[6]:rm