HDU 2814 - Interesting Fibonacci (Fibonacci性质 + 循环节)

题意

G(1)=F(ab)
G(n)=G(n1)F(ab)(n>=2)

G(n)modc

思路

abmodc 还有这么一个公式.

ab(amodc)bmodϕ(c)+ϕ(c)(modc),b>=ϕ(c)

所以这个题目就变成了求

ans=F(ab)F(ab)n1modc=(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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值