Leading and Trailing (数论)

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

时间: 2024-08-04 04:57:41

Leading and Trailing (数论)的相关文章

LightOJ 1282 Leading and Trailing 数论

题目大意:求n^k的前三位数 和 后三位数. 题目思路:后三位数直接用快速幂取模就行了,前三位则有些小技巧: 对任意正数都有n=10^T(T可为小数),设T=x+y,则n=10^(x+y)=10^x*10^y,其中10^x为10的整倍数(x为整数确定数位长度),所以主要求出10^y的值. T=log10(n^k)=klog10(n),可以调用fmod函数求其小数部分即y值. #include<iostream> #include<algorithm> #include<cst

UVA 11029 || Lightoj 1282 Leading and Trailing 数学

Leading and Trailing 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 st

LightOJ 1282 Leading and Trailing (快数幂 + 数学)

http://lightoj.com/volume_showproblem.php?problem=1282 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 t

UVA 11029 Leading and Trailing(大数n^k的前x位高精度问题)(好题)

Problem C Leading and Trailing Time limit: 2 seconds   Apart from the novice programmers, all others know that you can't exactly represent numbers raised to some high power. For example, the C function pow(125456, 455) can be represented in double da

[题解]UVA11029 Leading and Trailing

链接:http://vjudge.net/problem/viewProblem.action?id=19597 描述:求n^k的前三位数字和后三位数字 思路:题目要解决两个问题.后三位数字可以一边求高次幂一边取模,下面给出求前三位数字的方法. n^k = (10^lg n)^k = 10^(k*lg n) 为了描述方便,令X=n^k .则 lg X 的整数部分表示X有多少位.设整数部分为zs,小数部分为xs,则X=(10^zs)*(10^xs) . (10^zs)的形式就是100……,跟X的位

UVA Leading and Trailing 11029【数学+快速幂】

11029 - Leading and Trailing Time limit: 3.000 seconds Apart from the novice programmers, all others know that you can't exactly represent numbers raised to some high power. For example, the C function pow(125456, 455) can be represented in double da

UVA11029 Leading and Trailing【快速模幂+数学】

Apart from the novice programmers, all others know that you can't exactly represent numbers raised to some high power. For example, the C function pow(125456, 455) can be represented in double data type format, but you won't get all the digits of the

快速幂 E - Leading and Trailing LightOJ - 1282

E - Leading and Trailing LightOJ - 1282 快速幂主要是把n拆成2进制位,如果这一位有那么就乘,没有就不乘,而计数器也就是x是不断推进的,从x->x^2->x^4直到n的最高位精髓在于取模,后一步的要求结果只与前一步的模后数据有关 . 对于后三个数用了log10.log函数对求n^k这种问题还是很有用的.没想出来. 1 #include <iostream> 2 #include <cstring> 3 #include <st

E - Leading and Trailing (log的应用)

E - Leading and Trailing 题目链接:https://vjudge.net/problem/LightOJ-1282#author=yyb 题目大意: 给定两个数n,k 求n^k的前三位和最后三位. 解题思路: $b = a^{n}$  可以推出 $10^{n\log_{10}a} = b$. 然后计算n*log10(a),他可能大于1所以对1取余得到k,然后计算pow(10,2+k),得到前三位即可. 后三位ksm对1000取余就能得到. 代码: 1 #include <