ACM Step——Rightmost Digit

4 篇文章 0 订阅

原题链接:http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1&sectionid=2&problemid=12

Rightmost Digit

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10753 Accepted Submission(s): 2687
 

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.

 

Author

Ignatius.L

题解:这个题就是利用原数取模的方法简化,但是仅用取模(mod10)是超时的,小小的技巧是找到周期(可以理解他们周期都是4,这样的话代码简洁一点;具体分析的话,0,1,5,6一组周期是1,2,3,7一组周期是4,4,8,9一组周期是2)

源代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    long long int t,i;
    long long int n;
    long long int res;
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld",&n);
        res = n%10;
        if(res==0||res==1||res==5||res==6)
        printf("%d\n",res);
        else if(res==2)
        {
            if(n%4==1)
            printf("2\n");
            else if(n%4==2)
            printf("4\n");
            else if(n%4==3)
            printf("8\n");
            else
            printf("6\n");
        }
        else if(res==3)
        {
            if(n%4==1)
            printf("3\n");
            else if(n%4==2)
            printf("9\n");
            else if(n%4==3)
            printf("7\n");
            else
            printf("1\n");
        }
        else if(res==7)
        {
            if(n%4==1)
            printf("7\n");
            else if(n%4==2)
            printf("9\n");
            else if(n%4==3)
            printf("3\n");
            else
            printf("1\n");
        }
        else if(res==8)
        {
            if(n%4==1)
            printf("8\n");
            else if(n%4==2)
            printf("4\n");
            else if(n%4==3)
            printf("2\n");
            else
            printf("6\n");
        }
        else if(res==4)
        {
            if(n%2==1)
            printf("4\n");
            else
            printf("6\n");
        }
        else if(res==9)
        {
            if(n%2==1)
            printf("9\n");
            else
            printf("1\n");
        }
    }
    return 0;
}

(一)参考链接:https://blog.csdn.net/linan_141/article/details/46120871(这个是4个为一个周期)

知识点总结:

一般我们想到的求最后一位的方法是最后一位相乘,但如果数据很大的时候求n^n就会超时了,所以一般这种大数据的题就需要找规律了

我们发现2^1  2    2^2   4    2^3   8     2^4   6    2^5   2   2^6   4

                3^1  3    3^2   9    3^3   7     3^4   1    3^5   3   3^6   9

                4^1  4    4^2   6    4^3   4     4^4   6    4^5   4   4^6   6

                5^1  5    5^2   5    5^3   5     5^4   5    5^5   5   5^6   5

                归纳以下可得任何数的n次幂的个位都有一个周期是4

(二)参考链接:http://www.cnblogs.com/zafuacm/archive/2013/05/28/3104275.html(这个方法不是很明白,但是感觉很厉害的样子,不同的地方在于这个是求最左边的数)

HDU 1016(Leftmost Digit)N^N 最左边的数

Leftmost Digit

Problem Description

Given a positive integer N, you should output the leftmost 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

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        __int64 n;
        scanf("%I64d",&n);
        double tem=n*log10(1.0*n)-(__int64)(n*log10(1.0*n));
        double ans=pow(10.0,tem);
        printf("%d\n",(int)ans);
    }
    return 0;
}


感慨一下log10()非常好用
10^(a+b)=n^n  
n*log(n)=a+b    
b=n*log10(n)-(__int64)(n*log10(n))
10^b            从而表示第一位  
n=87455时,a=4,b=0.941784644.
10^a=10000.10^b=8.7455
从别人处引用而来
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值