Rightmost Digit(快速幂+同余定理)
来源:互联网 发布:生意参谋淘宝版下载 编辑:程序博客网 时间:2024/06/05 21:16
题目链接
Problem Description
Given a positive integer N, you should output the most right digit of N^N.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
Output
For each test case, you should output the rightmost digit of N^N.
Sample Input
2
3
4
Sample Output
7
6
Hint
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.
In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
#include<iostream>using namespace std;int f(int a,int b){ int ans=1; a = a % 10; while(b > 0){ if(b & 1) /** 1.b & 1取b二进制的最低位,判断和1是否相同,相同返回1,否则返回0,可用于判断奇偶 2.b>>1//把b的二进制右移一位,即去掉其二进制位的最低位 */ ans = (ans * a) % 10; b = b >> 1; a = (a * a)%10; } return ans;}int main(){ int n,t; int result; cin>>t; while(t--){ cin>>n; result=f(n,n);//计算n的b次方 cout<<result<<endl; } return 0;}
快速幂+同余定理详细解析
阅读全文
0 0
- Rightmost Digit(快速幂+同余定理)
- 快速幂+同余定理
- 同余定理 + 快速幂
- HDU 1061 Rightmost Digit (四则运算求余,快速幂)
- 快速幂及同余定理
- 快速排幂-----Digit Rightmost
- HDU Rightmost Digit (快速幂)
- Rightmost Digit(快速幂)
- poj 1995 Raising Modulo Numbers(同余定理+快速幂)
- POJ 1995 Raising Modulo Numbers 【快速幂&&同余定理】
- POJ 1995 Raising Modulo Numbers(快速幂,同余定理)
- NYOJ88汉诺塔(一)(快速幂&同余定理)
- hdoj5363Key Set(快速幂+同余定理)
- POJ -3641Pseudoprime numbers(快速幂+同余定理)
- poj1995Raising Modulo Numbers(快速幂+同余定理)
- hdoj2035 人见人爱A^B(快速幂+同余定理)
- POJ 1995 Raising Modulo Numbers 【快速幂+同余定理】
- poj3641Pseudoprime numbers(快速幂+同余定理)
- 【leetcode】680. Valid Palindrome II(Python & C++)
- 一个事物相关的列子
- PL/SQL
- Jzoj1279 解题
- ndk笔记
- Rightmost Digit(快速幂+同余定理)
- windows安装pip和easy_install
- iOS开发,如何利用Platinum库开发一个DLNA功能
- ireport 动态修改标题以及表头,本人亲试,绝对靠谱!!!
- 对代码生成器的看法
- SVD算法以及改进后的LFM模型在推荐系统的应用
- Class String 方法学习
- 算法小程序 字符串循环右移
- Centos7 mysql主从复制