HDU 1061 [Rightmost Digit] 数学方法

Posted 哇咔咔咔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1061 [Rightmost Digit] 数学方法相关的知识,希望对你有一定的参考价值。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1061

题目大意:求N^N的个位数

关键思想:对1个N来说,乘方时末尾会循环。对所有N来说,结果以20为周期。

代码如下(第一个思想):

//cnt为循环节长度
#include <iostream>
#include <cmath>
using namespace std;

int main(){
    int T;
    long long i,N,cnt;
    cin>>T;
    while(T--){
        cin>>N;
        if(N%10==0){
            cout<<0<<endl;
            continue;
        }
        long long t[100];
        bool v[10]={false};
        cnt=1,i=N;
        i%=10;
        while(!v[i]){
            t[cnt++]=i;
            v[i]=true;
            i*=N;
            i%=10;
        }
        if(N%(cnt-1)==0)cout<<t[cnt-1]<<endl;
        else cout<<t[N%(cnt-1)]<<endl;
    }
    return 0;
}

 (第二种方法:打表)

#include <stdio.h>
#include <cmath>
int num[20] = {0,1,4,7,6,5,6,3,6,9,0,1,6,3,6,5,6,7,4,9};

int main()
{
	int C;
	int n;
	scanf("%d", &C);
	while(C--)
	{
		scanf("%d", &n);
		printf("%d\n", num[n % 20]);
	}
}

  

以上是关于HDU 1061 [Rightmost Digit] 数学方法的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1061 Rightmost Digit(找规律)

hdu 1061 Rightmost Digit

HDU 1061 [Rightmost Digit] 数学方法

hdu-1061 Rightmost Digit

HDU-1061-Rightmost Digit

hdu1061Rightmost Digit(快速幂取余)