hdu1061Rightmost Digit(快速幂取余)
Rightmost Digit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 69614 Accepted Submission(s): 25945
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.
题意:给出n,要求算出n^n的最右边一位。
题解:普通方法进行幂运算再进行取余会超时,需要用到快速幂
#include<bits/stdc++.h>
using namespace std;
int mod_exp(int a, int b, int c) //快速幂取余a^b%c
{
int res, t;
res = % c;
t = a % c;
while (b)
{
if (b & )
{
res = res * t % c;
}
t = t * t % c;
b >>= ;
}
return res;
}
int main() {
int t;
while(~scanf("%d",&t))
{
while(t--)
{
int n;
scanf("%d",&n);
printf("%d\n",mod_exp(n,n,)%);
}
}
return ;
}
hdu1061Rightmost Digit(快速幂取余)的更多相关文章
- HDU 1061 Rightmost Digit --- 快速幂取模
HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...
- 洛谷 P1226 【模板】快速幂||取余运算
题目链接 https://www.luogu.org/problemnew/show/P1226 题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 ...
- LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)
链接: https://vjudge.net/problem/LightOJ-1282 题意: You are given two integers: n and k, your task is to ...
- 洛谷P1226 【模板】快速幂||取余运算
题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 输入格式: 三个整数b,p,k. 输出格式: 输出“b^p mod k=s” s为运算结果 S1: ...
- LuoguP1226 【模板】快速幂||取余运算
题目链接:https://www.luogu.org/problemnew/show/P1226 第一次学快速幂,将别人对快速幂原理的解释简要概括一下: 计算a^b时,直接乘的话计算次数为b,而快速幂 ...
- [每日一题2020.06.15]P1226 【模板】快速幂取余运算
我是题目 快速幂就是快速求 \(a^b\)的一种算法 快速幂 思想 : 比如我要求 \(6^9\) 首先将幂转化为二进制形式 : \[6^9 = 6^{1001} \tag{1} \] 可以得到 : ...
- 洛谷 P1226 【模板】快速幂||取余运算 题解
Analysis 快速幂模板,注意在最后输出时也要取模. 快速幂模板 inline ll ksm(ll x,ll y) { ll ans=; ) { ) { ans*=x; ans%=k; } x*= ...
- 【模板】快速幂&取余运算
输入\(b\),\(p\),\(k\)的值,求\(b^p mod k\)的值.其中\(b\),\(p\),\(k^2\)为长整型数. 1.普通做法 \(print\) \(pow(b,p)\)\(mo ...
- 【洛谷P1226 【模板】快速幂||取余运算】
题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 输入格式: 三个整数b,p,k. 输出格式: 输出“b^p mod k=s” s为运算结果 作为初 ...
随机推荐
- PHP中使用substr()截取字符串出现中文乱码问题该怎么办
一.使用mbstring扩展库的mb_substr()截取就不会出现乱码了. 可以用mb_substr()/mb_strcut()这个函数,mb_substr()/mb_strcut()的用法与sub ...
- 使用Vue构建单页应用一
一. 环境准备 1 安装Node.js 最好安装node.js-6.X.X 以上版本,https://nodejs.org/en/ 我使用的是 v6.3.1 Current 版本.Node. ...
- Shell脚本之Crontab的格式
Crontab的格式 第1列分钟1-59第2列小时1-23(0表示子夜)第3列日1-31第4列月1-12第5列星期0-6(0表示星期天)第6列要运行的命令 下面是crontab的格式:分 时 日 ...
- 谨慎修改Oracle数据库字符集(UTF8<->ZHS16GBK)
Preface Today,I'm gonna say something what is related with the character set in Oracle datab ...
- webstorm如何支持markdown
首先安装markdown 插件 第一个即可.
- #leetcode刷题之路15-三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- 【POJ 3368】Frequent values(RMQ)
Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...
- 开源框架:DBUtils使用详解
一.先熟悉DBUtils的API: 简介:DbUtils是一个为简化JDBC操作的小类库. (一)整个dbutils总共才3个包: 1.包org.apache.commons.dbutils 接 ...
- Python实现音乐的剪辑
一.读取音频文件 from scipy.io import wavfile import numpy as np like = wavfile.read('./嘤嘤嘤.wav') print (lik ...
- telent connection refused
1.问题场景 Centos7 做flume案例时,telnet hadoop-senior03.itguigu.com 44444 总是Connection redused, Trying 192.1 ...