题目链接

这道题, 给我的最大的知识点就是对于去模运算,一定可以找到循环节,这题只不过是嵌套了两层,可以分别找到循环节。关于这题如何找循环节的,直接暴力,网上也有很多。

找到循环节之后,另一个知识点就是对于线性关系可以使用矩阵快速幂来加速。

附上代码:

 /*************************************************************************
> File Name: 4292.cpp
> Author: Stomach_ache
> Mail: 1179998621@qq.com
> Created Time: 2014年04月20日 星期日 22时06分59秒
> Propose:
************************************************************************/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <fstream> using namespace std; #define MOD1 (1000000007)
#define MOD2 (222222224)
#define MOD3 (183120) typedef long long LL; struct Matrix {
LL a[][];
//Matrix(int m):n(m){memset(a, 0, sizeof(a));}
inline Matrix multiply(Matrix& y, LL mod) {
Matrix ret;
for (LL i = ; i < ; i++) {
for (LL j = ; j < ; j++) {
LL tmp = ;
for (LL k = ; k < ; k++) {
tmp = (tmp+a[i][k]*y.a[k][j])%mod;
//ret.a[i][j] = (ret.a[i][j]+tmp)%mod;
}
ret.a[i][j] = tmp;
}
} return ret;
}
}; Matrix power_mod(Matrix x, LL y, LL mod) {
Matrix ans;
ans.a[][] = ans.a[][] = ;
ans.a[][] = ans.a[][] = ;
while (y) {
if (y % ) {
ans = ans.multiply(x, mod);
}
x = x.multiply(x, mod);
y /= ;
} return ans;
} int main(void) {
Matrix A;
A.a[][] = ;
A.a[][] = ;
A.a[][] = ;
A.a[][] = ;
LL n;
while (cin >> n) {
if (n == || n == ) {
cout << n << endl;
continue;
}
Matrix tmp1 = power_mod(A, n-, MOD3);
n = tmp1.a[][];
if (n != && n != ) {
tmp1 = power_mod(A, n-, MOD2);
n = tmp1.a[][];
}
if (n != && n != ) {
tmp1 = power_mod(A, n-, MOD1);
n = tmp1.a[][];
}
cout << n << endl;
}
return ;
}

最新文章

  1. c#读写txt
  2. JS学习之函数内部属性和方法
  3. as画柱型图的简单算法(关于柱型图宽和间距问题)
  4. 转载--web前端35个jQuery小技巧!
  5. win7如何配置access数据源
  6. php使用redis存储
  7. 【转】CSS中怎么让DIV居中
  8. OC基础-day03
  9. dede列表标签递增数字生成
  10. 【转】android cts failed items
  11. Lua中的一些库(1)
  12. PHPStrom激活方法
  13. 使用 Resharper 快速做适配器
  14. MySQL 基础 备份和恢复
  15. 基于c的简易计算器二
  16. Python自动化之Django中间件
  17. docker + ubuntun 安装show doc
  18. sipp模拟电信运营商VoIP终端测试(SIP协议调试)
  19. 临时变量不能作为非const引用
  20. NPOI读写Excel sheet操作

热门文章

  1. Java通过接口或者抽象类调用方法的时候,怎么知道调用的是哪个实现类里的方法?
  2. [转载] OpenCV2.4.3 CheatSheet学习(四)
  3. Java的一些小知识
  4. Spring Cloud Consul综合整理
  5. angular报错:angular.min.js:118Error: [ng:areq] http://errors.angularjs.org/1.5.8/ng/areq
  6. RandomRowFilter(3)
  7. bootstrap--响应式框架页面环境配置
  8. 如何在TypeScript中使用JS类库
  9. Linux图形界面安装卸载,与命令界面之间的转换
  10. Django REST Framework之分页器