Lightoj 1282 leading and Trailing (Fast Power + math)

Source: Internet
Author: User

http://lightoj.com/volume_showproblem.php?problem=1282

Leading and Trailing Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%LLD &%llusubmit Status Practice Lightoj 1282

Description

You are given-integers: n and K, your task is to find the most significant three digits, and least s Ignificant three digits of NK.

Input

Input starts with an integer T (≤1000), denoting the number of test cases.

Each case starts with a line containing the integers: N (2≤n < 231) and K (1≤k≤107).

Output

For each case, print the case number and the three leading digits (most significant) and three trailing digits (least sign Ificant). You can assume this input is given such this NK contains at least six digits.

Sample Input

5

123456 1

123456 2

2 31

2 32

29 8751919

Sample Output

Case 1:123 456

Case 2:152 936

Case 3:214 648

Case 4:429 296

Case 5:665 669

Main topic: Give two number n, K, let the first three and the last three digits of n^k

Analysis:

After the three-bit direct use of the fast power to take the remainder can be calculated

Top three we can convert N^k to A.BC * 10^m, so ABC is the top three, n^k = A.BC * 10^m

LG (N^K) = LG (A.BC * 10^m)

<==>k * LG (N) = LG (A.BC) + LG (10^M) = LG (A.BC) + M

M is the integer portion of k * LG (N), and LG (A.BC) is a fractional part of K * LG (N)

x = LG (A.BC) = k * LG (n)-m = k * LG (N)-(int) (k * LG (N))

A.BC = POW (ten, X);

ABC = A.BC * 100;

So the first three digits of ABC can find

#include <stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>#include<algorithm>using namespaceStd;typedefLong Longll;intPow (intAintb) {    intAns =1; A%= +;  while(b) {if(b%2!=0) ans= (ans * a)% +; A= (A * a)% +; b/=2; }    returnans;}//Fast number PowerintMain () {intT, N, K, p =0; scanf ("%d", &t);  while(t--) {p++; scanf ("%d%d", &n, &k); Doublem = k * LOG10 (N)-(int) (k *log10 (n)); M= POW (Ten, M); intx = m * -; inty =Pow (n, k); printf ("Case %d:%d%03d\n", p, x, y); }    return 0;}

Lightoj 1282 leading and Trailing (Fast Power + math)

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.