您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm