zoukankan      html  css  js  c++  java
  • HDU

    Description

    求A^B的最后三位数表示的整数。 
    说明:A^B的含义是“A的B次方” 

    Input

    输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A=0, B=0,则表示输入数据的结束,不做处理。

    Output

    对于每个测试实例,请输出A^B的最后三位表示的整数,每个输出占一行。 

    Sample Input

    2 3
    12 6
    6789 10000
    0 0

    Sample Output

    8
    984
    1



    -----------------------------------------------------我是分割线^_^--------------------------------------------------------------------


    要特别注意一个结论:a*b%c = a%c * b%c.(也可以等于(a%c * b%c)%c,因为可能影响到结果)
    这是一个快速幂取模的模板,除此之外还有快速乘取模,之后有一个题会说到

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    using namespace std;
    
    int main()
    {
        //freopen("input.txt", "r", stdin);
        int a, b;
        while (scanf("%d%d", &a, &b), a || b) {
            int ans = 1;
            a %= 1000;
            while (b > 0) {//快速幂取模
    
                if (b % 2 == 1) {
                    ans = (ans * a) % 1000;//如果幂的次数是奇数,就优先一步乘入结果,让幂的次数变偶数
                }
                b>>=1;
                a = (a * a) % 1000;//例如让2的四次方变成4的二次方,从而减少运算次数
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
     
  • 相关阅读:
    PHP设计模式
    秒杀方案
    lua 安装
    docker 相关命令
    dockerfile
    JS工具对象 DATE 方法
    JS工具对象 Array
    JS工具对象 String 10种常用 方法
    工具对象
    JS工具对象Math 7个常用 方法
  • 原文地址:https://www.cnblogs.com/steamedbun/p/5727617.html
Copyright © 2011-2022 走看看