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 <bits/stdc++.h>
 2 using namespace std;
 3 #define debug(a) cout<<#a<<":"<<a<<endl;
 4 typedef long long ll;
 5 const int mod=1e3;
 6 int maxn,minn;
 7 int T,n;
 8 ll ksm(ll a,ll b){
 9     ll ans=1;
10     while(b){
11         if(b&1){
12             ans=ans*a%mod;
13         }
14         a=a*a%mod;
15         b>>=1;
16     }
17     return ans;
18 }
19
20 int main(){
21     ll a,b,c;
22     ll a1,a2,num=0;
23     cin>>T;
24     while(T--){
25         num++;
26         cin>>a>>b;
27         a1=pow(10,2+fmod((double)b*log10(a),1));
28         while(a1>1000){
29             a1/=10;
30         }
31         a2=ksm(a,b);
32         printf("Case %d: %03lld %03lld\n",num,a1,a2);
33     }
34
35     return 0;
36 }

原文地址:https://www.cnblogs.com/meanttobe/p/12397210.html

时间: 2024-07-30 13:23:04

E - Leading and Trailing (log的应用)的相关文章

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

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

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), den

UVa 11029 Leading and Trailing

题目要求输出N的K次方的前三位和后三位.后三位的解法不用多说了,用二分法快速去模即可.关键是前三位怎么求?题目中说N能用32位带符号整数表示,K最大是10的六次方.因此N^K的解ans最多不过10^(9*10^6),因此我们完全可以用以十为底的对数x+y表示,其中x表示对数的整数部分,y表示对数的小数部分.显然,ans的具体数字是由10^y来表示的,而x只是用来将小数以为成整数而已.并且可以确定的是,10^y必小于10且大于1,因为大于10的话,y就大于1了,而小于1的话,y就小于0了显然不可能