码迷,mamicode.com
首页 > 其他好文 > 详细

UVA-11582 数学

时间:2017-08-13 16:52:55      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:bsp   数学   str   contest   题意   tps   names   cst   iostream   

UVA-11582

题意:

求f[a^b]%n ,其中f是斐波那契数列,1<=n<=1000,0<=a,b<=2^64;

代码:

//这题重点是要发现 f[i]%n会出现循环,然后找出他的循环节m取 a^b%m 即可 
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef  unsigned long long ull;
ull f[1000009];
int re[1009],t;
int pow_mod(ull a,ull b,ull mod){
    if(b==0) return 1;
    ull tmp=pow_mod(a,b/2,mod);
    tmp=tmp*tmp%mod;
    if(b&1) tmp=a*tmp%mod;
    return tmp;
}
int main()
{
    for(int i=2;i<=1000;i++){
        ull pre=1,pree=0,now,mod=i;
        for(int j=2;j<=1000000;j++){
            now=(pre+pree)%mod;
            if(now==1&&pre==0){
                re[i]=j-1;
                break;
            }
            pree=pre;pre=now;
        }
    }
    f[0]=0;f[1]=1;
    ull a,b;
    int n;
    scanf("%d",&t);
    while(t--){
        scanf("%llu%llu%d",&a,&b,&n);
        if(n==1||a==0){
            printf("0\n");
            continue;
        }
        int p=pow_mod(a%re[n],b,re[n]);
        //cout<<p<<endl;
        for(int i=2;i<=p;i++)
            f[i]=(f[i-1]+f[i-2])%n;
        printf("%llu\n",f[p]);
    }
    return 0;
}

 

UVA-11582 数学

标签:bsp   数学   str   contest   题意   tps   names   cst   iostream   

原文地址:http://www.cnblogs.com/--ZHIYUAN/p/7353875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!