UV-11029-Leading and Trailing (Rapid power + formula deformation)
Idea: the last three digits can be directly used for power modulo, and the first three digits can be used in two ways: double and public. For details, refer to the code.
Note that the last three digits must be 0, I .e. % 03d.
AC code ①:
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define LL long # define INF 1000000000 using namespace std; # define MOD 1000int 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) {// calculate the first three double ret = 1 using double; 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, & n, & k); int ttt = n % 1000; ttt = kmod (ttt, k); double lll = kkmod (double) n, k); lll * = 1000; // maybe lll is less than 1000, maybe less than three while (lll> = 1000) {lll/= 10 ;} /* char str [1234]; sprintf (str, % lf, 1000 * lll); str [3] = ''; * /// you can also output the first three digits // printf (% lf, lll); printf (% d... % 03d, (int) lll, ttt); // remember to use % 03d for the last three digits. Output in strict accordance with the format} return 0 ;}
AC code ②:
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define LL long # define INF 0x7fffffffusing 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, & n, & k); int lll, ttt; lll = kmod (n % 1000, k); ttt = (int) pow (10, 2 + fmod (k * log10 (n), 1 )); // calculate the first three digits of printf (% d... % 03d, ttt, lll);} return 0 ;}