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 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).
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 |
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 |
题目大意:输出前三位和后三位;
题解:后三位这里如果是2的话,输出002 ; 如果是22输出022;后三位用快速幂取模然后“%03lld”输出;
前三位的话推公式:
如果有 x = n^k = k*(log10(n*1.0));
p1 = k*(log10(n*1.0)) - (int) k*(log10(n*1.0)); 这里是小数部分;
y =(int) pow(10,p1+2.0); 因为要求的是前三位; 即可;
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #define ll long long using namespace std ; ll quick(ll a , ll b ,ll mod) { ll res = 1 ; a = a % mod ; while(b) { if(b%2) res = res * a % mod ; b/=2 ; a = a * a % mod ; } return res ; } double fun(int n , int k ) { double most ; most = k*log10(n*1.0) ; most -=(int)most; most=pow(10,2.0+most); return most ; } int main() { int t ; cin>>t; for(int cas = 1 ; cas <= t ; cas++) { ll a , b ; cin>>a>>b; ll judge1 = quick(a,b,1000); ll judge2 = fun(a,b); printf("Case %d: %lld %03lld\n",cas,judge2,judge1); } return 0 ; }