/* * [题意] * 输入n, x, m * 求(1^x)*(x^1)+(2^x)*(x^2)+(3^x)*(x^3)+...+(n^x)*(x^n) * [解题方法] * 设f[n] = [x^n, n*(x^n), (n^2)*(x^n),..., (n^x)*(x^n)] * 则f[n][k] = (n^k)*(x^n) * 问题转化为求:( g[n] = f[1][x]+f[2][x]+...+f[n][x] ) * 设C(i,j)为组合数,即i种元素取j种的方法数 * 所以有:f[n+1][k] = ((n+1)^k)*(x^(n+1)) (二次多项式展开) * = x*( C(k,0)*(x^n) +C(k,1)*n*(x^n)+...+C(k,k)*(n^k)*(x^n) ) * = x*( C(k,0)*f[n][0]+C(k,1)*f[n][1]+...+C(k,k)*f[n][k] ) * 所以得: * |x*1 0................................0| |f[n][0]| |f[n+1][0]| * |x*1 x*1 0............................0| |f[n][1]| |f[n+1][1]| * |x*1 x*2 x*1 0........................0| * |f[n][2]| = |f[n+1][2]| * |......................................| |.......| |.........| * |x*1 x*C(k,1) x*C(k,2)...x*C(k,x) 0...0| |f[n][k]| |f[n+1][k]| * |......................................| |.......| |.........| * |x*1 x*C(x,1) x*C(x,2).......x*C(x,x) 0| |f[n][x]| |f[n+1][x]| * |0................................0 1 1| |g[n-1] | | g[ n ] | */ #include <iostream> #include <cstring> using namespace std; #define M 55 #define LL long long #define FF(i, n) for(int i = 0; i < n; i++) int ans[M], mod, C[M][M]; int ret[M][M], init[M][M]; void ini(int n, int x) { memset(init, 0, sizeof(init)); FF(i, n) FF(j, i+1) init[i][j] = (LL)x*C[i][j] % mod; FF(i, n) ans[i] = x; ans[n] = 0; init[n][n-1] = init[n][n] = 1; } void matmul(int a[][M], int b[][M], int n) { int tp[M][M] = {0}; FF(i, n) FF(k, n) if(a[i][k]) FF(j, n) if(b[k][j]) tp[i][j] = (tp[i][j] + (LL)a[i][k]*b[k][j]) % mod; FF(i, n) FF(j, n) a[i][j] = tp[i][j]; } void matmul(int a[], int b[][M], int n) { int tp[M] = {0}; FF(j, n) if(a[j]) FF(i, n) if(b[i][j]) tp[i] = (tp[i] + (LL)a[j]*b[i][j]) % mod; FF(i, n) a[i] = tp[i]; } void qmod(int n, int b) { FF(i, n) FF(j, n) ret[i][j] = (i==j); for( ; b; b >>= 1) { if (b & 1) matmul(ret, init, n); matmul(init, init, n); } } int main() { int i, j, n, x; while (cin >> n >> x >> mod, n >= 1) { for(i = 0; i <= x; i++) C[i][0] = C[i][i] = 1; for(i = 2; i <= x; i++) for(j = 1; j < i; j++) C[i][j] = ((LL)C[i-1][j-1]+C[i-1][j]) % mod; ini(x+1, x); qmod(x+2, n); matmul(ans, ret, x+2); cout << ans[x+1] << endl; } return 0; }
相关推荐
HDU 1022 Train Problem I 附详细思路
A simple SDK for HDU. hdu-api 是一个集结 HDU 所有教务管理服务的 SDK,提供了一卡通服务、考试、课表、选课和一些公共信息如空闲教室、上课时间等信息的 API。 hdu-api 主要基于 Requests 库和 Beautiful Soup 库...
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫“大明”。 这时他已经不是那个只会做100以内加法的那个“小明”了,现在他甚至会任意长度的正小数的加法。 现在,给你...
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
The first line of the input will contain a single integer indicating the number of problem instances. Each instance will consist of a single line of the form m n1 n2 n3 ... nm where m is the number ...
考试类精品--A simple SDK for HDU. 一个提供一卡通服务、考试、课表、选课和公共信息等 API
HDU的一题........HDU DP动态规
hdu2101AC代码
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu_2102_passed_sorce
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
hdu 1166线段树代码
自己做的HDU ACM已经AC的题目