当前位置:网站首页>上课作业(7)——#598. 取余运算(mod)
上课作业(7)——#598. 取余运算(mod)
2022-08-01 05:53:00 【xyc20120615】
目录
Description
输入 b,p,k 的值,求 b^p mod k 的值。其中 b,p,k*k 为长整形数。
Format
Input
输入 3 个整数 b,p,k。
Output
求 b^p\ mod~ kbp mod k 的值。
Samples
输入数据 1
2 10 9
输出数据 1
2^10 mod 9=7
Limitation
1s, 1024KiB for each test case.
方法一:
用pow函数再加个取余运算直接快速求出答案,结果40分,数据超了最大类型的限额(unsigned long long)。
错误程序:
#include<bits/stdc++.h>
using namespace std;
int main(){
long a,b,c;
unsigned long long ans;
cin>>a>>b>>c;
ans=pow(a,b);
ans%=c;
cout<<a<<'^'<<b<<" mod "<<c<<'='<<ans;
return 0;
}
方法二:
用快速幂并在while循环里穿插着取余运算的代码,因为还要输出原来的数据并且循环后数据会改变所以要先把输出的模板输出,最后再输出最终答案。
AC程序:
#include<bits/stdc++.h>
using namespace std;
long a,b,c;
unsigned long long ans=1;
int main(){
cin>>a>>b>>c;
cout<<a<<'^'<<b<<" mod "<<c<<'=';
while(b){
if(b%2==1){
ans*=a;
ans%=c;
}
b/=2;
a*=a;
a%=c;
}
ans%=c;
cout<<ans;
return 0;
}
边栏推荐
猜你喜欢
About making a progress bar for software initialization for Qt
Matlab simulink particle swarm optimization fuzzy pid control motor pump
混合型界面:对话式UI的未来
first unique character in characters
声音信号处理基频检测和时频分析
【音视频】srs直播平台搭建
2022/07/29 入职健海JustFE团队,我学到了高效开发(年中总结)
Robot_Framework: commonly used built-in keywords
MySQL-Data Definition Language-DDLdatebase define language
小心你的字典和样板代码
随机推荐
Sound Signal Processing Fundamental Frequency Detection and Time-Frequency Analysis
leetcode125 验证回文串
仿牛客网讨论社区项目—项目总结及项目常见面试题
小心你的字典和样板代码
Selenium: Element wait
太厉害了,终于有人能把文件上传漏洞讲的明明白白了
【MySQL必知必会】 表的优化 | 充分利用系统资源
MySQL-Data Operation-Group Query-Join Query-Subquery-Pagination Query-Joint Query
Solve the problem of page flicker caused by browser scroll bars
Selenium: element judgment
The solution to the inconsistency between the PaddleX deployment inference model and the GUI interface test results
NUMPY
【音视频】srs直播平台搭建
深度比较两个对象是否相同
Robot_Framework: Assertion
Selenium: mouse, keyboard events
Selenium: upload and download files
2022.7.26 模拟赛
Leetcode第 304 场周赛
图片更新之后Glide加载依旧是原来的图片问题