UVa11582

Colossal Fibonacci Numbers!
Oooh...pretty
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
Input begins with an integer t 10; 000,
the number of test cases. Each test case
consists of three integers a, b, n where
0 a; b < 264 (a and b will not both be
zero) and 1 n 1000.
Output
For each test case, output a single line containing the remainder of f(ab) upon division by n.
Sample Input
31
1 2
2 3 1000
18446744073709551615 18446744073709551615 1000
Sample Output
1
21
250

题意:

给出64位整数a、b以及不超过1000的正整数n,求斐波那契数列第a ^ b项模n的结果。

输入:

情况数T,之后T行每行a、b、n。

输出:

斐波那契数列第a ^ b项模n的结果。

分析:

由于斐波那契数列的每一项都是由前两项相加得来,并且每一项都对n取模,所有每一项的情况一共有n种,而相邻两项若组成有序数对,则不同的数对的情况也只有n ^ 2种,所以只需要计算n * n项就可以找到数列规律。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 typedef unsigned long long ull;
 6 const int maxn = 1000;
 7 ull F[maxn * maxn + 10];
 8 int power(ull a,ull b,int t){
 9     ull res = 1;
10     while(b){
11         if(b & 1) res = (a * res) % t;
12         a = (a * a) % t;
13         b >>= 1;
14     }
15     return res;
16 }
17 int main()
18 {
19     int T;
20     cin >> T;
21     while(T--)
22     {
23         ull a,b,n,l;
24         int flag = 1;
25         cin >> a >> b >> n;
26         if(n == 1) {printf("0\n"); continue;}
27         F[0] = 0;
28         F[1] = 1;
29         for(ull i = 2 ; ; i++){
30             F[i] = (F[i - 1] % n + F[i - 2] % n) % n;
31             if(F[i] == F[1] && F[i - 1] == F[0]){
32                 l = i - 1;
33                 break;
34             }
35         }
36         cout << F[power(a % l, b, l)] << endl;
37     }
38     return 0;
39 }

时间: 2024-12-26 22:03:22

UVa11582的相关文章

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[100

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

UVa11582巨大的斐波那契数

1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <vector> 6 #include <iostream> 7 #include <algorithm> 8 using namespace std; 9 typedef unsigned long long ull; 10 const i

UVa11582 Colossal Fibonacci Numbers!

1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int maxn = 10005; //如果是1005就会RE,当不确定时,最好取大点. 7 int A[maxn]; 8 #define LL unsigned long long // 注意不能用long long, 因为long

UVa11582 - Colossal Fibonacci Numbers!(模运算)

#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<stack> #include<vector> #include<map> #include<set> #include<queue> #include<algorithm> using namespace std; typedef

[快速幂][UVa11582]巨大的斐波那契数列!

题目大意 输入a, b, n, 计算f( a ^ b) % n  , f( i ) = f( i - 1) + f( i - 2 ) , f( 0 ) = f(1) = 1, 其中,0 <= a , b <= 2 ^ 64,  0 <= n <= 1000 思考 首先扔出两个结论(证明过程链接在此,个人水平有限看不懂.) 1.斐波那契数列模n 必有循环节. 2.斐波那契数列模n 的情况下,最多n^2项就会出现重复. 先找出循环节M,之后计算a^b%M 是多少,输出fib[a^b%M

uva11582 Colossal Fibonacci Numbers(分治法)

题意:输入两个非负整数a.b和正整数n(a,b>=0&&a,b<2^64,n>=1&&n<=1000),你的任务是计算f(a^b)除以n的余数.其中f(0)=f(1),且对于所有非负整数i,f(i+2)=f(i+1)+f(i) 解题思路:设F(i)=f(i) mod n.不难发现当(F(i),F(i+1))重复出现时,整个序列就开始重复.所以我们需要找到对于不同的n重复周期,因为余数有n中,所以最多n^2项就会出现重复(其实我们也可以通过打表观察,在

【UVA11582】巨大的斐波那契数

题意 输入两个非负整数a.b和正整数n(0<=a,b<264,1<=n<=1000),你的任务是计算f(ab)除以n的余数,f(0) = 0, f(1) = 1,且对于所有非负整数i,f(i + 2) = f(i + 1) + f(i). 分析 首先可以观察到n是很小的,意思是n的完全剩余系的元素个数也不超过1e3个,所以设F[i]=f(i)%n,则F函数的循环节也不会超过1e3的长度(应该是吧,我推测的) 多组数据,我们就预处理出每个n的循环节,再跑快速幂取模就行了,注意:ab取

深坑之---数学

本学期的数学生涯正式开始,第一个进军的毫无疑问是数论. 现在还属于学习和消化阶段,写这样零散地整理知识点,以后再总结. 1. 模算术 ( 快速幂取模 ) //在O(logn)的时间复杂度完成 a^b % m 的计算 //另外还有模算术 (a+b)%c =( a%c + b%c ) %c (a-b)%c =( a%c - b%c + c) %c (ab)%c = (a%c)(b%c) % c LL fast_mod(LL a,LL b,LL n) { LL ans = 1; while(b) {