ACM Step——Rightmost Digit

小编 2026-06-19 阅读:164 评论:0
原题链接:http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1&sectionid=2&problemid=12...

原题链接: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
从别人处引用而来
版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表