toj 4613 Number of Battlefields
toj 4613 Number of Battlefields
时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte
总提交: 8 测试通过:6
描述
In the previous problem, we assume the perimeter of the figure equals to p, how many battlefields are possible? For example, there are no battlefields possible forp < 8, but two for p = 8:
Here are the nine battlefields for p=10:
You’re asked to output the number of battlefields modulo 987654321.
输入
There will be at most 25 test cases, each with a single integer p ( 1<=p<=109), the perimeter of the battlefield. The input is terminated by p = 0.
输出
For each test case, print a signle line, the number of battlefields, modulo 987654321.
样例输入
7
8
9
10
0
样例输出
0
2
0
9
/*
本题用快速矩阵幂的方法求斐波那契序列
#include <cstdio>
#include <iostream>using namespace std;const int MOD = 10000;int fast_mod(int n) // 求 (t^n)%MOD
{int t[2][2] = {1, 1, 1, 0};int ans[2][2] = {1, 0, 0, 1}; // 初始化为单位矩阵int tmp[2][2]; //自始至终都作为矩阵乘法中的中间变量while(n){if(n & 1) //实现 ans *= t; 其中要先把 ans赋值给 tmp,然后用 ans = tmp * t{for(int i = 0; i < 2; ++i)for(int j = 0; j < 2; ++j)tmp[i][j] = ans[i][j];ans[0][0] = ans[1][1] = ans[0][1] = ans[1][0] = 0; // 注意这里要都赋值成 0for(int i = 0; i < 2; ++i) // 矩阵乘法{for(int j = 0; j < 2; ++j){for(int k = 0; k < 2; ++k)ans[i][j] = (ans[i][j] + tmp[i][k] * t[k][j]) % MOD;}}}// 下边要实现 t *= t 的操作,同样要先将t赋值给中间变量 tmp ,t清零,之后 t = tmp* tmpfor(int i = 0; i < 2; ++i)for(int j = 0; j < 2; ++j)tmp[i][j] = t[i][j];t[0][0] = t[1][1] = 0;t[0][1] = t[1][0] = 0;for(int i = 0; i < 2; ++i){for(int j = 0; j < 2; ++j){for(int k = 0; k < 2; ++k)t[i][j] = (t[i][j] + tmp[i][k] * tmp[k][j]) % MOD;}}n >>= 1;}return ans[0][1];
}int main()
{int n;while(scanf("%d", &n) && n != -1){printf("%d\n", fast_mod(n));}return 0;
}
*/#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll mod=987654321;ll A[2][2],B[2][2],T[2][2];void pow(int n)
{if(n==0){for(int i=0;i<2;i++)for(int j=0;j<2;j++)B[i][j]=(i==j);return;}if(n&1){pow(n-1);for(int i=0;i<2;i++)for(int j=0;j<2;j++){T[i][j]=0;for(int k=0;k<2;k++)T[i][j]=(T[i][j]+A[i][k]*B[k][j])%mod;}for(int i=0;i<2;i++)for(int j=0;j<2;j++){B[i][j]=T[i][j];}}else{pow(n/2);for(int i=0;i<2;i++)for(int j=0;j<2;j++){T[i][j]=0;for(int k=0;k<2;k++)T[i][j]=(T[i][j]+B[i][k]*B[k][j])%mod;}for(int i=0;i<2;i++)for(int j=0;j<2;j++){B[i][j]=T[i][j];}}
}int main()
{int n;A[0][0]=1; A[0][1]=1;A[1][0]=1; A[1][1]=0;while (scanf("%d", &n) == 1 && n){ll ans=0;if(n&1) ans=0;else if(n<4) ans=0;else{pow(n-4);//从第5个开始ans=B[0][0]-n/2+1;//B[0][0]求出对应p下的方块排列总数,(n/2-1)是相应的形成规则矩形的个数,两者之差即为所求的符合要求的方块个数ans%=mod;if(ans<0)ans+=mod;}printf("%lld\n",ans);}return 0;
}
toj 4613 Number of Battlefields相关推荐
- kuangbin带你飞专题合集
题目列表 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题二 搜索进阶 [kuangbin带你飞]专题三 Dancing Links [kuangbin带你飞]专题四 最短路 ...
- 《算法入门经典大赛——培训指南》第二章考试
UVa特别考试 UVa站点专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge& ...
- 算法学习经典例题整理
陆续会对本篇博客进行更新! 搜索:https://vjudge.net/contest/292597 区间DP:https://vjudge.net/contest/293892 树状背包:https ...
- kuangbin带你飞 专题1-23 题单
kuangbin大神,对于打过ACM比赛的ACMer,无人不知无人不晓. 在此,附上vjudge平台上一位大神整理的[kuangbin带你飞]专题目录链接. [kuangbin带你飞专题目录1-23] ...
- 老鱼的-kuangbin专题题解
kuangbin专题问题一览 专题一 简单搜索 POJ 1321 棋盘问题 POJ 2251 Dungeon Master POJ 3278 Catch That Cow POJ 3279 Flipt ...
- toj 4610 Biggest Number
toj 4610 Biggest Number 时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte 总提交: 25 测试通过:5 描述 You have a maz ...
- toj 3616 Add number (没想到啊~~)
Add number 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte 总提交: 60 测试通过: 21 描述 Employees of Baidu like ...
- toj 4612 A Shooting Game
toj 4612 A Shooting Game 时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte 总提交: 4 测试通过:4 描述 A and B are pl ...
- toj 4615 Tetrahedrons and Spheres
toj 4615 Tetrahedrons and Spheres 时间限制(普通/Java):6000MS/18000MS 内存限制:65536KByte 总提交: 2 测试通过:2 描述 Ther ...
最新文章
- Mac拷贝/复制文件夹路径快捷键
- 让Linux系统开机速度更快的方法
- 已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭
- spring+springmvc+mybatis配置
- 竞争神经网络与SOM神经网络及其在矿井突水水源判别的应用
- C语言sizeof和strlen的含义,用法和区别
- 微信公众平台开发(31)微信第三方登录接口
- How to: Create and Initialize Trace Listeners
- Bootstrap弹出层(modal)垂直居中简单解决方案(无需修改js)
- ArcGISEngine二次开发(2):地图制图
- 湖南计算机专科学院分数线,湖南电子科技职业学院历年录取分数线
- 【产品志】华硕 ARUA 的 ARGB 方案
- 流利阅读12.28 Seriously, Prada, what were you thinking? Why the fashion industry keeps bumbling into rac
- struts2框架深入
- Markdown文档数学公式的使用
- Microsoft SQL Server数据库
- 在Linux上配置DRBD部署
- 数模-化验结果判别及matlab程序,数模-化验结果判别及matlab程序.doc
- Nike+ Fuelband SE不能激活无法连接的请看这里。
- C# Unity3D游戏开发