题目链接:

  Problem  2198  快来快来数一数

题目描述:

  给出n个六边形排成一排,a[i]代表i个六边形能组成的生成树个数,设定s[i]等于a[1]+a[2]+a[3]+....+a[i-1]+a[i],问s[n]为多少?

解题思路:

  n取值范围[1, 1018],打表内存不够,然后就要考虑快速幂咯!纳尼!!!!快速幂写出来竟然超时,敢信?果然还是见题太少了。(GG)

  对于a[n] = 6*a[n-1] - a[n-2],可以很明显看出。

  然后求和的时候就要化简一番了,但是并不是很难,最终的公式是:s[n] = 6*s[n-1] - s[n-2] + 5;然后构造矩阵,预处理构造矩阵,跑快速幂即可!!

代码:

 #include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
using namespace std; #define LL long long
const LL maxn = ;
const LL mod = ;
struct mat
{
LL col, row;
LL p[maxn][maxn];
} pp[]; mat mul (mat a, mat b);
mat pow (LL n, LL res, mat b); int main ()
{
LL n, t;
mat b;
scanf ("%lld", &t);
memset (pp[].p, , sizeof(pp[].p));
memset (b.p, , sizeof(b.p));
pp[].col = pp[].row = b.row = maxn;
b.col = ;
pp[].p[][] = ;
pp[].p[][] = -;
pp[].p[][] = pp[].p[][] = pp[].p[][] = ;
b.p[][] = ;
b.p[][] = ;
b.p[][] = ;
for (int i=; i<; i++)
pp[i] = mul(pp[i-], pp[i-]); while (t --)
{
mat a;
scanf ("%lld", &n);
a = pow(n-, , b);
printf ("%lld\n", a.p[][] % mod);
} return ;
} mat mul (mat a, mat b)
{
mat c;
c.col = a.col;
c.row = b.row;
memset (c.p, , sizeof(c.p));
for (int k=; k<a.row; k++)
for (int i=; i<a.col; i++)
{
if (a.p[i][k] == ) continue;
for (int j=; j<b.row; j++)
{
if (b.p[k][j] == ) continue;
c.p[i][j] = (c.p[i][j] + a.p[i][k] * b.p[k][j] + mod) % mod;
}
}
return c;
} mat pow (LL n, LL res, mat b)
{
while (n)
{
if (n % )
b = mul (b, pp[res]);
res ++;
n /= ;
}
return b;
}

写的好丑!不要喷我 (捂脸逃~~~~)

最新文章

  1. java实现甘特图的2种方法:SwiftGantt和Jfree (转)
  2. webapi 通过dynamic 接收可变参数
  3. DataGrid--多记录CRUD
  4. codeforce好地方啊 Bear and Elections *
  5. python和pywin32实现窗口查找、遍历和点击
  6. 检测到有潜在危险的 Request.Form 值。 说明: ASP.NET 在请求中检测到包含潜在危险的数据
  7. WP7 MD5加密
  8. PHP CLI模式开发(转)
  9. 优酷播放器demo
  10. hive left outer join的问题
  11. ORACLE用户表空间使用情况查询
  12. jquery 中dataTable显示加载中,图片或文字
  13. java基础(持续整理)
  14. ECMAScript 原始类型
  15. js中,for循环里面放ajax,ajax访问不到变量以及每次循环获取不到数据问题总结
  16. AI,大数据,复杂系统 最精 40本大书单
  17. JS实现简单的运行代码 &amp; 侧边广告
  18. ios蓝牙自定义快捷键
  19. HashMap的长度为什么要是2的n次方
  20. 09 ORM 多表操作,创建表,添加记录

热门文章

  1. 理解和使用WPF 验证机制
  2. (白书训练计划)UVa 11572 Unique Snowflakes(窗体滑动法)
  3. 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决的方法
  4. adb常用命令整理
  5. locate和grep命令
  6. bzoj2436: [Noi2011]Noi嘉年华
  7. java 简单贪吃蛇
  8. Silverlight中使用MVVM(4)
  9. UIFont 字体样式 [UIFont fontWithName~];
  10. angularJS ng-if的用法