Topic Transfer: UVA-11029
Idea: The last three bits can be directly and quickly power modulo, then the first three bits can have two approaches, one is the use of double, one is the use of the formula method, the specific look at the code bar
Note that the rear three bit is less than three bits to fill 0, that is, with%03d
AC Code ①:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 1000000000
using namespace std;
#define MOD 1000
int T;
int n, k;
int kmod (int x, int n) {// fast power
int ret = 1;
while (n) {
if (n & 1) ret = (ret * x)% MOD;
x = (x * x)% MOD;
n >> = 1;
}
return ret;
}
double kkmod (double x, int n) {// Use double to find the first three digits
double ret = 1;
while (n) {
if (n & 1) ret = ret * x;
while (ret> = INF) ret / = INF;
x = x * x;
while (x> = INF) x / = INF;
n >> = 1;
}
return ret;
}
int main () {
scanf ("% d", & T);
while (T-) {
scanf ("% d% d", & n, & k);
Ransom
int ttt = n% 1000;
ttt = kmod (ttt, k);
Ransom
double lll = kkmod ((double) n, k);
lll * = 1000; // may lll be less than 1000, maybe less than three
while (lll> = 1000) {
lll / = 10;
}
/ * char str [1234];
sprintf (str, "% lf", 1000 * lll);
str [3] = '\ 0'; * / // You can also output the first three digits like this
// printf ("% lf \ n", lll);
Ransom
printf ("% d ...% 03d \ n", (int) lll, ttt); // Remember to use% 03d for the last three digits, and output it strictly according to the format
}
return 0;
}
AC Code ②:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 0x7fffffff
using namespace std;
int T;
int n, k;
int kmod (int x, int n) {
int ret = 1;
while (n) {
if (n & 1) ret = (ret * x)% 1000;
x = x * x% 1000;
n >> = 1;
}
return ret;
}
int main () {
scanf ("% d", & T);
while (T-) {
scanf ("% d% d", & n, & k);
int lll, ttt;
lll = kmod (n% 1000, k);
ttt = (int) pow (10, 2 + fmod (k * log10 (n), 1));
printf ("% d ...% 03d \ n", ttt, lll);
}
return 0;
}
Uva-11029-leading and Trailing (Fast Power + formula variant)