UVa-11582: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
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
3
1 1 2
2 3 1000
18446744073709551615 18446744073709551615 1000
Sample Output
1
21
250
概译:对于斐波那契数列f = {0,1,1,2……},给出n,f数列的所有项都%=n,还给了a和b,求f[ab]等于几。
思路:
发现a和b极大所以肯定是找规律了
--> 斐波那契是固定的那循环长度应该跟n有关
--> 余数最多有n种,(注意序列是0,1开头),则最坏情况下n个数以后又爆出0
--> 斐波那契数列只要确定两个数则后面的序列都是确定的,确定了0以后,0后面那个数一旦确定则循环节就确定了
--> 最坏情况,0的后面n次以后爆出1
--> 故在n²个数以内必出循环节,n²的暴力还是可以承受的,找一下循环节长度L,输出f[ab%L(快速幂)]
PS:不妨简单证明一下0的后面最坏n次以后必爆1.
假如序列会发生:01abc,02xyz,02xyz,01……这种情况,则由于非1数字先行重复了,会导致我们未必在n次以内捕获数字1.但这种情况真的会存在吗?
--> 如果真的存在了,由于z+0=2,所以一旦02循环,则02将成为永远的循环节,不会再出现01了,只会有01abc,02xyz,02xyz,02xyz,……
--> (c+0)%n=2,c又在0~n之内,c=2=z
--> (b+c)%n=0,b又在0~n之内,b=n-2=y
--> ……以此类推,01何以创造02帝国?
--> 矛盾。故斐波那契一定是以01开头的循环节,n次内必爆1.
声明:由于紫书和题解们都是一笔描过n²内可求解,故上述想法为个人脑洞,欢迎纠错与讨论。
50ms
#include <bits/stdc++.h>
using namespace std; typedef unsigned long long ull; int test, n, f[]={,};
ull a, b; ull qpow(ull a, ull b, int mod)//为了防爆ull,a传入a%mod
{
ull ans = ;
while (b)
{
if (b% == ) ans = ans*a%mod;
a = a*a%mod;
b >>= ;
}
return ans;
} int main()
{
scanf("%d", &test);
while (test--)
{
int record = ;
scanf("%llu%llu%d", &a, &b, &n);
//寻找循环节
//也可以把f开成f[maxn][maxn*maxn+5]在test循环之前预处理
//如果预处理的话这里直接O(1)查询了,空间换时间吧
for (int i = ; i <= n*n + ; i++)
{
f[i] = (f[i-] + f[i-]) % n;
if (f[i] == && f[i-] == )
{
record = i-;//循环节长度
break;
}
}
printf("%d\n", n == ? : f[qpow(a%record, b, record)]);
}
}
再贴一个预处理的标程:
20ms
// UVa11582 Colossal Fibonacci Numbers!
// Rujia Liu
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = + ;
typedef unsigned long long ULL;
int f[maxn][maxn*], period[maxn];
int pow_mod(ULL a, ULL b, int n) {
if(!b) return ;
int k = pow_mod(a, b/, n);
k = k * k % n;
if(b % ) k = k * a % n;
return k;
}
int solve(ULL a, ULL b, int n) {
if(a == || n == ) return ; // attention!
int p = pow_mod(a % period[n], b, period[n]);
return f[n][p];
}
int main() {
for(int n = ; n <= ; n++) {
f[n][] = ; f[n][] = ;
for(int i = ; ; i++) {
f[n][i] = (f[n][i-] + f[n][i-]) % n;
if(f[n][i-] == && f[n][i] == ) {
period[n] = i - ;
break;
}
}
}
ULL a, b;
int n, T;
cin >> T;
while(T--) {
cin >> a >> b >> n;
cout << solve(a, b, n) << "\n";
}
return ;
}
至于为什么标程的二维f数组只开到maxn*6……我打了个表,最长是n=750时循环节3000~
UVa-11582:Colossal Fibonacci Numbers!(模算术)的更多相关文章
- UVA 11582 Colossal Fibonacci Numbers(数学)
Colossal Fibonacci Numbers 想先说下最近的状态吧,已经考完试了,这个暑假也应该是最后刷题的暑假了,打完今年acm就应该会退了,但是还什么都不会呢? +_+ 所以这个暑假,一定 ...
- UVa 11582 Colossal Fibonacci Numbers! 紫书
思路是按紫书上说的来. 参考了:https://blog.csdn.net/qwsin/article/details/51834161 的代码: #include <cstdio> # ...
- UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】
题目链接:Uva 11582 [vjudge] watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fil ...
- UVA 11582 Colossal Fibonacci Numbers!(循环节打表+幂取模)
题目链接:https://cn.vjudge.net/problem/UVA-11582 /* 问题 输入a,b,n(0<a,b<2^64(a and bwill not both be ...
- UVa #11582 Colossal Fibonacci Numbers!
巨大的斐波那契数 The i'th Fibonacci number f (i) is recursively defined in the following way: f (0) = 0 and ...
- UVa 11582 - Colossal Fibonacci Numbers!(数论)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 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]. 即计算大斐波那契数再取模. 一开始看到大斐波那契数,就想到了矩阵 ...
- UVA 11582 Colossal Fibonacci Numbers!【数学】
大一刚开始接触ACM就买了<算法竞赛入门经典>这本书,当时只能看懂前几章,而且题目也没做,粗鄙地以为这本书不适合自己.等到现在快大三了再回过头来看,发现刘老师还是很棒的! 扯远了... 题 ...
- UVA - 11582 Colossal Fibonacci Numbers! (巨大的斐波那契数!)
题意:输入两个非负整数a.b和正整数n(0<=a,b<264,1<=n<=1000),你的任务是计算f(ab)除以n的余数,f(0) = 0, f(1) = 1,且对于所有非负 ...
- Colossal Fibonacci Numbers! UVA 11582 寻找循环节
/** 题目:Colossal Fibonacci Numbers! UVA 11582 链接:https://vjudge.net/problem/UVA-11582 题意:f[0] = 1, f[ ...
随机推荐
- 异步模式模式Future(结合Callable可以获取线程返回结果)
submit 和 excute是有啥区别 如果有这样的需求: 多线程实现下载,提高效率. 不论是Thread类还是Runnable接口重写run方法,有个特点就是没有返回值~~~~~~ 我都主线程 如 ...
- <ZZ>linux yum命令详解
http://www.cnblogs.com/chuncn/archive/2010/10/17/1853915.html yum(全称为 Yellow dog Updater, Modified)是 ...
- CISCO-从路由器上下载IOS
准备工作:一台装有TFTP服务器的PC,一台带有IOS的路由器,并用网线连接上 设置路由器接口和计算机网卡的IP地址在同一网段,并且互相能ping通. 1,安装Cisco TFTP Server 2, ...
- python3 安装 opencv3 (win10,64bit)
python3只能安装opencv3 (python2安装opencv应该比python3安装的要简单,可参阅网上其他教程) 步骤参考http://stackoverflow.com/quest ...
- python 基础之第八天--字典相关
zx #####################创建字典###################################### In [11]: dict([('name','bob'),('a ...
- disablescroll
页面的设置 disablescroll:true(需要配合设置 enablePullDownRefresh:false ) 可以实现页面上下不能滑动 另一种实现方法: 设置页面的根元素 绝对定位, p ...
- bzoj4589
fwt 原理并不知道 nim游戏石子异或和=0后手赢 那么也就是求a[1]^a[2]^...^a[n]=0的方案数 这个和bzoj3992一样可以dp dp[i][j]表示前i个数异或和为j的方案数 ...
- 【转】Oracle Freelist和HWM原理及性能优化
文章转自:http://www.wzsky.net/html/Program/DataBase/74799.html 近期来,FreeList的重要作用逐渐为Oracle DBA所认识,网上也出现一些 ...
- jquery : eval() 解析json的注意
jquery eval解析JSON中的注意点介绍 来在:http://www.jb51.net/article/40842.htm 在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: ...
- CodeForces 1098F. Ж-function
题目简述:给定字符串$s[1 \dots n](n \leq 2 \times 10^5)$,以及$Q \leq 2 \times 10^5$个询问,每个询问有两个参数$1 \leq l \leq r ...