zoukankan      html  css  js  c++  java
  • HDU Rightmost Digit

                                                     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
    ", dd );
    	}
    	return 0;
    }
    
  • 相关阅读:
    Hiho----无间道之并查集
    Linux之压缩与解压
    Linux之用户和用户组
    Linux之关机/重启命令及一些符号
    Linux之基本操作命令
    Linux之vi/vim
    解决eclipse中maven报错Failed to read artifact descriptor for xxxx:jar
    CentOS 7下 solr 单机版安装与配置
    CentOS 7下 Tomcat7 安装与配置
    CentOS 7下 JDK 1.7 安装与配置
  • 原文地址:https://www.cnblogs.com/yspworld/p/3893956.html
Copyright © 2011-2022 走看看