UVA11582 Colossal Fibonacci Numbers!(fibonacci序列模x的周期性)

题意:两个非负整数a,b<2^64 计算斐波拉契序列f(a^b)mod x  (x<1000)

思路:显然a^b只能用快速幂,而且还必须要取模,所以去尝试找f(n)mod x的周期

不能发现当二元组(f[i]%x,fp[i-1]%x)=(f[0]%x,f[1]%x) 的时候开始循环,所以周期为i

因为f[n]%x的余数最多只有1000种所以在f[0...n^2]以内就能找到周期

<span style="font-size:14px;">//	Accepted	C++	0.096
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

typedef unsigned long long ll;
int period[1005];
int an[101000];

ll powmod(ll a,ll b,ll mod)
{
    ll ans=1;
    while(b)
    {
        if(b&1) ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}

int main()
{
    int T;
    scanf("%d",&T);
    int a0,a1;
    for(int i=1;i<=1000;i++)
    {
        a0=0%i,a1=1%i;
        for(int j=2;;j++)
        {
            int a2=(a0+a1)%i;
            if(a2==1%i&&a1==0) {period[i]=j-1;break;} //存周期
            a0=a1,a1=a2;
        }
    }
    while(T--)
    {
        ll a,b;
        int mod;
        cin>>a>>b>>mod;
        int p=powmod(a%period[mod],b,period[mod]);
        an[0]=0,an[1]=1%mod;
        for(int i=2;i<=p;i++) an[i]=(an[i-1]+an[i-2])%mod;
        if(a==0&&b==0) puts("0");
        else printf("%d\n",an[p]);
    }
    return 0;
}
</span>
时间: 2024-11-05 18:51:41

UVA11582 Colossal Fibonacci Numbers!(fibonacci序列模x的周期性)的相关文章

HDU 3117 Fibonacci Numbers(Fibonacci矩阵加速递推+公式)

题目意思很简单:求第n个Fibonacci数,如果超过八位输出前四位和后四位中间输出...,否则直接输出Fibonacci数是多少. 后四位很好求,直接矩阵加速递推对10000取余的结果就是. 前四位搜了一下:http://blog.csdn.net/xieqinghuang/article/details/7789908 Fibonacci的通项公式,对,fibonacci数是有通项公式的-- f(n)=1/sqrt(5)(((1+sqrt(5))/2)^n+((1-sqrt(5))/2)^n

UVA 11582 Colossal Fibonacci Numbers! 数学

n比较小,最多n*n就回出现循环节.... Colossal Fibonacci Numbers! Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem F: Colossal Fibonacci Numbers! The i'th Fibonacci number f (i) is recursively defined in the

UVA 11582 Colossal Fibonacci Numbers!(打表+快速幂)

Colossal Fibonacci Numbers! The i'th Fibonacci number f (i) is recursively defined in the following way: f (0) = 0 and f (1) = 1 f (i+2) = f (i+1) + f (i)  for every i ≥ 0 Your task is to compute some values of this sequence. Input begins with an int

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> #inc

codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 = 1; F2 = 1; Fn = Fn - 1 + Fn - 2 (n > 2). DZY loves Fibonacci numbers very much. Today DZY gives you an array consisting of n integers: a1, a2, ...,

Codeforces446C - DZY Loves Fibonacci Numbers

Portal Description 给出一个\(n(n\leq3\times10^5)\)个数的序列,进行\(m(m\leq3\times10^5)\)次操作,操作有两种: 给区间\([L,R]\)加上一个斐波那契数列,即\(\{a_L,a_{L+1},...,a_R\} \rightarrow \{a_L+F_1,a_{L+1}+F_2,...,a_R+F_{R-L+1}\}\) 询问区间\([L,R]\)的和,对\(10^9+9\)取模. 斐波那契数列:\(F_1=1,F_2=2\)且满足

zoj Fibonacci Numbers ( java , 简单 ,大数)

题目 //f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2) import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @xqq */ public BigInteger an(int n) { BigInteger c; BigInteger a = BigInteger.valueOf(1); BigInteger b = BigIn

hdu 3117 Fibonacci Numbers

点击此处即可传送到hdu 3117 **Fibonacci Numbers** Problem Description The Fibonacci sequence is the sequence of numbers such that every element is equal to the sum of the two previous elements, except for the first two elements f0 and f1 which are respectively

Even Fibonacci numbers

problem 2:Even Fibonacci numbers 题意:求斐波那契数列不大于4百万的值为偶数的和 代码如下: 1 #ifndef PRO2_H_INCLUDED 2 #define PRO2_H_INCLUDED 3 #include<iostream> 4 using namespace std; 5 6 namespace pro2{ 7 int solve(){ 8 int p1=1,p2=2,p,sum=0; 9 for(;p1<=4000000;){ 10 if