当前位置:网站首页>上课作业(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
- 实战演练 Navicat 中英文模式切换
- 从零开始—仿牛客网讨论社区项目(一)
- 企业员工人事管理系统(数据库课设)
- AspNet.WebApi.Owin 自定义Token请求参数
- Selenium: mouse, keyboard events
- LeetCode 0149. Maximum number of points on a line
- Seleniu: Common operations on elements
- The solution to the inconsistency between the PaddleX deployment inference model and the GUI interface test results
- flinkcdc对mysql的date字段类型转化有什么解决思路么
猜你喜欢
随机推荐
小白的0基础教程SQL: 什么是SQL 01
Selenium: upload and download files
Challenge 52 days to memorize Peppa Pig (Day 01)
How JS works
Selenium: mouse, keyboard events
Robot_Framework:关键字
The solution to the inconsistency between the PaddleX deployment inference model and the GUI interface test results
史上超强最常用SQL语句大全
Selenium: browser operation
Jupyter shortcuts
LeetCode每日一题(309. Best Time to Buy and Sell Stock with Cooldown)
将CSV文件快速导入MySQL中
Selenium: element positioning
测试工具(四)Jenkins环境搭建与使用
MySQL-Data Definition Language-DDLdatebase define language
Robot_Framework:断言
uva12326
Selenium:操作JS
【FiddlerScript】利用FiddlerScript抓包保利威下载
湖仓一体电商项目(一):项目背景和架构介绍









