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[ ...
随机推荐
- Double.valueOf()与Double.parseDouble()两者的区别
写代码用到这两个方法,不知道有什么区别,看一下源码: Double.parseDouble(String str) public static double parseDouble(String s) ...
- 创建一个catkin工作空间
先确定自己的环境变量是否设置正确 export | grep ROS 若出现如下的,说明是正确的 declare -x ROSLISP_PACKAGE_DIRECTORIES="" ...
- UVA10518 How Many Calls? —— 矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-10518 题解: 问:求斐波那契数f[n]的时候调用了多少次f[n] = f[n-1] + f[n-2],没有记忆化,一直递归 ...
- LightOJ1341 Aladdin and the Flying Carpet —— 唯一分解定理
题目链接:https://vjudge.net/problem/LightOJ-1341 1341 - Aladdin and the Flying Carpet PDF (English) S ...
- Ubuntu安装基础教程
作者:TeliuTe 来源:基础教程网 二十三.安装Ubuntu14.04 返回目录 下一课 14.04 版安装与前面版本类似,学习中遇到不清楚的地方,可以参考一下前面的内容,操作中注意细心,下面来看 ...
- linux应用之vsftp服务的安装及配置(centos)
1.centos中vsftp服务的安装 方法1:rpm方式 #rpm –ivh vsftpd-2.0.5-10.el5.i386.rpm 安装rpm程序包(网上下载的rpm包) 方法2:yum方式 ...
- D3.JS V4 绘制中国地图
参考:http://bl.ocks.org/almccon/fe445f1d6b177fd0946800a48aa59c71 http://blog.csdn.net/lzhlzz/article/d ...
- CS231n 2016 通关 第四章-反向传播与神经网络(第一部分)
在上次的分享中,介绍了模型建立与使用梯度下降法优化参数.梯度校验,以及一些超参数的经验. 本节课的主要内容: 1==链式法则 2==深度学习框架中链式法则 3==全连接神经网络 =========== ...
- 用于生成交易统计时间戳(常配合echarts走势图使用)
<?php //获取交易统计时间戳 时间段内每小时 public function getPayCountTimeHours($start_date,$end_date){ $data = ar ...
- 深度学习之Batch归一化
前言 以下内容是个人学习之后的感悟,转载请注明出处~ Batch归一化 在神经网络中,我们常常会遇到梯度消失的情况,比如下图中的sigmod激活函数,当离零点很远时,梯度基本为0 ...