UVA-11582 数学

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;
}
时间: 2024-08-25 09:13:42

UVA-11582 数学的相关文章

UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】

题目链接:Uva 11582 [vjudge] 题意 输入两个非负整数a.b和正整数n(0<=a,b<=2^64,1<=n<=1000),让你计算f(a^b)对n取模的值,当中f(0) = 0,f(1) =  1.且对随意非负整数i.f(i+2)= f(i+1)+f(i). 分析 全部的计算都是对n取模.设F(i) =f(i)mod n, 非常easy发现,F(x)是具有周期性的,由于对N取模的值最多也就N个,当二元组(F(i-1),F(i))反复的时候.整个序列也就反复了.周期i

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(大fib,打表找循环节)

f (0) = 0 and f (1) = 1 f (i+2) = f (i+1) + f (i)  for every i ≥ 0 Sample input three integers a,b,n where 0 ≤ a,b < 264 (a and b will not both be zero) and 1 ≤ n ≤ 1000. T a  b  n 3 1 1 2 2 3 1000 18446744073709551615 18446744073709551615 1000 Sampl

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

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 11889-Benefit(数学_快速枚举因子)

Recently Yaghoub is playing a new trick to sell some more. When somebody gives him A Tomans, he who never has appropriate changes, asks for B Tomans such that lowest common multiple of A and B equals to C and he will pay back a round bill. Or otherwi

UVA 11582

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=2629&mosmsg=Submission+received+with+ID+16978519 我们是要计算 f(a^b)%n 根据式子 f(i)=f(i-1)+f(i-2)   f(i)%n=(f(i-1)%n+f(i-2)%n; 余数最多有n种,所以f

UVA 11582 - Colossal Fibonacci Numbers!(数论)(分治法幂取模)

巨大的斐波那契数! 题目大意:斐波那契数列f[N],给你a,b,n,求f[a^b]%n. 思路:数论题.f[a^b]%n是有周期的,我们求出来这个周期后就可以将简化成f[(a%周期)^b]%周期运用分治法幂取模. 注意用unsigned long long(貌似是 long long的二倍),不然会溢出,又学了一招... 不知道哪的bug,一直改不对,一直,后来捡来别人的和自己一样的代码一改就对了,,, #include<iostream>//UVA #include<cstdio>

UVa 11582 - Colossal Fibonacci Numbers!(数论)

链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2629 题意: 输入两个非负整数a.b和正整数n(0≤a,b<2^64,1≤n≤1000),你的任务是计算f(a^b)除以n的余数.其中f(0)=0,f(1)=1,且对于所有非负整数i,f(i+2)=f(i+1)+f(i). 分析: 所有计算都是对n取模的,设F(i)=f(i)

UVA 10127- Ones 数学

Given any integer 0 ≤ n ≤ 10000 not divisibleby 2 or 5, some multiple of n is a number whichin decimal notation is a sequence of 1’s. Howmany digits are in the smallest such a multipleof n?InputA ?le of integers at one integer per line.OutputEach out