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[ ...
随机推荐
- Android系统DHCP问题【转】
本文转载自:http://blog.csdn.net/tankai19880619/article/details/42972551 一.现象 12小时压测wifi连接后,发现网络连接中断:相关log ...
- 卡特兰数 HDU2067 & HDU4165 & HDU1134
题目链接:https://vjudge.net/problem/HDU-2067 小兔的棋盘 Time Limit: 1000/1000 MS (Java/Others) Memory Limi ...
- 使用MongoDB.NET 2.2.4驱动版本对 Mongodb3.3数据库中GridFS增删改查
Program.cs代码如下: internal class Program { private static void Main(string[] args) { GridFSHelper help ...
- plsql导入csv数据,未响应,invalid identifier
问题分析: 1.确保cvs字段名与表字段名一致,不要有空格 2.cvs字段对应表字段的大写,确保表字段都是大写 3.如果字段能对应上,plsql会自动识别出来
- 树堆(Treap)
平衡树 简介: 平衡二叉树(Balanced Binary Tree)具有以下性质:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树.平衡二叉树的常用实现方 ...
- Android设备管理器 DevicePolicyManager
设备管理器有个特点,你注册了之后如果不解除注册就会难以卸载带有设备管理器的应用,目前4.3版本仍未提示用户如何卸载,maybe later. 在「设定-安全」你可以看见「设备管理器」,它提供一些高级功 ...
- vue 常用的表单验证,包括手机号码,固定电话和身份证...
<template> <div> <pl-content-box> <pl-page-nav :show-previous=true></pl-p ...
- AQS与重入锁ReetrantLock原理
一.AQS原理 AQS(AbstractQueuedSynchronizer)队列同步器是用来构建锁.同步组件的基础框架. AQS内部通过一个volatile int类型的成员变量state控制同步状 ...
- Linux设备驱动之Kobject、Kset
作者:lizuobin(也是我们兼职的论坛答疑助手) 原文: https://blog.csdn.net/lizuobin2/article/details/51523693 纠结又纠结,虽然看了一些 ...
- bzoj4004
线性基 构成线性基的个数是定的,所以我们对价值进行贪心就行了,根据拟阵那套理论,我们排个序,然后能塞进去就塞,这样就求出最小值了. 思维江化,只要是多维向量都能用线性基搞. #include<b ...