首页 > 代码库 > UVA 11582 Colossal Fibonacci Numbers! 找循环节
UVA 11582 Colossal Fibonacci Numbers! 找循环节
注意n=1的情况
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>using namespace std;typedef unsigned long long LL;LL n,a,b,r;LL mypow(LL a,LL b) { if(b == 0) return 1; a %= r; if(b & 1) return mypow(a * a % r,b / 2) % r * a % r; else return mypow(a * a % r,b / 2) % r;}void solve() { LL now = 1,prev = 0,round = 1; while(1) { LL tmp = now; now = now + prev; prev = tmp; now %= n; prev %= n; if(round != 1 && now == 1 % n && prev == 0) break; round++; } r = round; LL t = mypow(a,b); if(t == 0) cout << 0 << endl; else if(t == 1) cout << 1 % n << endl; else { prev = 1; now = 0; for(int i = 1;i <= t;i++) { LL tmp = now; now += prev; now %= n; prev = tmp; } cout << now % n << endl; }}int main() { int T; cin >> T; while(T--) { cin >> a >> b >> n; solve(); } return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。