UVA 11029 Leading and Trailing

Source: Internet
Author: User
Tags pow

Original question:
Apart from the novice programmers, all others know so you can ' t exactly represent numbers raised to some high power. For example, the C function pow (125456, 455) can is represented in double data type format, and you won ' t get all the Digi TS 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, where T represents the number of test cases. Each of the next T lines contains the positive integers, n and K. N would fit in the net bit integer and K would be is less than 100 00001.

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 of N^k and TTT represents the last three digits of n^k. You is assured that N^k would contain at least 6 digits.

Sample Input
2
123456 1
123456 2

Sample Output
123 ... 456
152 ... 936

English:
Lets you calculate the first 3 bits and the last 3 bits of the n^k.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod=1000;

ll Quick_power (ll N, ll K)
{
    ll res = 1;
    while (k)
    {
        if (K & 1)
            res = res * n MoD;
        n = n * n% MoD;
        K >>= 1;
    }
    return res;
}
int main ()
{
//  Ios::sync_with_stdio (false);
    int t;
    scanf ("%d", &t);
    ll N,k;
    while (t--)
    {
        scanf ("%lld%lld", &n,&k);
        ll B=quick_power (n,k);
        ll A= (LL) (POW (10.0, 2 + fmod (K*LOG10 (1.0*n), 1)));
        printf ("%lld...%03lld\n", A, b);
    }
    return 0;
}

Ideas

The last three bits are very good, fast power to take the mold on the line.
How to ask for the front three.
Search on the Internet, it feels amazing.
Set N^k=p

The result of P taking the logarithm of base 10 is Q

The integer digits of q are represented by x, and the decimal digits are denoted by y
The key is above this decimal place.

Back to the count.
p= (10^x) x (10^y)

10^x said that the number of P is a few numbers, then 10^y must be p this number reduced x-bit

For example
Suppose p=654321
Find the integer part and fractional part X and y of log10 (p)
X=5

654321 = (10^5) x (10^y)
Apparent (10^y) =6.54321
Multiplying (10^y) by 100 and then rounding is the top three, it's fantastic, ha-_-.

Manual write loss accuracy, using the library function fmod

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.