经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
BZOJ2476: 战场的数目(矩阵快速幂)
来源:cnblogs  作者:自为风月马前卒  时间:2018/12/3 10:13:40  对本文有异议

题意

题目链接

Sol

神仙题Orzzz

考虑两边是否有\(1\)

\(f[i]\)表示周长为\(2i\)的方案数

第一种情况:左侧或右侧有一个1,那么把这个1删去,对应的方案数为\(f[i - 1]\)

第二种情况:左侧和右侧都有一个1,把这两个1删去,对应的方案数为\(f[i - 2]\)

第三种情况:左侧右侧都没有1,把最下面一层删去,对应的方案为\(f[i - 1]\)

综上,递推式为\(f[i] = 3f[i - 1] - f[i - 2]\)

最后再减去矩形的方案为\(N / 2 - 1\)

矩阵快速幂优化一下

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 2e5 + 10, mod = 987654321;
  4. inline int read() {
  5. char c = getchar(); int x = 0, f = 1;
  6. while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
  7. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  8. return x * f;
  9. }
  10. int N;
  11. int add(int x, int y) {
  12. if(x + y < 0) return x + y + mod;
  13. return x + y >= mod ? x + y - mod : x + y;
  14. }
  15. int mul(int x, int y) {
  16. return 1ll * x * y % mod;
  17. }
  18. struct Ma {
  19. int m[3][3];
  20. Ma() {
  21. memset(m, 0, sizeof(m));
  22. }
  23. Ma operator * (const Ma &rhs) const {
  24. Ma ans;
  25. for(int k = 1; k <= 2; k++)
  26. for(int i = 1; i <= 2; i++)
  27. for(int j = 1; j <= 2; j++)
  28. ans.m[i][j] = add(ans.m[i][j], mul(m[i][k], rhs.m[k][j]));
  29. return ans;
  30. }
  31. };
  32. Ma MatrixPow(Ma a, int p) {
  33. Ma base;
  34. for(int i = 1; i <= 2; i++) base.m[i][i] = 1;
  35. if(p < 0) return base;
  36. while(p) {
  37. if(p & 1) base = base * a;
  38. a = a * a; p >>= 1;
  39. }
  40. return base;
  41. }
  42. int main() {
  43. while(scanf("%d", &N) && (N)) {
  44. if((N & 1) || (N < 8)) {puts("0"); continue;}
  45. if(N == 8) {printf("2\n"); continue;}
  46. N >>= 1;
  47. Ma a;
  48. a.m[1][1] = 3; a.m[1][2] = - 1;
  49. a.m[2][1] = 1; a.m[2][2] = 0;
  50. a = MatrixPow(a, N - 5);
  51. printf("%d\n", add(add(mul(13, a.m[1][1]), mul(5, a.m[1][2])), -(N - 1)));
  52. }
  53. return 0;
  54. }
  55. /*
  56. 7
  57. 8
  58. 9
  59. 10
  60. 0
  61. */
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号