UVA 11029 Leading and Trailing (top X-position high-precision problem for large numbers of n^k) (Good question)

Source: Internet
Author: User



< P align= "Center" style= "Text-align:center" > Problem C

leading and Trailing

Apart from the novice programmers, all others know so you can ' t exactly represent numbers raised to some high power. For example, the C functionpow(125456, 455) can is 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 would be a integer t<1001, whereT represents the number of test cases. Each of the nextT lines contains the positive integers, n and K. N would fit in the net bit integer andK would be is less than 10000001.

Output

For each line of input there would be a line of output. It'll be the of the format LLL ... TTT, where LLL represents the first three digits ofn^k and TTT represents the last three digits ofn^k. You is assured that n^k would contain at least 6 digits.

Span style= "Color:maroon; font-family:arial; Font-size:16pt ">sample Input

output for Sample Input

2

123456 1

123456 2

123...456

152...936

ProblemSEtter:shamim Hafiz

Alternate solution:janeAlam Jan



Test instructions: The first three digits of n^k and the three digits at the end

Idea: At the end of the three-digit direct fast power + modulus can be

Because the three-digit number at the end is determined only by the three-digit number at the end of the operation

And the first three digits can be perceptual cognition only by the first n digits of the operation process, the accuracy of the subsequent number of almost no effect on the answer, this property in the computer floating point number is very well implemented, so try to solve the original problem with floating point arithmetic.

n^k=10^ (k*log10n) m, the logarithm of 10 is to make the information of the answer is fully visualized in the floating point number, because the integer part of K*LOG10 (n) has no effect on the answer (just *10000 ...), so the first three bits of 10^K*LOG10 (n) are the answer.

#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;}



UVA 11029 Leading and Trailing (top X-position high-precision problem for large numbers of n^k) (Good question)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.