1282

http://lightoj.com/volume_showproblem.php?problem=1282

题目大意: 求n的k次方的前三位和后三位数然后输出

后三位是用快速幂做的,我刚开始还是不会快速幂,后来慢慢理解了。

前三位求得比较厉害

我们可以吧n^k = a.bc * 10.0^m;

k*log10(n)  = log10(a.bc) + m;

m为k * lg(n)的整数部分,lg(a.bc)为k * lg(n)的小数部分;

x = log10(a.bc) = k*log10(n) - m = k*log10(n) - (int)k*log10(n);

x = pow(10.0, x);

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>

using namespace std;
typedef long long int LL;
#define N 1001000
#define ESP 1e-8
#define INF 0x3f3f3f3f
#define memset(a,b) memset(a,b,sizeof(a))

int Pow(int a, int b, int c)
{
    if(b == 0)
        return 1;
    LL t = Pow(a, b>>1, c);

    t = t*t%c;
    if(b%2 == 1)
        t = t*a%c;

    return t;
}///快速幂

int main()
{
    int T, t=1;
    scanf("%d", &T);
    while(T --)
    {
        int n, k;
        scanf("%d %d", &n, &k);

        double m = k*log10(n) - (LL)(k*log10(n));
        m = pow(10.0, m);

        int ans = Pow(n, k, 1000);

        printf("Case %d: %d %03d
", t++, (int)(m*100), ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/linliu/p/5547089.html