杭電 1061 Rightmost Digit計算N^N次方的最後一位
題目:求N^N的最右面的那位一位,比如3*3*3=27--- 》7就是所求的
一般的,我們會寫出下面的程式碼:
考慮到,如果出示的n比較大,每次乘的數,可以變成n%10#include <iostream> using namespace std; int rightD(int n) { int s=1; for (int i=0;i<n;++i) { s=(s*n)%10; } return s; } int main() { int numcount=0;//數字的個數 int n=0; cin>>numcount; while (numcount-- > 0) { cin>>n; cout<<rightD(n)<<endl; } return 0; }
於是,我們得到下面的程式碼:(注意變數t)
int rightD(int n)
{
int s=1;
int t=n%10;
for (int i=0;i<n;++i)
{
s=(s*t)%10;
}
return s;
}
可是我們拿到評測系統中,還是Time Limit Exceeded
我們看for迴圈,每次都執行n次乘法。我們可以一次依次獲得n^1 ,n^2, n^4, n^8,所以我們可以很快得到n^n,時間複雜度o(log2 N)所以就有:
#include <iostream> using namespace std; int rightD(int n) { int ans=1; int tmp=n%10; while (n!=0) { if(n&1==1)//判斷最後一位 { ans=(ans*tmp)%10; } tmp = (tmp*tmp)%10; n = n>>1; } return ans; } int main() { int numcount=0;//數字的個數 int n=0; cin>>numcount; while (numcount-- > 0) { cin>>n; cout<<rightD(n)<<endl; } return 0; }
見下圖,第一行,表示1的位置
當然你也可以不使用函式,上圖2的位置,就是下面的程式碼:
#include<iostream> using namespace std; int main() { int a,ans,n=0; double dval = 0; int count=0; cin>>n; while(n--) { cin>>a; ans=1; count=a; a = a%10; while (count) { if (count&1==1) ans=(ans*a)%10; a=(a*a)%10; count>>=1; } cout<<ans<<endl; } return 0; }
到此可以說,該題目已經完結了,但是本題可以使用靜態表的方法,時間複雜度o(1)。
我們看一下n= (0 - 99),的結果:
0 1 4 7 6 5 6 3 6 9
0 1 6 3 6 5 6 7 4 9
0 1 4 7 6 5 6 3 6 9
0 1 6 3 6 5 6 7 4 9
0 1 4 7 6 5 6 3 6 9
0 1 6 3 6 5 6 7 4 9
0 1 4 7 6 5 6 3 6 9
0 1 6 3 6 5 6 7 4 9
0 1 4 7 6 5 6 3 6 9
0 1 6 3 6 5 6 7 4 9
所以有下面程式碼:
#include <iostream>
using namespace std;
unsigned int buf[20]={0,1,4,7,6,5,6,3,6,9,
0,1,6,3,6,5,6,7,4,9,
};
int main()
{
int numcount=0;
int n=0;
cin>>numcount;
while (numcount-- > 0)
{
cin>>n;
cout<<buf[n%20]<<endl;
}
return 0;
}
也是可以的,呵呵
相關推薦
杭電 1061 Rightmost Digit計算N^N次方的最後一位
題目:求N^N的最右面的那位一位,比如3*3*3=27--- 》7就是所求的 一般的,我們會寫出下面的程式碼: #include <iostream> using namespace
HDU 1061 Rightmost Digit(n的n次方的個位數)
題意很簡單,,就是求n的n次方的末位數字,常見做法有兩種: 1.快速冪取模 2.打表找規律 我沒有采用以上兩種做法,而是套了個整數超大次冪取模的模板,核心是尤拉降冪 //A^B %C=A^(
求斐波那契數列第N項的最後一位
RT,該怎麼求呢? 首先,你可能會想到,順序遍歷求解。 利用通項公式,可以得到斐波那契序列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34... 那每一項的最後一位就是: 0, 1, 1, 2, 3, 5, 8, 3, 1, 4... 這樣做的效
快速冪 HDU-1021 Fibonacci Again , HDU-1061 Rightmost Digit , HDU-2674 N!Again
1. Fibonacci Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s)
快速冪 HDU-1021 Fibonacci Again , HDU-1061 Rightmost Digit , HDU-2674 N!Again
target must sha pre end mod pos 級別 number 1. Fibonacci Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav
hdu-1061 Rightmost Digit
發現 們的 time logs for color div cnblogs digi 題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=1061 題目類型: 水題 題意概括: 求n的n次方的個位數。 解題思路: 因
HDU-1061-Rightmost Digit (快速冪模板)
Problem DescriptionGiven a positive integer N, you should output the most right digit of N^N. InputThe input contains several test cases. The first
數論 HDU-1061 Rightmost Digit
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
hdu 1066 Last non-zero Digit in N! 數學,求n!最後一位非零數
題意:求n!的最後一位非零數。(n很大,需要字元輸入) 題解: 我們發現n!末尾的0都是通過5和2想成得到的,我們將n分成20個數一組,最後剩下不足20個數。我們來討論【1-20】這20個數中含有5的數,只有5,10,15,20是5的倍數,我們還要找4個2來使之乘積得到10
6304 Chiaki Sequence Revisited[2018杭電多校聯賽第一場 G](找規律+位運算+逆元)
【題意】 給定一個序列a,定義a[1]=a[2]=1,a[n]=a[n-a[n-1]]+a[n-1-a[n-2]](n>=3),求該序列的前n項和是多少,結果對 1e9+7 取模 【輸入格式】 第一行為資料組數T(T<1e5),下面T行每行
計算身份證校驗碼(最後一位)
map git 校驗碼 meta ffi spa class ++ msg 在線預覽 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <
JS-計算身份證校驗碼(最後一位)
線上預覽 <!DOCTYPE html> <html lang="en"> <head> <title>Document</title> <style> #msg{ color: red; }
18位身份證號碼最後一位校驗碼的計算方法
http://dev.csdn.net/article/63/63451.shtm公民身份號碼是特徵組合碼,由十七位數字本體碼和一位校驗碼組成。排列順序從左至右依次為:六位數字地址碼,八位數字出生日期碼,三位數字順序碼和一位數字校驗碼。校驗方法:(1)十七位數字本體碼加權求和
【杭電-oj】 -1060-Leftmost Digit(輸出n的n次方最左邊數)
Problem Description Given a positive integer N, you should output the leftmost digit of N^N. Input The input contains several test
杭電2018多校第六場(2018 Multi-University Training Contest 6) 1012.Pinball(HDU 6373) -簡單的計算幾何+物理受力分析
info 簡單的 垂直 -- vector 分析 code space cti 6373.Pinball 物理受力分析題目。 畫的有點醜,通過受力分析,先求出θ角,為arctan(b/a),就是atan(b/a),然後將重力加速度分解為垂直斜面的和平行斜面的,垂直
杭電acm——2553(N皇后問題)
#include<iostream> using namespace std; #include<math.h> #include<cstring> int x[15],y[15],z[15],n,k; bool isRQ(int m) {
杭電oj-1042-N!
Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in one line, process to the end of file.
杭電ACM2001--計算兩點間的距離
計算兩點間的距離 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 291358 &nb
杭電 2002 計算球體積
計算球體積 Problem Description 根據輸入的半徑值,計算球的體積。 Input 輸入資料有多組,每組佔一行,每行包括一個實數,表示球的半徑。 Output 輸出對應的球的體積,對於每組輸入資料,輸出一行,計算結果保留三位小數。 Sample Input 1
杭電ACM2001計算兩點間的距離 &&&&杭電ACM2002計算球體積-----20140722
2001---- #include<stdio.h> #include<math.h> main() {double a,b,c,d,s; while(scanf("%lf %lf %lf %lf",&a,&b,&c,&am