评测地址:http://acm.hust.edu.cn/vjudge/problem/41990

 The i'th Fibonacci number f (i) is recursively de ned in the following way:

 f () =  and f () = 

 f (i + ) = f (i + ) + f (i) for every i 

 Your task is to compute some values of this sequence.

 Input

 Input begins with an integer t
; , the number of test cases. Each test case consists of three in-tegers a, b, n where a; b < (a and b will not both be zero) and n . Output For each test case, output a single line containing the remainder of f (ab) upon division by n. Sample Input Sample Output

题目文本

题目大意:

输入两个非负整数a,b和正整数n(0<=a,b<2^64,1<=n<=1000),你的任务是计算f(a^b)除以n的余数。其中f(0)=f(1)=1,且对于所有非负整数i,f(i+2)=f(i+1)+f(i)。

解题思路:

这么大的数字,直接算是不现实的。

此时我们会想,要是f(i)% n能出现循环多好啊。

我们取F(i)=f(i) % n。若(F(i-1),F(i))出现重复,则整个序列将开始重复。 比如n=3时 1,1,2,0,2,2,1,0,1,1,2,0,2,2… 因为余数最多有n种可能,所以最多到第n^2项,就会出现重复,开始循环。 所以我们先花至多O(n^2)的时间处理一下找到循环节,再判断一下F(a^b)具体等于哪一项即可。

AC代码:

#include<cstdio>
#include<iostream>
#define ll unsigned long long
using namespace std;
const int N=1e6+;
int T,n,mod,f[N]={,,};
ll a,b;
int kpow(ll a,ll p){
int ans=;
for(;p;p>>=,a=(a*a)%mod) if(p&) ans=(ans*a)%mod;
return ans;
}
int main(){
cin>>T;
while(T--){
cin>>a>>b>>n;
if(n==||!a){printf("0\n");continue;}
for(int i=;i<=n*n+;i++){
f[i]=(f[i-]+f[i-])%n;
if(f[i]==f[]&&f[i-]==f[]){mod=i-;break;}
}
printf("%d\n",f[kpow(a%mod,b)]);
}
return ;
}

Colossal Fibonacci Numbers(巨大的斐波那契数)UVA 11582的更多相关文章

  1. UVA 11582 Colossal Fibonacci Numbers! 大斐波那契数

    大致题意:输入两个非负整数a,b和正整数n.计算f(a^b)%n.其中f[0]=f[1]=1, f[i+2]=f[i+1]+f[i]. 即计算大斐波那契数再取模. 一开始看到大斐波那契数,就想到了矩阵 ...

  2. [Swift]LeetCode509. 斐波那契数 | Fibonacci Number

    The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...

  3. Project Euler 104:Pandigital Fibonacci ends 两端为全数字的斐波那契数

    Pandigital Fibonacci ends The Fibonacci sequence is defined by the recurrence relation: F[n] = F[n-1 ...

  4. 【TOJ 3600】Fibonacci II (对数+斐波那契通项式)

    描述 2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列(f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2](i> ...

  5. hdu1568&&hdu3117 求斐波那契数前四位和后四位

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1568 题意:如标题所示,求斐波那契数前四位,不足四位直接输出答案 斐波那契数列通式: 当n<=2 ...

  6. 用x种方式求第n项斐波那契数,99%的人只会第一种

    大家好啊,我们又见面了.听说有人想学数据结构与算法却不知道从何下手?那你就认真看完本篇文章,或许能从中找到方法与技巧.     本期我们就从斐波那契数列的几种解法入手,感受算法的强大与奥妙吧. 原文链 ...

  7. DP:斐波纳契数

    题目:输出第 n 个斐波纳契数(Fibonacci) 方法一.简单递归 这个就不说了,小n怡情,大n伤身啊……当n=40的时候,就明显感觉到卡了,不是一般的慢. //输出第n个 Fibonacci 数 ...

  8. 算法笔记_001:斐波那契数的多种解法(Java)

    本篇文章解决的问题来源于算法设计与分析课程的课堂作业,主要是运用多种方法来计算斐波那契数.具体问题及解法如下: 一.问题1: 问题描述:利用迭代算法寻找不超过编程环境能够支持的最大整数的斐波那契数是第 ...

  9. golang 斐波那契数

    golang 斐波那契数 package main import "fmt" /* 斐波那契数,亦称之为斐波那契数列(意大利语: Successione di Fibonacci) ...

  10. 斐波那契数[XDU1049]

    Problem 1049 - 斐波那契数 Time Limit: 1000MS   Memory Limit: 65536KB   Difficulty: Total Submit: 1673  Ac ...

随机推荐

  1. Linux递归删除文件命令

    Linux递归删除文件命令 find . -name "*.log.*" -exec ls {} \; find . -name "*.log.*" -exec ...

  2. Codeforces Round #349 (Div. 2) D. World Tour (最短路)

    题目链接:http://codeforces.com/contest/667/problem/D 给你一个有向图,dis[i][j]表示i到j的最短路,让你求dis[u][i] + dis[i][j] ...

  3. PictureEdit中拖放图片

    public partial class Form2 : Form { string fileName = string.Empty; public Form2() { InitializeCompo ...

  4. idea 搭建java项目

    IntelliJ IDEA 12.0搭建Maven Web SSH2架构项目示例         以IDEA为环境,搭建SSH架构示例程序,用Maven管理依赖.这篇文章是一个示例,你需要首先搭建好M ...

  5. POJ 1979 dfs和bfs两种解法

      fengyun@fengyun-server:~/learn/acm/poj$ cat 1979.cpp #include<cstdio> #include<iostream&g ...

  6. git使用具体介绍

    1. Git概念  1.1. Git库中由三部分组成         Git 仓库就是那个.git 文件夹,当中存放的是我们所提交的文档索引内容,Git 可基于文档索引内容对其所管理的文档进行内容追踪 ...

  7. vector 与map的下标操作

    1.vector的下标操作不会添加元素,只能针对已经存在的元素操作. 2.map的下标操作具有副作用,key不存在,会在map中添加一个具有该key的新元素,新元素的value使用默认构造方法. 3. ...

  8. JQuery Plugin 1 - Simple Plugin

    1. How do you write a plugin in jQuery? You can extend the existing jQuery object by writing either ...

  9. STM8的wait for interrupt

    如果我用disable interrupt和enable interrupt包裹wait forinterrupt(WFI).你说WFI还能被唤醒么?有思考过么? 昨晚拿STM8L151K4的开发板, ...

  10. 通过程序 VB.Net 或 C# 读取文本文件行数

    1, VB.NET 读取 (通过streamReader) ' tmpCount = 0 'Dim tmpSR As New StreamReader(fileFullName, System.Tex ...