求n^k的前3位和后3位

来源:互联网 发布:淘宝网关键词排名查询 编辑:程序博客网 时间:2024/06/13 02:11

E - Leading and Trailing
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Submit Status Practice LightOJ 1282

Description

You are given two integers: n and k, your task is to find the most significant three digits, and least significant 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 two 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 significant). You can assume that the input is given such that 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




#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <cstdlib>#include <cstdio>#include <algorithm>using namespace std;typedef long long ll;int qpow(ll x,ll k,ll mod){ll ret = 1;ll mul = x;while(k > 0){if(k & 1){ret = (ret * mul) % mod;}mul = mul * mul % mod;k >>= 1;}return ret;}int main(){int T;scanf("%d",&T);int C = 1;while(T--){ll n,k;scanf("%lld%lld",&n,&k);int x = pow(10,2+k*log10(n)-int(k*log10(n)));int y = qpow(n,k,1000);printf("Case %d: %d %03d\n",C++,x,y);}    return 0;}


0 0