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.

 
最先想到的是一个一个算,但是由于数据范围太大,O(n^2)的时间复杂度对于n = 10亿时,10亿^10亿次乘法运算实在是不能忍受的。因此下面的程序超时(Time Limit Exceeded)。
#include<stdio.h>
int main(void)
{
int cases, n, copy_n, result;
scanf("%d", &cases);
while(cases--)
{
scanf("%d", &n);
copy_n = n;
n = n%;
result = ;
while(copy_n--)
{
result = (result*n)%;
}
printf("%d\n", result);
} return ;
}

下面,快速幂一来就AC了:

#include<stdio.h>
int my_power(int m, int n); // 求m的n次方的尾数
int main(void)
{
int cases, n;
scanf("%d", &cases);
while(cases--)
{
scanf("%d", &n);
printf("%d\n", my_power(n, n));
} return ;
} int my_power(int m, int n)
{
m = m%;
if(n == )
return m;
if(n% == )
return ( my_power(m*m, n/) ) % ;
else
return ( my_power(m*m, n/)*m ) % ;
}

可以看到,快速幂的时间复杂度是O(logn),n = 10亿时,大约32次递归调用就能出结果,效率极大的提高了。

HDU_1061:Rightmost Digit的更多相关文章

  1. Rightmost Digit

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  2. HDU 1061 Rightmost Digit --- 快速幂取模

    HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...

  3. hdoj 1061 Rightmost Digit【快速幂求模】

    Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. HDOJ 1061 Rightmost Digit(循环问题)

    Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...

  5. Rightmost Digit(快速幂+数学知识OR位运算) 分类: 数学 2015-07-03 14:56 4人阅读 评论(0) 收藏

    C - Rightmost Digit Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...

  6. Rightmost Digit(快速幂)

    Description Given a positive integer N, you should output the most right digit of N^N.               ...

  7. <hdu - 1600 - 1601> Leftmost Digit && Rightmost Digit 数学方法求取大位数单位数字

    1060 - Leftmost Digit 1601 - Rightmost Digit 1060题意很简单,求n的n次方的值的最高位数,我们首先设一个数为a,则可以建立一个等式为n^n = a * ...

  8. 杭电 1061 Rightmost Digit计算N^N次方的最后一位

    Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...

  9. HDOJ 1061 Rightmost Digit

    找出数学规律 原题: Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

随机推荐

  1. 切换默认jdk版本执行

    Java代码 # cd /usr/bin # ln -s -f /usr/java/jdk1.6.0_24/jre/bin/java # ln -s -f /usr/java/jdk1.6.0_24/ ...

  2. Mybatis逆向工程文件标签的详细介绍:

    ?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUB ...

  3. 【xlwings1】Python-Excel 模块哪家强

    Python-Excel 模块哪家强?   0. 前言 从网页爬下来的大量数据需要清洗? 成堆的科学实验数据需要导入 Excel 进行分析? 有成堆的表格等待统计? 作为人生苦短的 Python 程序 ...

  4. CF605A Sorting Railway Cars

    传送门 题目大意 给出一个 1 到 n 的排列,每次操作可以将某个位置的数字移动到最前面或最后面,求将排列从小到大排序的最小操作次数 如:4 1 2 5 3 操作1:将5和3换一下变成4 1 2 3 ...

  5. SSM4-Linux上jdk、tomcat、zookeeper------tar zxvf的安装

    1.zookeeper .Zookeeper介绍 官方推荐使用zookeeper注册中心. 注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发 ...

  6. uva 10036

    10036 - Divisibility 额..直接复制不过来,只好叙述一下了...t组样例,n个数(1-10000),k(2-100)是要取余的数,然后给出n个数第一个数前不能加正负号,其他的数前面 ...

  7. PYTHON网络爬虫与信息提取[BeautifulSoup](单元四)

    1 简介 from bs4 import BeautifulSoup soup=BeautifulSoup(<p>data</p>,'html.parser') 2 基本元素 ...

  8. 开发者必看!探秘阿里云Hi购季开发者分会场:海量学习资源0元起!

    摘要: 开发者分会场致力于帮助开发者学习了解阿里云最新技术,为开发者设计全方位的技术成长与进阶之路. 2019阿里云云上Hi购季活动已经于2月25日正式开启,从已开放的活动页面来看,活动分为三个阶段: ...

  9. setStorage、getStorage、 removeStorage 封装

    // 本地存储 setStorage(name, data){ let dataType = typeof data; // json对象 if(dataType === 'object'){ win ...

  10. linux开发脚本自动部署及监控

    linux开发脚本自动部署及监控 开发脚本自动部署及监控一.编写脚本自动部署反向代理.web.nfs:要求:1.部署nginx反向代理三个web服务,调度算法使用加权轮询: #!/bin/sh ngx ...