Leading and Trailing
https://vjudge.net/contest/288520#problem/E
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
求前三位则需要一些数学知识对于给定的一个数n,它可以写成10^a,其中这个a为浮点数,则n^k=(10^a)^k=10^a*k=(10^x)*(10^y);其中x,y分别是a*k的整数部分和小数部分,对于t=n^k这个数,它的位数由(10^x)决定,它的位数上的值则有(10^y)决定,因此我们要求t的前三位,只需要将10^y求出,在乘以100,就得到了它的前三位。
fmod(x,1)可以求出x的小数部分。
参考博客:https://blog.csdn.net/w144215160044/article/details/48916839
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7 #define eb emplace_back 8 #define maxn 100005 9 #define eps 1e-8 10 #define pi acos(-1.0) 11 #define rep(k,i,j) for(int k=i;k<j;k++) 12 typedef long long ll; 13 typedef pair<int,int> pii; 14 typedef pair<long long,int>pli; 15 typedef pair<int,char> pic; 16 typedef pair<pair<int,string>,pii> ppp; 17 typedef unsigned long long ull; 18 const long long mod=998244353; 19 /*#ifndef ONLINE_JUDGE 20 freopen("1.txt","r",stdin); 21 #endif */ 22 23 int pow_mul(ll a,ll b){ 24 int ans=1; 25 while(b){ 26 if(b&1) ans=ans*a%1000; 27 b>>=1; 28 a=a*a%1000; 29 } 30 return ans; 31 } 32 33 int main(){ 34 #ifndef ONLINE_JUDGE 35 // freopen("1.txt","r",stdin); 36 #endif 37 // std::ios::sync_with_stdio(false); 38 int t; 39 cin>>t; 40 for(int _=1;_<=t;_++){ 41 ll n,k; 42 cin>>n>>k; 43 int ans1=pow(10.0,2.0+fmod(k*log10(n*1.0),1)); 44 int ans2=pow_mul(n,k); 45 printf("Case %d: %d %03d\n",_,ans1,ans2); 46 } 47 48 }
原文地址:https://www.cnblogs.com/Fighting-sh/p/10551110.html