The first three digits of the n^k and the last three digits are obtained.
Range: N (2≤n < 231) and K (1≤k≤107).
Top three bits: Set n^k = X
---> lg (N^K) =LG (x)
---> Klg (n) =lg (x)
---> x=10^ (KLGN).
For the first three bits, the integer portion of KLGN greater than 2 can be discarded. Bit=floor (klgn-2), x=10^ (Klgn-bit).
After three bits: Fast power mode 1000.
Code:
#include <iostream> #include <cstdio> #include <cmath>using namespace Std;typedef long Long ll;//n (2≤n < 2^31) and K (1≤k≤10^7). ll Mod_pow (ll A, ll B, ll MoD) {ll res = 1;while (b > 0) {if (b & 1) res = res * A % Mod;a = A * A% mod;b >>= 1;} return res;} int calc (int n, int k) {int bit = floor (k * LOG10 (N)-2), Return floor (POW (k * LOG10 (N)-bit));} int main () {int n, k;int t;scanf ("%d", &t), for (int cas = 1; CAs <= t; ++cas) {scanf ("%d%d", &n, &k); int fir st = Calc (n, k); int last = Mod_pow (n, K, +);p rintf ("Case%d:%3d%03d\n", CAs, first, last);} return 0;}
Lightoj 1282 Leading and Trailing