UVA Leading and Trailing 11029 "math + fast Power"

Source: Internet
Author: User

11029-leading and trailingtime limit:3.000 seconds

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, 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 an integer T < 1001, where T represents the number of test cases. each
Of the next T lines contains-positive integers, n and K. N would fit in the net bit integer and K 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 of N K and TTT represents the last three digits of n K. You are
Assured that n K would contain at least 6 digits.
Sample Input
2
123456 1
123456 2
Sample Output
123...456
152...936


Test instructions: give you a number n and let you beg it for the top three and last three digits after the K-th square.

Problem Solving Ideas:

After three bit better think, direct fast power to take surplus on it. The primary three-bit calculation.

We can think of a number, for example 1589. If this number is converted, replace it with 10^x.  Then x is equal to LOG10 (1589). And if we put log10 (1589) Mod 1, the result of the 1 remainder is that we only take the decimal digits of x. Imagine, if the x is divided into integers z and fractional g, then 10^x=10^ (z+g) =10^g*10^z,z is an integer, so 10^z is 10000~, a number multiplied by 10^z means that we want the number is the number of z+1 bit. If you change Z to 2, we get a three-digit number, and this number is the top three of n^k.

The *fmod function. Fmod (x, y) calculates the remainder of the X/Y.

AC Code:

#include <stdio.h> #include <math.h> #include <vector> #include <queue> #include <string> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm>using Namespace Std;typedef long long LL; ll Pow_mod (ll x,ll N) {    ll res=1;    while (n) {        if (n&1) res=res*x%1000;        x=x*x%1000;        n>>=1;    }    return res;} int main () {    int t;    scanf ("%d", &t);    int xp=0;    while (t--) {        LL n,k;        scanf ("%lld%lld", &n,&k);        int Ans=pow (10,2+fmod (K*LOG10 (n), 1));        int Res=pow_mod (n,k);        printf ("%d...%03d\n", ans,res);    }    return 0;}


Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

UVA Leading and Trailing 11029 "math + fast Power"

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.