uva11029 - Leading and Trailing

            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 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 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 format LLL . . . T T T, where
LLL represents the first three digits of n
k and T T T represents the last three digits of n,k. You areassured 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次方的前三位和后三位

tip:后三位取余%1000,前三位这样:如x=123456=1.23456*10^5,则 log10(x)=log10(1.23456)+5 ; log10(1.23456)=y; 10^y=1.23456 

  前三位即是10^y*100;注意后三位可能在前面有0的存在。

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #define ll long long
 6 
 7 using namespace std;
 8 
 9 /*ll power1(ll a,ll b)
10 {
11     int ans=a;
12     for(int i=1;i<b;i++)
13         ans=ans*a%1000;
14     return ans%1000;
15 }*/
16 ll power1(ll a,ll n)  ///二分pow
17 {
18     ll ans=1;
19     while(n)
20     {
21         if(n&1) ///为奇
22             ans=ans*a%1000;
23         a=a*a%1000;
24         n/=2;
25     }
26     return ans%1000;
27 }
28 
29 ll power2(ll a,ll b)
30 {
31     ll p,q,ans;
32     double f=b*log10(a);
33     q=(ll)f;///整数部分
34     p=(ll)(f*10000000)-q*10000000;///小数部分*10000000
35     double x=1.0*p/10000000;
36     ans=(ll)(pow(10,x)*100);
37     return ans;
38 }
39 
40 int main()
41 {
42     ll n,k;
43     int t;
44     cin>>t;
45     while(t--)
46     {
47         cin>>n>>k;
48         ll p=power2(n,k),q=power1(n,k);
49         printf("%lld...%03lld\n",p,q);
50     }
51     return 0;
52 }

 

转载于:https://www.cnblogs.com/moqitianliang/p/4681256.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值