Colossal Fibonacci Numbers! UVA - 11582(快速幂,求解)
Problem Description
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 < 2 64 (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 ƒ(ab ) upon division by n.
Sample Input
3 1 2
2 3
744073709 184467955
Sample Output
21
题目大意:
给出a,b,n,让你计算f(a^b)%n,f(n)=f(n-1)+f(n-2);
因为是%n所以余数最多n*n种,于是我们就可以用快速幂求出是在数列中是第几个数,然后代入f[]
输出就可以了~
操作代码如下:(注:n&1为真则n为奇数)
#include<iostream>
#include<cstdio>
using namespace std;
#define ll unsigned long long
const int maxx=;
int f[maxx*maxx];
int pow(ll m,ll n,int k)
{
int b=;
while(n>)
{
if(n&)
{
b=(b*m)%k;
}
n=n>>;
m=(m*m)%k;
}
return b;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ll a,b;
int n,m;
scanf("%llu%llu%d",&a,&b,&n);
if(n==||a==)
printf("0\n");
else
{
f[]=;
f[]=;
m=n*n+;
int s;
for(int i=; i<=m; i++)
{
f[i]=(f[i-]+f[i-])%n;
if(f[i]==f[]&&f[i-]==f[])
{
s=i-;
break;
}
}
int k=pow(a%s,b,s);
printf("%d\n",f[k]);
}
}
return ;
}
Colossal Fibonacci Numbers! UVA - 11582(快速幂,求解)的更多相关文章
- Colossal Fibonacci Numbers! UVA 11582 寻找循环节
/** 题目:Colossal Fibonacci Numbers! UVA 11582 链接:https://vjudge.net/problem/UVA-11582 题意:f[0] = 1, f[ ...
- Uva11582 Colossal Fibonacci Numbers!(同余模定理+快速幂)
https://vjudge.net/problem/UVA-11582 首先明确,斐波那契数列在模c的前提下是有循环节的.而f[i] = f[i-1]+f[i-2](i>=2)所以只要有两个连 ...
- UVa 11582 (快速幂取模) Colossal Fibonacci Numbers!
题意: 斐波那契数列f(0) = 0, f(1) = 1, f(n+2) = f(n+1) + f(n) (n ≥ 0) 输入a.b.n,求f(ab)%n 分析: 构造一个新数列F(i) = f(i) ...
- HDU 3117 Fibonacci Numbers( 矩阵快速幂 + 数学推导 )
链接:传送门 题意:给一个 n ,输出 Fibonacci 数列第 n 项,如果第 n 项的位数 >= 8 位则按照 前4位 + ... + 后4位的格式输出 思路: n < 40时位数不 ...
- 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!(模算术)
这是个开心的题目,因为既可以自己翻译,代码又好写ヾ(๑╹◡╹)ノ" The i’th Fibonacci number f(i) is recursively defined in the f ...
- UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】
题目链接:Uva 11582 [vjudge] watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fil ...
- UVa 11582 - Colossal Fibonacci Numbers!(数论)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- CF1168C And Reachability 【构造,dp】
题目链接:洛谷 题目描述:给出$n$个数$a_i$,若$i<j$且$a_i & a_j>0$,则$i$到$j$连一条有向边,$q$次询问,询问从$l$开始是否能到达$r$. 数据范 ...
- harbor 私有镜像仓库使用
使用harbor私有镜像仓库 登录镜像仓库reg.lvusyy.com docker login reg.lvusyy.com [centos@k8smaster ~]$ sudo docker lo ...
- 使用Excel拼凑SQL语句
快速将一列多行数据合并到一个单元格 EXCEL如何快速将一列多行数据合并到一个单元格,并加分隔符?这是批量处理由一线业务员统计的数据时的常用方法,尤其是当一列数据是wher ...
- JAVA基于File的基本的增删改查
直接上代码 public class TestFile { /** * 创建目录 * @param filename 路径 */ public static void createFile(Strin ...
- spring项目启动错误——java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
最近在搭spring项目框架的时候,遇到一个很伤的问题,翻了很多帖,都报告说什么少spring-context包啊之类的,但实际上spring的那些依赖我根本没漏,下面是我的pom: <depe ...
- SpringMVC之ajax与表单 Post 数据提交差异小结
最近在写一个富文本框的后台数据服务的时候遇到一些关于 ajax 提交与 表单提交的比较特殊的案例,这里拿来跟大家分享,希望能让大家有所启发. 1. 首先是常见表单提交在SpringMVC的控制器中的代 ...
- 二、navicat连接本地数据库以及远程数据库
本地连接 1.打开navicat 2.连接 最后点击确定就连接成功了: 远程数据库 和上面一样.....
- Windows Bat 之For 循环
Windows Bat 之For 循环 1. For 循环基本用法. 1.1 格式 在cmd窗口中: FOR %variable IN (set) DO command [command-pa ...
- Oracle查询表和字段
查看表字段.类型.注释 SELECT A.COLUMN_NAME,B.comments,A.DATA_TYPE FROM USER_TAB_COLUMNS A LEFT JOIN user_col_c ...
- spark入门备忘---1
import org.apache.spark.{SparkContext, SparkConf} import scala.math.random /** * 利用spark进行圆周率的计算 */ ...