Leading and Trailing
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 |
Output for Sample Input |
5 123456 1 123456 2 2 31 2 32 29 8751919 |
Case 1:123 456 Case 2:152 936 Case 3:214 648 Case 4:429 296 Case 5:665 669 |
Test instructions: n^k the first three digits and the last three digits;
Idea: The last, apparently fast power% 1000; Note that the output requires a leading 0;
Start with double, fast power, each will base in 1-10, the last answer *100 to int on the line;
#include <iostream>#include<string>#include<cstring>#include<algorithm>#include<cstdio>using namespacestd;#definell Long Long#defineESP 1E-13Const intn=1e4+Ten, m=1e6+50000, inf=1e9+Ten, mod=1000000007;intQuickpow (intXinty) {x%= +; intsum=1; while(y) {if(y&1) sum*=x,sum%= +; X*=x; X%= +; Y>>=1; } returnsum;}voidChangeDouble&a) { while(A-10.0>= (-ESP)) {a/=Ten; }}DoublePow1 (DoubleXinty) {change (x); Doubleans=1.0; while(y) {if(y&1) ans*=x,change (ans); X*=x; Change (x); Y>>=1; } returnans;}intMain () {intx,y,i,z,t; intT,cas; scanf ("%d",&T); for(cas=1; cas<=t;cas++) {scanf ("%d%d",&x,&y); DoubleA= (Double) x; Doubleans=Pow1 (a,y); intyu=Quickpow (x, y); if(yu>= -) printf ("Case %d:%d%d\n", CAs, (int) (ans*100.0), Yu); Else if(yu>=Ten) printf ("Case %d:%d 0%d\n", CAs, (int) (ans*100.0), Yu); Else if(yu>=0) printf ("Case %d:%d 00%d\n", CAs, (int) (ans*100.0), Yu); } return 0;}
UVA 11029 | | Lightoj 1282 leading and Trailing math