• 杭电 1097 A hard puzzle


    Problem Description
    lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin.
    this puzzle describes that: gave a and b,how to know the a^b's the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise.
     
    Input
    There are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)
     
    Output
    For each test case, you should output the a^b's last digit number.
     
    Sample Input
    7 66 8 800
     
    Sample Output
    9 6
     
    Author
    eddy
     
    Recommend
    JGShining
        问题描述:给两个32位的整型数啊a, b,计算出a^b的最后一位!
        问题解答:本题的解决方法:快速幂取余。所选用算法的时间复杂度:log(b);但是它的相关理论我现在还不是很理解,只是先拿来借用!
     1 #include <stdio.h>
     2 
     3 int pow_mod( int a, int n, int m )
     4 {
     5     int ans = 1;
     6     a = a % m;
     7     while( n > 0 )
     8     {
     9         if( n % 2 == 1 )
    10             ans = ( ans * a ) % m;
    11         n = n / 2;
    12         a = ( a * a ) % m;
    13     }
    14     return ans;
    15 }
    16 int main()
    17 {
    18     int a, n;
    19     while( scanf( "%d%d",&a, &n ) != EOF )
    20         printf( "%d\n", pow_mod(a,n,10) );
    21 
    22     return 0;
    23 }
    View Code
  • 相关阅读:
    Nginx 相关配置文件修改
    LNMP平台构建实验 +bbs社区搭建
    CSGO项目
    创世战车项目
    IGXE搬砖项目
    11_samba服务器的搭建
    26_django内置static标签
    06_git添加远程仓库并向远程仓库中推送代码
    23_添加apps到项目的搜索路径
    23_django日志器的配置和其使用方法
  • 原文地址:https://www.cnblogs.com/yizhanhaha/p/3132333.html
Copyright © 2020-2023  润新知