HDOJ 2294 - Pendant(DP+矩阵快速幂)
来源:互联网 发布:网络拓扑结构连线最短 编辑:程序博客网 时间:2024/06/05 17:44
题目链接
题意:有个高富帅,要送个很装逼的吊坠给他女朋友。他有k种珠子,然后要串成一个珠子个数小于等于n 的链子(k种珠子都必须要用到)。输入n和k,输出他可以做出多少种不一样的项链。
思路:可以想到递推式f(x, y) = f(x – 1, y) * y + f(x – 1, y – 1) * (k – y + 1)(表示x个珠子用了y种类型),因为n过大,无法直接求出来,所以要使用矩阵快速幂。
GG队友的思路
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;typedef long long ll;const int MOD = 1234567891;const int MAXN = 35;struct mat{ ll s[MAXN][MAXN]; mat() { memset(s, 0, sizeof(s)); } mat operator * (const mat& c) { mat ans; memset(ans.s, 0, sizeof(ans.s)); for (int i = 0; i < MAXN; i++) for (int j = 0; j < MAXN; j++) for (int k = 0; k < MAXN; k++) ans.s[i][j] = (ans.s[i][j] + s[i][k] * c.s[k][j]) % MOD; return ans; } void put() { for (int i = 0; i < MAXN; i++) { for (int j = 0; j < MAXN; j++) printf("%lld ", s[i][j]); printf("\n"); } }}c;ll n;int k;void init() { memset(c.s, 0, sizeof(c.s)); c.s[0][0] = 1; c.s[k][0] = 1; for (int i = 1; i < k + 1; i++) { c.s[i][i] = i; if (i > 1) c.s[i - 1][i] = k - i + 1; } }mat pow_mod(ll n) { if (n == 1) return c; mat a = pow_mod(n / 2); mat ans = a * a; if (n % 2) ans = ans * c; return ans;}int main() { int cas; scanf("%d", &cas); while (cas--) { scanf("%lld%d", &n, &k); init(); mat ans = pow_mod(n); printf("%lld\n", k * ans.s[1][0] % MOD); } return 0;}
0 0
- HDOJ 2294 - Pendant(DP+矩阵快速幂)
- HDU 2294 Pendant DP+矩阵快速幂
- hdu 2294 Pendant (dp+矩阵快速幂)
- HDU 2294 Pendant (DP+矩阵快速幂降维)
- hdu 2294 Pendant dp+矩阵
- HDU - 2294 Pendant (DP滚动数组降维+矩阵快速幂)
- hdu 2294 Pendant DP+矩阵优化
- HDOJ 2294 - Pendant
- Aoj 92 Pendant (DP+矩阵)
- 【HDU2294】Pendant-DP矩阵优化
- hdu 2294 Pendant(矩阵乘法)
- HDU2294 Pendant (齐次线性方程的矩阵解法 + 矩阵快速幂)
- hdoj 5015 构造矩阵+矩阵快速幂
- HDU2294 Pendant 矩阵应用
- hdu2294---Pendant (矩阵)
- POJ3734Blocks DP&&矩阵快速幂
- hdu 2294 Pendant (动态规划+矩阵) 解题报告
- 【矩阵快速幂】 HDOJ 4990 Reading comprehension
- Qt开发应用程序Debug与Release设置 .
- XTU_1184 A Love Letter
- css抖动的样式源码
- 当单元格中的内容长度大于表列的宽度时显示省略号.
- unity的本地化存储PlayerPrefs
- HDOJ 2294 - Pendant(DP+矩阵快速幂)
- POJ 3253
- 虚拟桌面的本质论
- Linux环境下段错误的产生原因及调试方法小结
- 『ANDROID』 在Android开发过程中,常Android 更新UI的两种方法——handler和runOnUiThread()
- 堆和栈的区别
- openwrt 3G无线上网卡拨号--以华为EC189为例
- cxf整合spring
- 阿里面试重点总结