标签:http name vector tab 翻译 enter == des space
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2275 | Accepted: 484 |
Description
Input
Output
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; }
标签:http name vector tab 翻译 enter == des space
原文地址:http://www.cnblogs.com/ZefengYao/p/7814051.html