当前位置:网站首页>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;
}
边栏推荐
- 轻流完成与「DaoCloud Enterprise 云原生应用云平台」兼容性认证
- ES6:迭代器
- BigInt:处理大数字(任意长度的整数)
- Unit practice experiment 8 - using cmstudio to design microprogram instructions based on basic model machine (1)
- 【网络是怎么连接的】第二章(中):一个网络包的发出
- Sharing ideas for a quick switch to an underlying implementation
- Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
- solo 博客系统的 rss 渲染失败
- Software testing - concept
- Beifu PLC passes MC_ Readparameter read configuration parameters of NC axis
猜你喜欢

黑马笔记---常用API

使用SSH密钥对登陆服务器

倍福PLC选型--如何看电机是多圈绝对值还是单圈绝对值编码器

倍福PLC实现绝对值编码器原点断电保持---bias的使用

A must for programmers, an artifact utools that can improve your work efficiency n times

倍福将EtherCAT模块分到多个同步单元运行--Sync Units的使用

Redis learning - 05 node JS client operation redis and pipeline pipeline

详细讲解C语言11(C语言系列)

【网络是怎么连接的】第一章:浏览器生成消息

Websocket and socket IO case practice
随机推荐
el-form-item 包含两个input, 校验这两个input
File remote synchronization and backup artifact Rsync
[esp32-C3][RT-THREAD] 基于ESP32C3运行RT-THREAD bsp最小系统
面试题积累
Electron official docs series: Distribution
This function has none of deterministic, no SQL solution
processing 随机生成线动画
倍福EtherCAT Xml描述文件更新和下载
Less than 40 lines of code to create a blocprovider
A must for programmers, an artifact utools that can improve your work efficiency n times
国标GB28181协议EasyGBS视频平台TCP主动模式拉流异常情况修复
Research and development practice of Kwai real-time data warehouse support system
[BSidesCF 2019]Kookie 1
QT .pri 的建立与使用
postgis计算角度
sql 将数据表b字段值赋值到数据表a中某一列
Tiger DAO VC产品正式上线,Seektiger生态的有力补充
Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
美学心得(第二百三十八集) 罗国正
ES6模块