UVa 11029 Leading and trailing (how do I calculate the initial three-bit and three-bit of the n^k?) )
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 this you can ' t exactly represent-numbers to raised. For example, the C Functionpow (125456, 455) can is represented in double data type format, but your won ' t get all digit s of the result. However we can get at least some satisfaction if we could know few of the leading and trailing. This is the requirement of this problem.
Input
The "a" of input is 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 would fit in the bit integer ANDK would be less than 10000 001.
Output
For each line of input there would be one line of output. It'll be the format LLL ... TTT, where LLL represents the three digits ofn^k and TTT represents the last three digits. You are assured Thatn^k'll contain at least 6 digits.
Sample Input |
Output for Sample Input |
2 123456 1 123456 2 |
123...456 152...936 |
Train of thought: After three bit good, mod=1000 fast power is.
What about the top three? --Logarithmic
The integer portion of the X=LG (n^k), the decimal part of the Y=LG (n^k), then the n^k is determined by Y-because the 10^x is 1000 ... 0. So 10^y is the top three by multiplying the 100-rounding.
Complete code:
/*0.015s*/
#include <cstdio>
#include <cmath>
#define SF scanf
#define PF printf
typedef long long LL;
const int mod = 1000;
ll Pow_mod (int n, int k)
{
if (k = = 0) return 1;
ll temp = Pow_mod (n, k >> 1);
temp = temp * temp% mod;
if (k & 1) Temp = temp * n% mod;///Note that the intermediate operation result will be super int return
temp;
}
int main ()
{
int t, n, K;
Double Intpart;
SF ("%d", &t);
while (t--)
{
sf ("%d%d", &n, &k);
PF ("%d...%03lld\n", (int) POW (10.0, 2.0 + MODF ((double) k * LOG10 (n), &intpart)), Pow_mod (n, k));
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/