标签:快速 line integer ring double 原型 cti ber can
You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of nk.
Input starts with an integer T (≤ 1000), denoting the number of test cases.
Each case starts with a line containing two integers: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107).
For each case, print the case number and the three leading digits (most significant) and three trailing digits (least significant). You can assume that the input is given such that nk contains at least six digits.
分析:后三位直接快速幂取余得,对于体格给定的整数n可以写成n = 10^a形式,其中a是浮点数, n ^ k = (10 ^ a) ^ k = (10 ^ x) * (10 ^ y), 其中x,y分别为ak的整数部分和小数部分,对于t=n^k这个数,他的位数由10^x决定,他的位数上的值由10^y决定。因此我们要求t的前三位,只用求出10^y就可以了。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn = 1e7+5;
const int mod = 1000;
int quickmi(int a, int b)
{
if(b == 0)
return 1;
int tmp = quickmi(a, b>>1);
tmp = tmp * tmp % mod;
if(b & 1)
tmp = tmp * (a % mod) % mod;
return tmp % mod;
}
int main(void)
{
int T, cas;
int n, m;
scanf("%d", &T);
cas = 0;
while(T--)
{
cas++;
scanf("%d%d", &n, &m);
int last = quickmi(n % 1000, m);
double y = 2.0 + fmod(m * log10(n * 1.0), 1);
int first = pow(10.0, y);
printf("Case %d: %03d %03d\n", cas, first, last);
}
return 0;
}
1282 - Leading and Trailing 求n^k的前三位和后三位。
标签:快速 line integer ring double 原型 cti ber can
原文地址:http://www.cnblogs.com/dll6/p/7698498.html