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(快速幂,求解)的更多相关文章

  1. Colossal Fibonacci Numbers! UVA 11582 寻找循环节

    /** 题目:Colossal Fibonacci Numbers! UVA 11582 链接:https://vjudge.net/problem/UVA-11582 题意:f[0] = 1, f[ ...

  2. Uva11582 Colossal Fibonacci Numbers!(同余模定理+快速幂)

    https://vjudge.net/problem/UVA-11582 首先明确,斐波那契数列在模c的前提下是有循环节的.而f[i] = f[i-1]+f[i-2](i>=2)所以只要有两个连 ...

  3. 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) ...

  4. HDU 3117 Fibonacci Numbers( 矩阵快速幂 + 数学推导 )

    链接:传送门 题意:给一个 n ,输出 Fibonacci 数列第 n 项,如果第 n 项的位数 >= 8 位则按照 前4位 + ... + 后4位的格式输出 思路: n < 40时位数不 ...

  5. UVA 11582 Colossal Fibonacci Numbers(数学)

    Colossal Fibonacci Numbers 想先说下最近的状态吧,已经考完试了,这个暑假也应该是最后刷题的暑假了,打完今年acm就应该会退了,但是还什么都不会呢? +_+ 所以这个暑假,一定 ...

  6. UVa 11582 Colossal Fibonacci Numbers! 紫书

    思路是按紫书上说的来. 参考了:https://blog.csdn.net/qwsin/article/details/51834161  的代码: #include <cstdio> # ...

  7. UVa-11582:Colossal Fibonacci Numbers!(模算术)

    这是个开心的题目,因为既可以自己翻译,代码又好写ヾ(๑╹◡╹)ノ" The i’th Fibonacci number f(i) is recursively defined in the f ...

  8. UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】

    题目链接:Uva 11582 [vjudge] watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fil ...

  9. UVa 11582 - Colossal Fibonacci Numbers!(数论)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. UVALive 4726 Average ——(斜率优化DP)

    这是第一次写斜率优化DP= =.具体的做法参照周源论文<浅谈数形结合思想在信息学竞赛中的应用>.这里仅提供一下AC的代码. 有两点值得注意:1.我这个队列的front和back都是闭区间的 ...

  2. jQuery源代码学习之十——动画Animate

    一.Animate模块的代码结构 // 定义了一些变量 tweeners = {}; function createFxNow() {} function createTween() {} funct ...

  3. mysql索引分类

    mysql索引分类 一.总结 一句话总结: 主键索引:设定为主键后数据库会自动建立索引,innodb为聚簇索引 单值索引:一个索引只包含单个列,一个表可以有多个单列索引:CREATE INDEX id ...

  4. 安装RabbitMQ管理插件失败

    运行 rabbitmq-plugins.bat enable rabbitmq_management后提示失败信息  是因为erlang和RabbitMQ版本冲突导致

  5. jmeter连接oracle数据库

    == 下载及添加这个文件到 这个路径下 连接设置: 测试连接 链接: https://pan.baidu.com/s/1W0YcVf4VLdsjnxv5umKngQ 提取码: np7j

  6. 进程 | 线程 | 当Linux多线程遭遇Linux多进程

    背景 本文并不是介绍Linux多进程多线程编程的科普文,如果希望系统学习Linux编程,可以看[<Unix环境高级编程>第3版] 本文是描述多进程多线程编程中遇到过的一个坑,并从内核角度分 ...

  7. 树莓派根分区扩展至整张sd卡

    第一步,安装raspi-config sudo apt-get install raspi-config 第二步,运行raspi-config sudo raspi-config 界面选择,Expan ...

  8. Qt编写数据可视化大屏界面电子看板10-改造QCustomPlot

    一.前言 为了抛弃对QChart的依赖,以及echart的依赖,(当然,后期也会做qchart的版本和echart的版本,尤其是echart的版本是肯定会做的,毕竟echart的效果牛逼的一塌糊涂,全 ...

  9. shell练习题集合

    1. 获取ip或MAC地址(方法不唯一) [root@cicd ~]# ip a| grep 'inet' | awk -F " +" '{print $3}'| awk -F & ...

  10. 为TMenuItem增加指针Data属性

    Delphi的有些组件中都包含.Data属性,比如TTreeNode,.Data属性可以认为是一个指针,可以指向任何类或者结构,方便后续操作. 但是TMenuItem没有.Data属性,下面介绍最简单 ...