UVA - 11029 Leading and Trailing

题意:求前三位,后三位,后三位我们可以用快速幂解决,至于前三位是这样来的,

因为n^k = 10^(log10(n^k))=10^(k*log10(n)),对这个数取整的话,小数部分再*100就是前三位了,显然前三位只受k*log10(n)小数部分的影响,最后再取整,还有后三位不够三位用0补

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int n,k;

int Front(int n,int k){
    double s = k*log10(n) - (int)(k*log10(n));
    s = pow(10,s);
    return s*100;
}

int Rear(int n,int k){
    if (!k)  
        return 1;
    long long s = Rear(n, k/2);
    if (k % 2 == 1)
        s = s*s % 1000 * n % 1000;
    else
        s = s*s % 1000;
    return s;
}



int main(){
    int t;
    scanf("%d",&t);
    while (t--){
        scanf("%d%d",&n,&k);
        printf("%d...%03d\n",Front(n,k),Rear(n,k));
    }
    return 0;
}



你可能感兴趣的:(UVA - 11029 Leading and Trailing)