当前位置:网站首页>上课作业(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;
}边栏推荐
猜你喜欢

matlab wind speed model wavelet filtering

The solution to the inconsistency between the PaddleX deployment inference model and the GUI interface test results

牛客多校2022第四场A,H,K,N

可视化全链路日志追踪

LeetCode 0149. Maximum number of points on a line

【MySQL必知必会】 表的优化 | 充分利用系统资源

Malicious attacks on mobile applications surge by 500%

Dell PowerEdge Server R450 RAID Configuration Steps

混合型界面:对话式UI的未来

WebSocket implements chat function
随机推荐
Selenium:元素定位
Selenium: element positioning
小白的0基础教程SQL: 什么是SQL 01
用位运算为你的程序加速
How JS works
轻量级的VsCode为何越用越大?为什么吃了我C盘10G?如何无痛清理VsCode缓存?手把手教你为C盘瘦身
LeetCode每日一题(309. Best Time to Buy and Sell Stock with Cooldown)
2022年湖南工学院ACM集训第六次周测题解
Selenium:元素等待
[Translation] Securing cloud-native communications: From ingress to service mesh and beyond
Seleniu: Common operations on elements
将CSV文件快速导入MySQL中
NUMPY
Qt Widget project loading example of qml
Hunan institute of technology in 2022 ACM training sixth week antithesis
解决浏览器滚动条导致的页面闪烁问题
Matlab simulink particle swarm optimization fuzzy pid control motor pump
The sword refers to Offer 68 - I. Nearest Common Ancestor of Binary Search Trees
Does flinkcdc have any solution for mysql's date field type conversion?
Robot growth in China