UVA 11029 Leading and Trailing [数学]

Apart from the novice programmers, all others know that you can’t exactly represent numbers raised
to some high power. For example, the C function pow(125456, 455) can be represented in double data
type format, but you won’t get all the digits of the result. However we can get at least some satisfaction
if we could know few of the leading and trailing digits. This is the requirement of this problem.
Input
The first line of input will be an integer T < 1001, where T represents the number of test cases. Each
of the next T lines contains two positive integers, n and k. n will fit in 32 bit integer and k will be less
than 10000001.
Output
For each line of input there will be one line of output. It will be of the format LLL … T T T, where
LLL represents the first three digits of n
k and T T T represents the last three digits of n
k
. You are
assured that n
k will contain at least 6 digits.
Sample Input
2
123456 1
123456 2
Sample Output
123…456
152…936

——————————-

题目大意 就是求 n^k 的前三位和后三位

题解 :
后三位很好处理了 直接快速幂取模就好了

前三位就比较复杂了

n^k=A 同时取log
k*log(n)=log(A) => A=10^(k*log(n))

在这里我们知道k*log(10) =a.b (a是整数部分,b是小数部分)

在这里 a对A 的贡献就是一堆0 所以并没有什么用

所以A=10^(b)
因为要取 A 的前三位 所以A= 10^(2+b) 即可

还有一个坑点 就是后三位数如果是 12 的话 要输出012 就是前导0要有 wrong 了无数发

附本题代码

————————–

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <set>

using namespace std;

#define LL unsigned long long int

LL qmod (LL a,LL b,LL mod)
{
    LL res=1;
    while(b)
    {
        if(b&1) res=(res*a)%mod;
        b >>= 1;
        a=(a*a)%mod;
    }
    return res;
}

LL  solve(int n, LL m,int weishu)
{
    weishu-=1;

    LL p, q, ans;
    double f = m*log10(n);

    //接下来三行就是求f的小数部分的
    q = (LL)f;
    p = (LL)(f*10000000)-q*10000000;
    double x = 1.0*p/10000000;

    ans = (LL)(pow(10, x+weishu));
    return ans;
}

int main()
{
    int t,n,k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&k);
        LL a= qmod(n,k,1000) ;
        LL b= solve(n,k,3) ;
        printf("%lld...%03lld\n",b,a);
    }
    return 0;
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值