HDU 2814 - Interesting Fibonacci (Fibonacci性质 + 循环节)
2015-02-20 18:40
295 查看
题意
G(1)=F(ab)G(n)=G(n−1)F(ab)(n>=2)
求G(n)modc
思路
求abmodc还有这么一个公式.ab≡(amodc)bmodϕ(c)+ϕ(c)(modc),b>=ϕ(c)
所以这个题目就变成了求
ans=F(ab)F(ab)n−1modc=(F(ab)modc)F(ab)modϕ(c)+ϕ(c)modc
而Fibonacci数列有一个性质,它的n次方取模会出现一个循环节。假设循环节长度为len
F(ab)modc=F(abmodlen)modc
所以只要求出循环节的长度就行。
注意要特判ϕ(c)=1和c=1的情况。
代码
#include <stack> #include <cstdio> #include <list> #include <cassert> #include <set> #include <iostream> #include <string> #include <vector> #include <queue> #include <functional> #include <cstring> #include <algorithm> #include <cctype> #include <string> #include <map> #include <cmath> using namespace std; #define LL long long #define ULL unsigned long long #define SZ(x) (int)x.size() #define Lowbit(x) ((x) & (-x)) #define MP(a, b) make_pair(a, b) #define MS(arr, num) memset(arr, num, sizeof(arr)) #define PB push_back #define X first #define Y second #define ROP freopen("input.txt", "r", stdin); #define MID(a, b) (a + ((b - a) >> 1)) #define LC rt << 1, l, mid #define RC rt << 1|1, mid + 1, r #define LRT rt << 1 #define RRT rt << 1|1 const double PI = acos(-1.0); const int INF = 0x3f3f3f3f; const double eps = 1e-8; const int MAXN = 2e4 + 10; const int MOD = 1e9 + 7; const int dir[][2] = { {-1, 0}, {0, -1}, { 1, 0 }, { 0, 1 } }; const int hash_size = 4e5 + 10; int cases = 0; typedef pair<int, int> pii; ULL F[3000]; int get_phi(int n) { int m = (int)sqrt(n+0.5); int ans = n; for (int i = 2; i <= m; i++) if (n%i == 0) { ans = ans / i * (i-1); while (n % i == 0) n /= i; } if (n > 1) ans = ans / n * (n-1); return ans; } ULL pow_mod(ULL a, ULL m, ULL n) { a %= n; ULL ret = 1; while (m) { if (m & 1) ret = ret*a%n; a=a*a%n; m >>= 1; } return ret; } int get_loop(int num) { F[0] = 0; F[1] = 1; F[2] = 1; for (int i = 3; ; i++) { F[i] = (F[i-1]%num + F[i-2]%num) % num; if (F[i] == 1 && F[i-1] == 0) return i-1; } } int main() { //ROP; ios::sync_with_stdio(0); int T; cin >> T; while (T--) { ULL a, b, n, c; cin >> a >> b >> n >> c; if (c == 1) { cout << "Case " << ++cases << ": 0" << endl; continue; } ULL oula = get_phi(c); int loop = get_loop(c); ULL c1 = pow_mod(a, b, loop); c1 = F[c1]; if (oula == 1) { cout << "Case " << ++cases << ": " << c1 << endl; continue; } loop = get_loop(oula); ULL t1 = pow_mod(a, b, loop); t1 = F[t1]; ULL mi = pow_mod(t1, n-1, oula) + oula; cout << "Case " << ++cases << ": " << pow_mod(c1, mi, c) << endl; } return 0; }
相关文章推荐
- HDU 2814 斐波那契循环节+欧拉函数降幂
- 多校第十场 HDU 3936 FIB Query(fibonacci 数列的性质 ,及Ologn 矩阵加速乘算法)
- hdu 1568 Fibonacci(fibonacci通项+对数性质)
- HDU 2814 斐波那契数列的循环节问题
- HDU 2814 Interesting Fibonacci 循环节
- hdu-2814-Interesting Fibonacci-斐波那契循环节
- HDU 1848 Fibonacci again and again (博弈,SG函数)
- HDU 4818 RP problem (2013年长春现场赛F题) 高斯消元性质利用
- hdu1848 Fibonacci again and again(SG)
- hdu 5167 Fibonacci(dfs)
- 2017多校第一场 HDU 6038 Function 循环节,脑洞,大思维题
- HDU 1005 循环节(找规律题目)
- hdu 1568 Fibonacci
- hdu 2203 亲和串(给两个字符串s1,s2,问s2可不可能出现在以s1为循环节的串中)
- HDU 3978 斐波那契循环节
- hdu 1568 Fibonacci
- (KMP 1.5)hdu 1358 Period(使用next数组来求最小循环节——求到第i个字符的循环节数)
- HDu 1358 Period(Next 数组找循环节)
- 【HDU 1848 Fibonacci again and again】 + sg函数
- hdu 1250 Hat's Fibonacci