码迷,mamicode.com
首页 > 其他好文 > 详细

uva11029 - Leading and Trailing

时间:2015-07-27 22:53:04      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

            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 }

 

uva11029 - Leading and Trailing

标签:

原文地址:http://www.cnblogs.com/moqitianliang/p/4681256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!