找出数学规律

原题:

Rightmost Digit

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

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.

讲解:(摘自网上)

  对于数字0~9, 它们的N次方,我们只看最后一个数字:
  0^1=0; 0^2=0; 循环周期T=1;
  1^1=1; 1^2=1; 循环周期T=1;
  2^1=2; 2^2=4; 2^3=8; 2^4=6 2^5=2; 循环周期T=4;
  3^1=3; 3^2=9; 3^3=7; 3^4=1;3^5=3; 循环周期T=4;
  4^1=4; 4^2=6; 4^3=4; 循环周期T=2;
  
  后面的大家可以自己算,会得出一个规律:
  他们的循环周期只有1,2,4这三种,所以对于源代码里的a,我们可以只算a%4次就可以了,大大减少了循环的次数

源代码:

 #include <iostream>
using namespace std; int zhou[]; int main() {
int N; cin >> N;
while (N--) {
long long int num;
cin >> num;
int a = num % ;
int b = (a * a) % ;
int c = (b * a) % ;
int d = (c * a) % ;
int m = num % ;
switch(m) {
case : cout << d << endl; break;
case : cout << a << endl; break;
case : cout << b << endl; break;
case : cout << c << endl; break;
}
}
return ;
}

HDOJ 1061 Rightmost Digit的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. 快速幂 HDU 1061 Rightmost Digit *

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

  7. 【HDOJ】1061 Rightmost Digit

    这道题目可以手工打表,也可以机器打表,千万不能暴力解,会TLE. #include <stdio.h> #define MAXNUM 1000000001 ][]; int main() ...

  8. HDU 1061 Rightmost Digit解决问题的方法

    求大量N^N的值最右边的数字,即最低位. 它将能够解决一个简单二分法. 只是要注意溢出,只要把N % 10之后.我不会溢出,代替使用的long long. #include <stdio.h&g ...

  9. hdu 1061 Rightmost Digit

    解决本题使用数学中的快速幂取余: 该方法总结挺好的:具体参考http://www.cnblogs.com/PegasusWang/archive/2013/03/13/2958150.html #in ...

随机推荐

  1. Qt_QTabWidget_多行

    ZC: 一下是网上搜到的一些信息 ZC: 我只是在 Android环境做了一些测试,别的环境暂未测试... 1.自定义 QTabBar子类 #ifndef MYTABBAR_H #define MYT ...

  2. fragment 小结

    1:注意事项   3.0以前的Android 版本要使用FragmentActivity 来装载Fragment ,使用到support v4包.   3.0以后的版本可以直接在Activity里面添 ...

  3. android listview的HeadView左右切换图片(仿新浪,网易,百度等切换图片)

    首先我们还是看一些示例:(网易,新浪,百度) 显示效果都不错,可是手感就不一样了,百度最棒,网易还行,新浪就操作很不好,这里我说的是滑动切换图片.自己可以测试一下.不得不说牛叉的公司确实有哦牛叉的道理 ...

  4. Java-Java程序设计的基本概念

    2017-10-06 15:31:39 一.Java程序的基本构成             二.数据类型与标识符 数据类型 标识符 广义的用于定义各种对象名称的字符串集合称为标识符,标识符一般分为用户 ...

  5. HTML表单组件

    HTML表单组件 一.说明 form标签里面的东西 二.效果图 三.代码 <!DOCTYPE html> <html> <head> <title>Fo ...

  6. 真核生物基因结构 & mRNA结构

    参考: 分子生物学教材 再一次,翻看真核生物基因结构! mRNA基本结构特点 Structure and function of Messenger RNA (mRNA ) 基因结构 其实这个结构不完 ...

  7. 20170622xlVBA多部门分类汇总同类合并单元格

    Public Sub Basic_CodeFrame() AppSettings On Error GoTo ErrHandler Dim StartTime, UsedTime As Variant ...

  8. quartz---的SimpleTrigger

    quartz---的SimpleTrigger package com.imooc.demo.helloQuartz; import java.text.SimpleDateFormat; impor ...

  9. PHP header函数设置http报文头示例详解

    //定义编码 header( 'Content-Type:text/html;charset=utf-8 '); //Atom header('Content-type: application/at ...

  10. [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆

    转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...