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. 经过测试,feign只能通过@RequestBody传对象参数

    通过feign调用,使用ModelAttribute 注解,参数没法传到对应的server

  2. Win内核原理与实现学习笔记1-windows内核版本列表

  3. jacky解读麻省理工《计算机科学与Python编程导论》第1集

    文:@数据分析-jacky(朱元禄) (一)导言 本课程讲的中心思想就是五个字:计算机思维 Python只是辅助工具,是辅助大家理解计算机思维,仅此而已 急功近利是人性,适得其反是结果:我们看到有很多 ...

  4. 学完微型服务器(Tomcat)对其工作流程的理解,自己着手写个简单的tomcat

    学完微型服务器(Tomcat)对其工作流程的理解,自己着手写个简单的tomcat 2019-05-09   19:28:42 注:项目(MyEclipse)创建的时候选择:Web Service Pr ...

  5. Shiro RememberMe 1.2.4远程代码执行漏洞-详细分析

    本文首发于先知: https://xz.aliyun.com/t/6493 0x01.漏洞复现 环境配置 https://github.com/Medicean/VulApps/tree/master ...

  6. DSSM算法-计算文本相似度

    转载请注明出处: http://blog.csdn.net/u013074302/article/details/76422551 导语 在NLP领域,语义相似度的计算一直是个难题:搜索场景下quer ...

  7. 黑马vue---37-38、vue实例的生命周期

    黑马vue---37-38.vue实例的生命周期 一.总结 一句话总结: created:实例已经在内存中创建OK,此时 data 和 methods 已经创建OK,此时还没有开始 编译模板 moun ...

  8. 性能调优 | 如何通过性能调优突破 MySQL 数据库性能瓶颈?

    本文出自头条号老王谈运维,转载请说明出处. MySQL 数据库瓶颈对 DBA 程序员而言,是非常棘手的问题.要正确的优化SQL,我们需要快速定位能性的瓶颈点,也就是说快速找到我们SQL主要的开销在哪里 ...

  9. linux tftp配置 (Ubuntu18.04)

    安装tftp客户端和服务器sudo apt-get install tftp-hpa tftpd-hpa xinetdtftp-hpa是客户端tftpd-hpa是服务器 配置文件 sudo vim / ...

  10. Java动态修改运行环境

    1.pom.xml直接添加一下配置 <profiles> <profile> <id>dev</id> <properties> <a ...