uva11029 - Leading and Trailing

11029 - Leading and Trailing

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1970

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 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 integer T<1001, where T represents the number of test cases. Each of the next T lines contains two positive integers, n and kn 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 format 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

Output for Sample Input

2

123456 1

123456 2

123...456

152...936




大致的题意是:要求n的k次方,但是n和k非常大的时候没有办法去求(实际上可以模拟的,暂且不论),所以题目要求的数据只是n的k次方的前三位和后三位。
思路:刚看到题便一眼想到用同余摸定理求出后三位,直接求的话太慢,干脆二分幂吧!但是出题人的用意显然不只是在此,前三位的求法才是卡人的地方,想了半天,最终用double保存n的k次方,相当于缩小了一定倍数,比如,n的k次方等于344958……那么化成double就是0.344……然后取小数点后3位乘以1000即可!
code:
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;
const int mod=1000;
const int M=1000000000;
const int N=0x7fffffff;
ll mi (int n,int m)
{
if (m==1) return n%mod;
if (m%2==0)
return (mi(n,m/2)%1000)*(mi(n,m/2)%mod)%mod;
else
return n*mi(n,m/2)*mi(n,m/2)%mod;
}
double mi2(int n,int m)
{
if (m==0) return 1.0;
double s=mi2(n,m/2)*mi2(n,m/2);
if (m%2)
s*=n;
while (s>M) s/=M;
return s;
}
int main()
{
int t;
cin>>t;
while (t--)
{
int n,k;
cin>>n>>k;
int b=mi(n,k)%mod;
double c=mi2(n,k);
//cout<<c<<endl;
while (c>=1000) c/=10;
int d=c*1000;
while (d>=1000) d/=10;
printf("%d...%03d\n",d,b);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值