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
  • 相关阅读:
    【Go语言】I/O专题
    【Go语言】集合与文件操作
    【Go语言】LiteIDE使用的个人使用方法
    【Go语言】错误与异常处理机制
    【Go语言】面向对象扩展——接口
    【Go语言】学习资料
    创建型模式(前引)简单工厂模式Simple Factory
    redis demo
    导出CSV格式
    mongo聚合命令
  • 原文地址:https://www.cnblogs.com/yizhanhaha/p/3132333.html
Copyright © 2011-2022 走看看