UVA 11029 Leading and Trailing(大数n^k的前x位高精度问题)(好题)


Problem C


Leading and Trailing


Time limit: 2 seconds

 

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…TTT, where LLL represents the first three digits of
n^k and TTT represents the last three digits of
n^k. You are assured that
n^k will contain at least 6 digits.


Sample Input


Output for Sample Input


2

123456 1

123456 2


123...456

152...936

ProblemSetter: Shamim Hafiz

Alternate Solution: Jane
Alam Jan

题意:求n^k的前三位数和末尾的三位数

思路:末尾的三位数直接快速幂+取模即可

因为末尾的三位数只由运算过程中末尾的三位数决定

而首三位数可以感性认知只由运算过程中前n位数决定,后面的数的精确度几乎对答案不影响,这种性质在计算机的浮点数中很好的实现了出来,所以试图把原问题用浮点运算解决。

n^k=10^(k*log10n)m,对10取对数就是为了把答案的信息全直观存在浮点数中,因为k*log10(n)的整数部分对答案没影响(只是*10000....),所以10^k*log10(n)的前三位就是答案。

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

const int mod = 1000;
typedef long long ll;
ll n,k;
int fans,bans;
int quickpow(ll x,int k)
{
    ll ans=1;
    while(k)
    {
        if(k&1) ans=(ans*x)%mod;
        x=x*x%mod;
        k>>=1;
    }
    return (int)ans;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld%lld",&n,&k);
        bans=quickpow(n,k);
        double t=k*log10(n);
        t=t-(int)t;
        double a=pow(10,t);
        fans=(int)(a*100);
        printf("%d...%03d\n",fans,bans);
    }
    return 0;
}



时间: 2024-08-29 06:09:40

UVA 11029 Leading and Trailing(大数n^k的前x位高精度问题)(好题)的相关文章

UVa 11029 Leading and Trailing

题目要求输出N的K次方的前三位和后三位.后三位的解法不用多说了,用二分法快速去模即可.关键是前三位怎么求?题目中说N能用32位带符号整数表示,K最大是10的六次方.因此N^K的解ans最多不过10^(9*10^6),因此我们完全可以用以十为底的对数x+y表示,其中x表示对数的整数部分,y表示对数的小数部分.显然,ans的具体数字是由10^y来表示的,而x只是用来将小数以为成整数而已.并且可以确定的是,10^y必小于10且大于1,因为大于10的话,y就大于1了,而小于1的话,y就小于0了显然不可能

UVA - 11029 - Leading and Trailing (快速幂+公式变形)

题目传送:UVA - 11029 思路:后三位可以直接快速幂取模,然后前三位可以有两种做法,一个是利用double,一个是利用公式法,具体看代码吧 注意,后三位不足三位要补0,即用%03d AC代码①: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #incl

Uva 11029 Leading and Trailing (求n^k前3位和后3位)

题意:给你 n 和 k ,让你求 n^k 的前三位和后三位 思路:后三位很简单,直接快速幂就好,重点在于如何求前三位,注意前导0 资料:求n^k的前m位 博客连接地址 代码: #include <iostream> #include <cmath> #include <cstdio> #include <algorithm> #define ll long long using namespace std; ll qmod(ll a,ll b,ll mod)

UVA Leading and Trailing 11029【数学+快速幂】

11029 - Leading and Trailing Time limit: 3.000 seconds 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 da

UVA 11029 || Lightoj 1282 Leading and Trailing 数学

Leading and Trailing You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of nk. Input Input starts with an integer T (≤ 1000), denoting the number of test cases. Each case st

UVA - 11029Leading and Trailing(快速幂取模取后三位 + log10()取前三位)

题目: UVA - 11029Leading and Trailing(快速幂取模取后三位 + log10()取前三位) 题目大意:给你N的k次方,然后要求你求出这个数的前三位和后三位. 解题思路:因为n和k都很大,这个数求出来是大数,所以可以用快速幂取模求后三位,因为后面的三位和前面的位数的没有关系.前面的三位比较难办.设x = log (n^k)  = k * log10(n),那么10^x = k * log10(n).将X = a(整数) + b(小数),整数部分10^a只是移动小数点,

[LightOJ1282]Leading and Trailing

题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=26992 Description You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of nk. Input Input starts with an in

LightOJ 1282 Leading and Trailing (快数幂 + 数学)

http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1282 Description You are given two integers: n and k, your task is t

[题解]UVA11029 Leading and Trailing

链接:http://vjudge.net/problem/viewProblem.action?id=19597 描述:求n^k的前三位数字和后三位数字 思路:题目要解决两个问题.后三位数字可以一边求高次幂一边取模,下面给出求前三位数字的方法. n^k = (10^lg n)^k = 10^(k*lg n) 为了描述方便,令X=n^k .则 lg X 的整数部分表示X有多少位.设整数部分为zs,小数部分为xs,则X=(10^zs)*(10^xs) . (10^zs)的形式就是100……,跟X的位