Rightmost Digit

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

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
 
快速幂的原理其实是同余定理 (a*b)%c=((a%c)*(a%c))%c的实现过程
#include<stdio.h>
#include<string.h>
int f(int x)
{
int ans=1;
int y=x;
x=x%10;
while(y)
{
if(y&1)
ans=(ans*x)%10;
y/=2;
x=(x*x)%10;
}
return ans;
}
int main()
{
int n,m,j,i,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%d\n",f(n));
}
return 0;
}

  

hdoj 1061 Rightmost Digit【快速幂求模】的更多相关文章

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

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

  2. hdu 2065 "红色病毒"问题(快速幂求模)

    n=1  --> ans = 2 = 1*2 = 2^0(2^0+1) n=2  -->  ans = 6 = 2*3 = 2^1(2^1+1) n=3  -->  ans = 20 ...

  3. 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 ...

  4. NYOJ-676小明的求助,快速幂求模,快速幂核心代码;

    小明的求助 时间限制:2000 ms  |  内存限制:65535 KB 难度:2 描述 小明对数学很有兴趣,今天老师出了道作业题,让他求整数N的后M位,他瞬间感觉老师在作弄他,因为这是so easy ...

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

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

  6. HDOJ 1061 Rightmost Digit

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

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

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

  8. HDU 1061.Rightmost Digit-规律题 or 快速幂取模

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

  9. 九度OJ 1085 求root(N, k) -- 二分求幂及快速幂取模

    题目地址:http://ac.jobdu.com/problem.php?pid=1085 题目描述: N<k时,root(N,k) = N,否则,root(N,k) = root(N',k). ...

随机推荐

  1. 小知识 Vector的枚举 和foreach的用法

    package com.java.c.votetor.www; import java.util.Enumeration;import java.util.Iterator;import java.u ...

  2. windows下面composer安装yii2

    1,安装composer "https://getcomposer.org/Composer-Setup.exe" 2,安装 composer-asset-plugin ,打开cm ...

  3. 学渣也要搞 laravel(4)—— 服务 加解密篇

    使用 Crypt::encrypt() 对数据进行加密,[注意要引入 use Illuminate\Support\Facades\Crypt;; ] 简单的做个测试: 先分配一个路由: Route: ...

  4. Python 手册——调用解释器

    通常Python的解释器被安装在目标机器的 /usr/local/bin/python 目录下:把 /usr/local/bin 目录放进你的UNIX Shell 的搜索路径里,确保它可以通过输入py ...

  5. 简单学c——前言

      1.学C语言需要什么基础吗? 零基础. 2.什么是C语言? C语言是一种编程语言. 3.什么是编程语言? 编程语言是用来定义计算机程序的形式语言,是一种被标准化的交流技巧,用来向计算机发出指令. ...

  6. 学习Swift -- 构造器(中)

    构造器(中) 值类型的构造器代理 构造器可以通过调用其它构造器来完成实例的部分构造过程.这一过程称为构造器代理,它能减少多个构造器间的代码重复. 构造器代理的实现规则和形式在值类型和类类型中有所不同. ...

  7. JDK源码阅读(三) Collection<T>接口,Iterable<T>接口

    package java.util; public interface Collection<E> extends Iterable<E> { //返回该集合中元素的数量 in ...

  8. jq总结1

    选择器 /** * 多目标选择器 * 可以选择多个元素或者表达式, * 包装成 jQuery 对象的集合 * 例子:$("div,span") */ $("table t ...

  9. Python学习笔记(一)Python安装及环境变量的配置

    1.下载python安装包. 下载地址:https://www.python.org/ 2.配置环境变量 找到python的安装路径.C:\Python27;script的路径:C:\Python27 ...

  10. NHibernate configuration

    http://blog.csdn.net/dbcolor/article/details/2061929