当前位置:网站首页>上课作业(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;
}边栏推荐
- 企业员工人事管理系统(数据库课设)
- uva10825
- 头歌MySQL数据库实训答案 有目录
- matplotlib pyplot
- Solve the problem of page flicker caused by browser scroll bars
- 【FiddlerScript】利用FiddlerScript抓包保利威下载
- Hunan institute of technology in 2022 ACM training sixth week antithesis
- Using FiddlerScript caught poly FiddlerScript 】 【 download
- What should I do if the neural network cannot be trained?
- Seleniu:元素常用操作
猜你喜欢

Sound Signal Processing Fundamental Frequency Detection and Time-Frequency Analysis

WPF项目-初步了解数据绑定 binding

对于升级go1.18的goland问题

How JS works

crypto-js使用

Robot_Framework:断言

戴尔PowerEdge服务器R450 RAID配置步骤

MySQL-Data Operation-Group Query-Join Query-Subquery-Pagination Query-Joint Query

Selenium:操作Cookie

「面经分享」西北大学 | 字节 生活服务 | 一面二面三面 HR 面
随机推荐
对于升级go1.18的goland问题
matlab wind speed model wavelet filtering
Selenium:表单切换
Talk about the bugs in using for in to traverse the array in js
Why is the lightweight VsCode used more and more?Why eat my C drive 10G?How to Painlessly Clean VsCode Cache?Teach you how to lose weight for C drive
Selenium:操作Cookie
AspNet.WebApi.Owin custom Token request parameters
2022.7.27 Selected lectures on good topics
微信小程序接口调用凭证(获取token)auth.getAccessToken接口开发
小白的0基础教程SQL: 什么是SQL 01
图片更新之后Glide加载依旧是原来的图片问题
How JS works
【音视频】srs直播平台搭建
Selenium:浏览器操作
Qt Widget project loading example of qml
安装SQL Server详细教程
Selenium: form switching
After the image is updated, Glide loading is still the original image problem
ORACLE modify another user package (package)
matlab simulink 粒子群优化模糊pid控制的电机泵