当前位置:网站首页>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;
}
边栏推荐
- 记一次phpcms9.6.3漏洞利用getshell到内网域控
- 倍福TwinCAT通过Emergency Scan快速检测物理连接和EtherCAT网络
- 深入解析 MySQL binlog
- postgis計算角度
- Goto statement to realize shutdown applet
- Beifu PLC passes MC_ Readparameter read configuration parameters of NC axis
- Splunk iowait 报警的解决
- 倍福TwinCAT3 NCI在NC轴界面中的基本配置和测试
- Unit practice experiment 8 - using cmstudio to design microprogram instructions based on basic model machine (1)
- 软件测试报告应该包含的内容?面试必问
猜你喜欢

EasyGBS如何解决对讲功能使用异常?

processing 随机生成线动画
![[esp32-c3][rt-thread] run RT-Thread BSP minimum system based on esp32c3](/img/4a/503240b332e3279047c438f1d9845e.png)
[esp32-c3][rt-thread] run RT-Thread BSP minimum system based on esp32c3

轻流完成与「DaoCloud Enterprise 云原生应用云平台」兼容性认证

【Spark】.scala文件在IDEA中几种图标的解释

Record a phpcms9.6.3 vulnerability to use the getshell to the intranet domain control

记一次phpcms9.6.3漏洞利用getshell到内网域控

文件远程同步、备份神器rsync

倍福PLC基于CX5130实现数据的断电保持

ES6:Map
随机推荐
. Net Maui performance improvement
goto语句实现关机小程序
power designer - 自定义注释按钮
This function has none of deterministic, no SQL solution
源码学习:AtomicInteger类代码内部逻辑
zoopeeper设置acl权限控制(只允许特定ip访问,加强安全)
微信小程序测试点总结
倍福TwinCAT3 NCI在NC轴界面中的基本配置和测试
A must for programmers, an artifact utools that can improve your work efficiency n times
机组实践实验8——使用CMStudio设计基于基本模型机微程序指令(1)
5+API,清除应用缓存
别乱用 FULL_CASE 和 PARALLEL_CASE
Lightflow completed the compatibility certification with "daocloud Enterprise Cloud native application cloud platform"
一个快速切换一个底层实现的思路分享
Is it safe for the head teacher to open a stock account and open an account for financial management?
PostGIS geographic function
postgis計算角度
计组实践实验9——使用CMStudio设计基于分段模型机微程序指令(2)
Learning Processing Zoog
倍福TwinCAT通过Emergency Scan快速检测物理连接和EtherCAT网络