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的更多相关文章

  1. Uva 11029 Leading and Trailing (求n^k前3位和后3位)

    题意:给你 n 和 k ,让你求 n^k 的前三位和后三位 思路:后三位很简单,直接快速幂就好,重点在于如何求前三位,注意前导0 资料:求n^k的前m位 博客连接地址 代码: #include < ...

  2. 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 ...

  3. lightoj 1282 && uva 11029

    Leading and Trailing lightoj 链接:http://lightoj.com/volume_showproblem.php?problem=1282 uva 链接:http:/ ...

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

    http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS     Me ...

  5. 【LightOJ1282】Leading and Trailing(数论)

    [LightOJ1282]Leading and Trailing(数论) 题面 Vjudge 给定两个数n,k 求n^k的前三位和最后三位 题解 这题..真的就是搞笑的 第二问,直接输出快速幂\(m ...

  6. Leading and Trailing (数论)

    Leading and Trailing https://vjudge.net/contest/288520#problem/E You are given two integers: n and k ...

  7. Leading and Trailing(数论/n^k的前三位)题解

    Leading and Trailing You are given two integers: n and k, your task is to find the most significant ...

  8. E - Leading and Trailing 求n^k得前三位数字以及后三位数字,保证一定至少存在六位。

    /** 题目:E - Leading and Trailing 链接:https://vjudge.net/contest/154246#problem/E 题意:求n^k得前三位数字以及后三位数字, ...

  9. LightOJ1282 Leading and Trailing —— 指数转对数

    题目链接:https://vjudge.net/problem/LightOJ-1282 1282 - Leading and Trailing    PDF (English) Statistics ...

随机推荐

  1. C/C++之全局、static对象/变量的初始化问题

    关于全局.static对象/变量的初始化问题 1. 全局变量.static变量的初始化时机:main()函数执行之前(或者说main中第一个用户语句执行之前). 2. 初始化顺序. 1)全局对象.外部 ...

  2. 文件系统、服务、防火墙、SELINUX——安全四大金刚

    一提到安全,大家都会想到防火墙,和文件系统权限.而实际工作环境中,我们在Linux的安全配置,会涉及到四个级别.我们思考一个场景,你要在百度盘中存放一个文件,这个动作需要考虑下面四个权限. 1 fir ...

  3. c++的各种类型转换方式

    const_cast 用于去掉const属性,把const类型的指针变为非const类型的指针,如:const int *fun(int x,int y){} int *ptr=const_cast& ...

  4. UNIX网络编程--简介(一)【转】

    本文转载自:http://blog.csdn.net/yusiguyuan/article/details/11760187 一.概述 a) 在编写与计算机通信的程序时,首先要确定的就是和计算机通信的 ...

  5. 关于jquery所有动画都有速度和动画的方向(在宽度方向上的动画)?

    不只是jquery的 animate 动画, 才有时间的 参数, 实际上, 在所有的动画中, 包括: show/hide/toggle, slideup/slidedown/slidetoggle, ...

  6. 51nod 1413 权势二进制

    本来刚开始还是想用每一位 -1的个数 然后再乘以10  不断累加 后来发现 完全不是这回事啊  因为本身就是0 和 1 所以只要记录出现的最大的数字 就是答案  因为 n >= 1 // 所以不 ...

  7. Educational Codeforces Round 56 (Rated for Div. 2)

    涨rating啦.. 不过话说为什么有这么多数据结构题啊,难道是中国人出的? A - Dice Rolling 傻逼题,可以用一个三加一堆二或者用一堆二,那就直接.. #include<cstd ...

  8. Spring batch的学习

    Spring batch是用来处理大量数据操作的一个框架,主要用来读取大量数据,然后进行一定处理后输出成指定的形式. Spring batch主要有以下部分组成: JobRepository     ...

  9. org.apache.shiro.session.InvalidSessionException: java.lang.IllegalStateException: getAttribute: Session already invalidated] with root cause

    1.遇到以下异常,找了好长时间,终于解决,报的异常如下: 七月 07, 2017 3:02:16 下午 org.apache.catalina.core.StandardWrapperValve in ...

  10. The way to Go(7): 常量

    Reference: Github: Go Github: The way to Go 常量 常量使用关键字 const 定义,用于存储不会改变的数据. const identifier [type] ...