Leading and Trailing(数论)
E - Leading and Trailing
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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
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).
Output
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.
Sample Input |
Output for Sample Input |
5 123456 1 123456 2 2 31 2 32 29 8751919 |
Case 1: 123 456 Case 2: 152 936 Case 3: 214 648 Case 4: 429 296 Case 5: 665 669 |
题意:求n^k前三个数和后三个尾数;
后三个尾数直接快速幂取模就可以了;
前面三个数我们首先看到
假设n = 10^a; 则n^k = 10^(a*k);假设a*k = x.y;
则x表示位数,0.y表示值,用fmod(a*k, 1)可以得到a*k的小数部分。。。。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 100+7;
ll mod = 1000;
ll mod1 = 1e10;
ll n, k;
int lspow(ll a, ll b){
ll ans = 1;
while(b) {
if(b&1) ans = ans*a%mod;
a = a*a%mod;
b >>= 1;
}
return ans;
}
int main()
{
int t, cas = 0;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld", &n, &k);
double x = pow(10 ,fmod(k*log10(1.0*n), 1));
x = x*100.0;
printf("Case %d: %d %03d\n", ++cas, (int)x, lspow(n%mod, k));
}
return 0;
}
上一篇: PHP的戏法方法(简介)