Rightmost Digit

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

 

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.
 普通的算法会超时,数据量比较大,到1亿了
优化算法,分治思想.
例如:( 5^5)%10  = ((5^2)^2*5)%10;
                             这个过程中要取余 !
还有一种思想利用乘方尾数周期性的关系!
 
#include <stdio.h>
#include <string.h> int mod(int a, int n)
{
long long x;
long long ans;
if(n==1||n==0)
return n==0?1:a; x = mod(a, n/2);
ans = x * x % 10; if(n%2==1)
ans=ans*a%10 ;
return (int)ans;
} int main()
{
int t;
int a;
int dd;
scanf("%d", &t);
while(t--)
{
scanf("%d", &a );
dd = mod(a, a);
printf("%d\n", dd );
}
return 0;
}

HDU Rightmost Digit的更多相关文章

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

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

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

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

  3. 题解报告:hdu 1061 Rightmost Digit(快速幂取模)

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

  4. 快速幂 HDU 1061 Rightmost Digit *

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

  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

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

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

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

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

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

  9. Rightmost Digit(快速幂)

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

随机推荐

  1. 从头认识Spring-1.15 对SpEl的值的操作(1)-数值运算

    这一章节我们来讨论一下对SpEl的值的运算. 1.domain 烤炉类:(不变) package com.raylee.my_new_spring.my_new_spring.ch01.topic_1 ...

  2. centos7 firefox 安装flash

    在官网下载flash的tar包 https://get.adobe.com/flashplayer/?spm=a2h0j.8191423.movie_player.5~5~5~8~A 在下载tar包的 ...

  3. DDD架构Sample

    http://dddsamplenet.codeplex.com/SourceControl/latest#DDDSample-Vanilla/Application/IBookingService. ...

  4. mock测试类的时候,添加@InjectMocks

    1.在单元测试某个类的时候,引入该类的时,添加注解@InjectMocks 2.该类的变量,需要添加注解:@Mock 3.类中需要第三方协作者时,通常会用到get和set方法注入.通过spring框架 ...

  5. 云中应用性能管理(APM)的下一步

    Michael Kopp是Compu-ware公司卓越APM中心的一名技术分析师,他做过10多年的Java/JEE领域的设计师和开发员.另外,Kopp还专攻虚拟和云的大规模生产部署的结构和性能. ? ...

  6. Currency Exchange - poj 1860

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22111   Accepted: 7986 Description Seve ...

  7. 64位win7环境eclipse集成svn后出现Failed to load JavaHL Library的解决办法

    http://lushuifa.iteye.com/blog/2038000

  8. TortoiseSVN与VisualSVN Server搭建SVN版本控制系统(转)

    地址:http://www.cnblogs.com/xing901022/p/4399382.html

  9. Mac下python连接mysql数据库

    一.下载Mysql官方connector驱动 地址:https://dev.mysql.com/downloads/connector/python/ 根据提示安装.dmg文件即可. 二.验证是否安装 ...

  10. wordpress 获取分类ID,分类标题,分类描述,分类链接url函数

    get_cat_ID()    根据分类名称获取分类ID   ///// get_cat_name()    根据分类ID获取分类名称 用法:<?phpget_cat_ID( $cat_name ...