UVA-11029 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 datatype format, but you won’t get all the digits of the result. However we can get at least some satisfaction if we could know few of the leading and trailing digits. This is the requirement of this problem.
Input
The first line of input will be an intege.T<1001, where T represents the number of test cases. Each of the next T lines contains two positive integers,n and k.n will fit in
32 bit integer and k will be less than 10000001.
Output
For each line of input there will be one line of output. It will be of the formt.LLL:::TTT, where LLL represents the first three digits of n,k and TTT represents the last three digits of n,k. You are assured that n k will contain at least 6 digits.
Sample Input
2
123456 1
123456 2
Sample Output
123...456
152...936
题目大意:两个数n、k,求n^k的前三位数字与后三位数字。
题目解析:后三位数字很容易求,通过幂取模即可。先考虑n不做幂运算时如何取前三位,令d=log10(n),则d=a(整数部分)+i(小数部分),n=10^d=10^(a+i)=10^a*10^i。
则10^i*100即为前三位。以123456为例,d=log10(123456)=5+i,则123456=10^i*10^5。又因为123456=1.23456*10^5,所以10^i=1.23456,所以前三位为10^i*100=123。
再来考虑n做幂运算时,将n^k变换为10^(klog(n)),则d=k*log10(n),接下来就不必细说了。
代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<string>
# include<cmath>
# include<algorithm>
using namespace std;
int mypow(long long a,long long b)
{
if(b==0)
return 1;
if(b==1){
a%=1000;
return a;
}
long long t=mypow(a,b/2);
t*=t;
t%=1000;
if(b&1)
t*=a;
t%=1000;
return t;
}
int main()
{
int T,k,i;
long long n;
scanf("%d",&T);
while(T--)
{
cin>>n>>k;
int ans2=mypow(n,k);
double u=k*log10(n);
double ans1=pow(10,u-(int)u)*100;
printf("%d...%03d\n",(int)ans1,ans2);
}
return 0;
}
UVA-11029 Leading and Trailing的更多相关文章
- Uva 11029 Leading and Trailing (求n^k前3位和后3位)
题意:给你 n 和 k ,让你求 n^k 的前三位和后三位 思路:后三位很简单,直接快速幂就好,重点在于如何求前三位,注意前导0 资料:求n^k的前m位 博客连接地址 代码: #include < ...
- 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 ...
- lightoj 1282 && uva 11029
Leading and Trailing lightoj 链接:http://lightoj.com/volume_showproblem.php?problem=1282 uva 链接:http:/ ...
- LightOJ 1282 Leading and Trailing (快数幂 + 数学)
http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS Me ...
- 【LightOJ1282】Leading and Trailing(数论)
[LightOJ1282]Leading and Trailing(数论) 题面 Vjudge 给定两个数n,k 求n^k的前三位和最后三位 题解 这题..真的就是搞笑的 第二问,直接输出快速幂\(m ...
- Leading and Trailing (数论)
Leading and Trailing https://vjudge.net/contest/288520#problem/E You are given two integers: n and k ...
- Leading and Trailing(数论/n^k的前三位)题解
Leading and Trailing You are given two integers: n and k, your task is to find the most significant ...
- E - Leading and Trailing 求n^k得前三位数字以及后三位数字,保证一定至少存在六位。
/** 题目:E - Leading and Trailing 链接:https://vjudge.net/contest/154246#problem/E 题意:求n^k得前三位数字以及后三位数字, ...
- LightOJ1282 Leading and Trailing —— 指数转对数
题目链接:https://vjudge.net/problem/LightOJ-1282 1282 - Leading and Trailing PDF (English) Statistics ...
随机推荐
- Jmeter--正则表达式提取器
正则提取器的一般使用场景是, 在我第二个请求参数中需要加入第一个请求的返回值, 此时通过正则提取器可以提取第一个请求返回值中指定的字段信息并赋值, 在第二个请求参数中直接引用该变量即可 jmeter的 ...
- 手撕vue-cli配置文件——check-versions.js篇
check-versions.js,vue-cli中检查版本的js文件. 'use strict' const chalk = require('chalk') const semver = requ ...
- Python入门之面向对象编程(四)Python描述器详解
本文分为如下部分 引言——用@property批量使用的例子来引出描述器的功能 描述器的基本理论及简单实例 描述器的调用机制 描述器的细节 实例方法.静态方法和类方法的描述器原理 property装饰 ...
- P4001 [BJOI2006]狼抓兔子(对偶图)
P4001 [BJOI2006]狼抓兔子 最短路+对偶图 看这题最容易想到的就是网络流.Dinic可以过,据说还跑得比正解快. 如果不写网络流,那么需要知道2个前置知识:平面图和对偶图(右转baidu ...
- C_Learning(3)
/ 结构体 / 声明结构体类型的一般形式: struct 结构体名[--表示的是这个结构体的类型] { 成员列表 }; [不要漏掉这个";"] / 声明结构可以放在main函 ...
- Metasploit应用举例
本篇文章包含以下几方面内容: 1.Metasploit端口扫描: 2.用其他模块 3.metasploit smb获取系统信息 4.Metsploit服务识别 5.ftp识别: 6.metasploi ...
- Python3基础 str *运算 重复拼接字符串
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- linux磁盘分区详解【转】
本文装载自:http://blog.csdn.net/aaronychen/article/details/2270048#comments 在学习 Linux 的过程中,安装 Linux 是每一个初 ...
- PMBOK十大知识领域是什么?
PMBOK十大知识领域是:整合管理.范围管理.时间管理.成本管理.质量管理.人力资源管理.沟通管理.风险管理.采购管理.干系人管理. 各用一句话概括项目管理知识体系十大知识领域: 1.整合管理:其作用 ...
- dp专题训练
****************************************************************************************** 动态规划 专题训练 ...