zoukankan      html  css  js  c++  java
  • LightOJ 1282 Leading and Trailing (快速幂+fmod)

    求n^k的前三位以及后三位;
    后三位可用快速幂求解;
    求n^k的前三位:
    n可以写成10^a(a为小数)的形式。n^k=(10^a)^k,即10^(a*k),a*k可以写成x(整数)+y(小数)的形式;
    函数:fmod(x,1)可以求出x的小数部分,因此用fmod(a*k,1)即可求出y。

    #include <algorithm>
    #include <iostream>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <map>
    using namespace std;
    typedef long long LL;
    const LL N = 1e6 + 10;
    const LL MOD = 1000;
    LL func(LL n, LL m)
    {
        LL sum = 1;
        while(m > 0) {
            if(m % 2)
                sum = (sum * n) % MOD;
            n = (n * n) % MOD;
            m /= 2;
        }
        return sum;
    }
    int main()
    {
        LL n, it = 1, i, m, k;
        cin >> n;
        while(n--) {
            scanf("%lld%lld", &m, &k);
            printf("Case %lld:", it++);
            LL ans = 100.0 * pow(10.0, fmod(k * log10(m * 1.0), 1));
            LL num = func(m, k);
            printf(" %03lld %03lld
    ", ans, num);
        }
        return 0;
    }
  • 相关阅读:
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    LeetCode
  • 原文地址:https://www.cnblogs.com/yu0111/p/6739804.html
Copyright © 2011-2022 走看看