当前位置:网站首页>UVa11582 [快速幂]Colossal Fibonacci Numbers!
UVa11582 [快速幂]Colossal Fibonacci Numbers!
2022-06-26 12:39:00 【YJEthan】
题意:f[]为斐波拉契数列,要你求f[a^b]%n;
思路:利用斐波拉契的性质,找余数的循环节,若f[i]==1&&f[i-1]==0,则循环节为i-1;
求a^b用快速幂
注:UVAunsigned long long输入输出用 %llu
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
typedef unsigned long long ull;
ull qpow(ull a,ull b,ull cnt)
{
a=a%cnt;
ull ans=1;
while(b>0)
{
if(b%2==1)
ans=a*ans%cnt;
b/=2;
a=a*a%cnt;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ull a,b,n;
scanf("%llu%llu%llu",&a,&b,&n);
ull i;
vector<ull>fib;
fib.push_back(0);
fib.push_back(1);
ull cnt=0;
for(i=2;i<=n*n;i++)
{
fib.push_back((fib[i-1]+fib[i-2])%n);
cnt++;
if(fib[i]==1&&fib[i-1]==0)
break;
}
if(n!=1)
printf("%llu\n",fib[qpow(a,b,cnt)]);
else printf("0\n");
}
return 0;
}
边栏推荐
猜你喜欢
![Vivado 错误代码 [DRC PDCN-2721] 解决](/img/de/ce1a72f072254ae227fdcb307641a2.png)
Vivado 错误代码 [DRC PDCN-2721] 解决

黑马笔记---常用API

倍福CX5130换卡对已有的授权文件转移操作

倍福通过CTU和TON实现时间片大小和数量的控制

小白懒人专用-win10-win11一键安装版

processsing 函数random
RSS rendering of solo blog system failed

倍福TwinCAT3 NCI在NC轴界面中的基本配置和测试

Less than 40 lines of code to create a blocprovider

Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
随机推荐
Research and development practice of Kwai real-time data warehouse support system
. Net Maui performance improvement
班主任让开股票账户,在挖财理财开户安全吗?
小白懒人专用-win10-win11一键安装版
【网络是怎么连接的】第二章(下):一个网络包的接收
Adobe Acrobat prevents 30 security software from viewing PDF files or there are security risks
详细讲解C语言11(C语言系列)
.NET MAUI 性能提升
【网络是怎么连接的】第二章(上): 建立连接,传输数据,断开连接
processsing 函数random
Redis learning - 04 persistence
倍福TwinCAT通过Emergency Scan快速检测物理连接和EtherCAT网络
RSS rendering of solo blog system failed
Basic principle and application routine of Beifu PLC rotary cutting
复制多个excel然后命名不同的名字
zoopeeper设置acl权限控制(只允许特定ip访问,加强安全)
ES6:迭代器
心脏滴血漏洞(CVE-2014-0160)分析与防护
微信小程序测试点总结
guacamole安装