[题解]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的位数一样,那么(10^xs)就是在100……的基础上修饰每一位上的数字,使之成为X。那么(10^xs)*100就是X的前三位(没想清楚的朋友可以在纸上划一划)。

下面给出我的实现。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 using namespace std;
 5 #define MOD 1000
 6 int T,N,K;
 7 int Trail;
 8 double Lead;
 9 inline void Get_int(int &Ret)
10 {
11     char ch;
12     bool flag=false;
13     for(;ch=getchar(),ch<‘0‘||ch>‘9‘;)
14         if(ch==‘-‘)
15             flag=true;
16     for(Ret=ch-‘0‘;ch=getchar(),ch>=‘0‘&&ch<=‘9‘;Ret=Ret*10+ch-‘0‘);
17     flag&&(Ret=-Ret);
18 }
19 int My_Pow(int n,int k)
20 {
21     if(k==1)
22         return n%MOD;
23     int p=My_Pow(n,k/2);
24     if(k%2)
25         return (p*p*(n%MOD))%MOD;
26     return (p*p)%MOD;
27 }
28 int main()
29 {
30     Get_int(T);
31     while(T--)
32     {
33         Get_int(N);Get_int(K);
34         Lead=(double)K*log10(N);
35         Lead=Lead-(int)Lead;
36         Lead=pow((double)10,2+Lead);
37         Trail=My_Pow(N,K);
38         printf("%d...%03d\n",(int)Lead,Trail);
39     }
40     return 0;
41 }

[题解]UVA11029 Leading and Trailing

时间: 2024-08-29 06:09:40

[题解]UVA11029 Leading and Trailing的相关文章

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

uva11029 - Leading and Trailing

题目: 求n的k次方,然后将答案用前三位和最后三位表示. Sample Input 2 123456 1 123456 2 Sample Output 123...456 152...936 分析: 题目中其实有提示,用double来表示n的k次方,double神奇的地方在于能转化为string类型的字符串.用到了sprintf这个函数.代码: #include <cstdio> #include <iostream> #include <cstring> using

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

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

快速幂 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 <