zoukankan      html  css  js  c++  java
  • 杭电 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
  • 相关阅读:
    2020.05.02【NOIP普及组】模拟赛C组31总结
    【提高组NOIP2008】传纸条 题解
    【NOIP2006PJ】Jam的计数法(count)题解
    话说placeholder
    css垂直居中
    fixed和absolute的区别
    链接的属性href=“?” ?该些什么及优缺点
    论ul、ol和dl的区别
    笔记本插拔电源黑屏一下
    CSS 样式书写规范
  • 原文地址:https://www.cnblogs.com/yizhanhaha/p/3132333.html
Copyright © 2011-2022 走看看