1. 程式人生 > >HDU 1061 N ^ N 的個位數

HDU 1061 N ^ N 的個位數

最原始的方法超時,Wa了幾次,原因是 0^0 = 0 ?

#include <iostream>
using namespace std;

int  main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n; 
		cin >> n;
		int a = n % 10;
		int b = a;
		int mod = 0;
		for(int i = 1; i < n; i++)
		{
			b = (b * a) % 10;
			if(b == a)
			{
				mod = i;
				break;
			}
		}
		if(1 == mod)
		{
			cout << a << endl;
			continue;
		}
		if(mod != 0)
			n =  n % mod + mod;
		b = a;
		for(int i = 1; i < n; i++)
				b = (b * a) % 10;
		cout << b << endl;
	}
	return 0;
}


打表方法肯定是最快,附上打表程式碼:

#include<stdio.h>  
int main()  
{  
 int n;  
 int a[10][4] = {{0},{1},{6,2,4,8},{1,3,9,7},{6,4},{5},{6},{1,7,9,3},{6,8,4,2},{1,9}},d,num;  
 scanf("%d",&num);  
 while(num--)  
 {  
    scanf("%ld",&n);  
    d = n % 10;  
    if(d == 0||d == 1||d == 5||d == 6)  
         printf("%d\n",d);  
    else if(d == 4||d == 9)   
         printf("%d\n",a[d][n % 2]);  
    else if(d == 2||d == 3||d == 7||d == 8)  
         printf("%d\n",a[d][n % 4]);  
 }  
 return 0;  
}  


相關推薦

HDU 1061 N ^ N個位數

最原始的方法超時,Wa了幾次,原因是 0^0 = 0 ? #include <iostream> using namespace std; int main() { int t; cin >> t; while(t--) { int

HDU 1061 Rightmost Digit(nn次方的個位數

題意很簡單,,就是求n的n次方的末位數字,常見做法有兩種: 1.快速冪取模 2.打表找規律 我沒有采用以上兩種做法,而是套了個整數超大次冪取模的模板,核心是尤拉降冪 //A^B %C=A^(

快速冪 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 1042 N

描述 spa 之前 font pan return task ces pro 題目來源:http://acm.hdu.edu.cn/showproblem.php?pid=1042Problem DescriptionGiven an integer N(0 ≤ N ≤ 1

hdu-1061 Rightmost Digit

發現 們的 time logs for color div cnblogs digi 題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=1061 題目類型: 水題 題意概括: 求n的n次方的個位數。 解題思路: 因

HDU 1042 N!

careful gree pac i++ shining you weight tle accep N! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Other

hdu 1061

while sam 遞歸 turn return several 快速冪 ould line 1、鏈接 http://acm.hdu.edu.cn/showproblem.php?pid=1061 2、題目 Problem DescriptionGiven a pos

HDU 2553 N皇後問題(DFS)

== using () 回溯 ble http 鏈接 clu acm 鏈接 : Here! 思路 : 最經典的DFS問題, 思路搜索每一行 $x$, 看看有那些列能合理放置, $(x, y)$ 如果是合法點則放置, 然後搜索下一行, 如果已經合法放置了 $N$ 個點,

HDU.3571.N-dimensional Sphere(高斯消元 模線性方程組)

con 需要 etc oid 如果 git inline cpp 由於 題目鏈接 高斯消元詳解 /* $Description$ 在n維空間中給定n+1個點,求一個點使得這個點到所有點的距離都為R(R不給出)。點的任一坐標|xi|<=1e17. $Solution$

HDU 2553 N皇後問題 (DFS_回溯)

class blog -a mono color popu cstring str == Problem Description 在N*N的方格棋盤放置了N個皇

hdu-2553 N皇後問題

cst check clas sum for dfs esp tput sin 在N*N的方格棋盤放置了N個皇後,使得它們不相互攻擊(即任意2個皇後不允許處在同一排,同一列,也不允許處在與棋盤邊框成45角的斜線上。 你的任務是,對於給定的N,求出有多少種合法的放置方法。

HDU - 1061-快速冪簽到題

快速冪百度百科:快速冪就是快速算底數的n次冪。其時間複雜度為 O(log₂N), 與樸素的O(N)相比效率有了極大的提高。 HDU - 1061 程式碼實現如下: import java.util.Scanner; public class Main { public static vo

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-2553 N皇后問題

Problem Description 在N*N的方格棋盤放置了N個皇后,使得它們不相互攻擊(即任意2個皇后不允許處在同一排,同一列,也不允許處在與棋盤邊框成45角的斜線上。 你的任務是,對於給定的N,求出有多少種合法的放置方法。 Input 共有若干行,每行一個正整數

大量級階乘 - HDU-2674 N!Again

 N!Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6338  

HDU 2674 N!Again

Problem Description WhereIsHeroFrom:             Zty, what are you doing ? Zty:                                     I want to calculate N

HDU 5601 N*M bulbs (找規律)

#include<bits/stdc++.h> using namespace std; #define debug puts("YES"); #define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++) #def

數論 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

第二次周賽G題(HDU-1061

問題連結:https://vjudge.net/problem/HDU-1061 問題簡述:求n的n次方的個位數。 Get:可以用快速冪,也可以找規律。 方法一:找規律 相關連結:https://blog.csdn.net/feynman1999/article/details/