poj 2720 Last Digits
Posted ZefengYao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2720 Last Digits相关的知识,希望对你有一定的参考价值。
Last Digits
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2275 | Accepted: 484 |
Description
Exponentiation of one integer by another often produces very large results. In this problem, we will compute a function based on repeated exponentiation, but output only the last n digits of the result. Doing this efficiently requires careful thought about how to avoid computing the full answer.
Given integers b, n, and i, we define the function f(x) recursively by f(x) = bf(x-1) if x > 0, and f(0)=1. Your job is to efficiently compute the last n decimal digits of f(i).
Given integers b, n, and i, we define the function f(x) recursively by f(x) = bf(x-1) if x > 0, and f(0)=1. Your job is to efficiently compute the last n decimal digits of f(i).
Input
The input consists of a number of test cases. Each test case starts with the integer b (1 <= b <= 100) called the base. On the next line is the integer i (1 <= i <= 100) called the iteration count. And finally, the last line contains the number n (1 <= n <= 7), which is the number of decimal digits to output. The input is terminated when b = 0.
Output
For each test case, print on one line the last n digits of f(i) for the base b specified. If the result has fewer than n digits, pad the result with zeroes on the left so that there are exactly n digits.
Sample Input
2 4 7 10 10 6 3 10 7 0
Sample Output
0065536 000000 4195387
翻译:有递归式f(x) = bf^(x-1),其中f(0)=1,给定b,x值,求f(x)的后n位数
思路:如果f(x)值足够小,直接进行计算即可,对于数值较大的f(x)无法尝试存储整个值,可以运用以下公式对f(x)的后n位数进行递归的求解
AC代码:
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<vector> #include<cstring> #include<string> #include<bitset> using namespace std; #define INF 0x3f3f3f3f #define MOD 1000000000 typedef long long ll; const int N_MAX = 100+3; int b, i, n; int ten_mod[8];//mod表 int fb[N_MAX][N_MAX];//fb[b][i]==b^f(i)=f(i+1) int res[N_MAX][N_MAX];//存储结果,res[b][i] int power(int x,int n) { if (n==-1)return -1;//!!! int res = 1; for (int i = 0; i < n;i++) { res *= x; if (res >= ten_mod[7]) return -1;//!!! } return res; } void prep(){//预处理 memset(res, -1, sizeof(res)); ten_mod[0] = 1; for (int i = 1; i < 8;i++) { ten_mod[i] =ten_mod[i-1]*10; } for (int i = 1; i < N_MAX;i++) { fb[i][0] = 1; for (int j = 1; j < N_MAX;j++) { fb[i][j] = power(i, fb[i][j - 1]);//如果数字大于1e7,则存储-1 } } } ll mod_pow(ll x,ll n,ll p) {//快速幂 ll res = 1; while (n) { if (n & 1)res=res*x%p; x =x* x%p; n >>= 1; } return res; } int eular_phi(int n) {//求欧拉函数 int res = n; for (int i = 2; i*i <= n;i++) { if (n%i == 0) { res = res / i*(i - 1); for (; n%i == 0; n /= i); } } if (n != 1)res = res / n*(n - 1); return res; } int calc(int b,int x,int mod) {//递归计算b^f(x-1) mod m if (mod == 1)return 0;//任意数模1为0 if (x == 0)return 1; if (fb[b][x] < 0) {//运用公式 int eu = eular_phi(mod); return mod_pow(b, calc(b, x - 1, eu) + eu, mod); } else { return fb[b][x] % mod; } } int solve() { int ans; if (res[b][i] < 0) { if (b == 1)ans = 1; else {//!! ans = calc(b, i, ten_mod[7]); } res[b][i] = ans; } ans = res[b][i]%ten_mod[n]; return ans; } int main() { prep(); char format[] = "%00d\\n"; while (scanf("%d",&b)&&b) { scanf("%d%d",&i,&n); format[2] = char(n + \'0\'); printf(format,solve()); } return 0; }
以上是关于poj 2720 Last Digits的主要内容,如果未能解决你的问题,请参考以下文章
poj3373--Changing Digits(DFS+剪枝///记忆化)