UVA 11885 - Number of Battlefields(斐波那契)
11885 - Number of Battlefields
。。
#include <stdio.h>
#include <string.h> const long long MOD = 987654321;
int p; struct mat {
long long v[2][2];
mat() {memset(v, 0, sizeof(v));}
mat operator *(mat c) {
mat ans;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
ans.v[i][j] = (ans.v[i][j] + v[i][k] * c.v[k][j] % MOD) % MOD;
}
}
}
return ans;
}
mat operator^(int k) {
mat x = *this, ans;
ans.v[0][0] = ans.v[1][1] = 1;
while (k) {
if (k&1)
ans = ans * x;
x = x * x;
k >>= 1;
}
return ans;
}
}; int main() {
while (~scanf("%d", &p) && p) {
if(p % 2) {printf("0\n"); continue;}
mat pri;
pri.v[0][0] = pri.v[0][1] = pri.v[1][0] = 1;
pri = pri^(p - 4);
printf("%lld\n",(pri.v[0][0] - ((long long)p / 2 - 1) + MOD) % MOD);
}
return 0;
}
UVA 11885 - Number of Battlefields(斐波那契)的更多相关文章
- uva 11885 - Number of Battlefields(矩阵高速幂)
题目连接:uva 11885 - Number of Battlefields 题目大意:给出周长p,问多少种形状的周长为p的,而且该图形的最小包围矩阵的周长也是p,不包含矩形. 解题思路:矩阵高速幂 ...
- HDU 1005 Number Sequence【斐波那契数列/循环节找规律/矩阵快速幂/求(A * f(n - 1) + B * f(n - 2)) mod 7】
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Tribonacci UVA - 12470 (简单的斐波拉契数列)(矩阵快速幂)
题意:a1=0;a2=1;a3=2; a(n)=a(n-1)+a(n-2)+a(n-3); 求a(n) 思路:矩阵快速幂 #include<cstdio> #include<cst ...
- python基础----斐波那契数列
python实现斐波那契数列的三种方法 """ 斐波那契数列 0,1,1,2,3,5,8,13,21,... """ # 方法一:while ...
- (斐波那契总结)Write a method to generate the nth Fibonacci number (CC150 8.1)
根据CC150的解决方式和Introduction to Java programming总结: 使用了两种方式,递归和迭代 CC150提供的代码比较简洁,不过某些细节需要分析. 现在直接运行代码,输 ...
- [LeetCode] Fibonacci Number 斐波那契数字
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...
- [Swift]LeetCode509. 斐波那契数 | Fibonacci Number
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...
- UVA 11582 Colossal Fibonacci Numbers! 大斐波那契数
大致题意:输入两个非负整数a,b和正整数n.计算f(a^b)%n.其中f[0]=f[1]=1, f[i+2]=f[i+1]+f[i]. 即计算大斐波那契数再取模. 一开始看到大斐波那契数,就想到了矩阵 ...
- hdu number number number 斐波那契数列 思维
http://acm.hdu.edu.cn/showproblem.php?pid=6198 F0=0,F1=1的斐波那契数列. 给定K,问最小的不能被k个数组合而成的数是什么. 赛后才突然醒悟,只要 ...
随机推荐
- HDU 1166 敌兵布阵 <线段树 单点修改 区间查询>
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Codechef REBXOR
Read problems statements in Mandarin and Russian. Translations in Vietnamese to be uploaded soon. Ni ...
- 【二分答案】【最大流】bzoj1305 [CQOI2009]dance跳舞
http://hzwer.com/1986.html #include<cstdio> #include<algorithm> #include<queue> #i ...
- JQuery获取第几个元素和判断元素在第几个
HTML代码: <ul> <li>jQuery判断当前元素是第几个元素示例</li> <li>jQuery获取第N个元素示例</li> &l ...
- 网络流量工具iftop,ifstat
此文非原创,转自 http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858923.html 介绍 ifstat工具是个网络接口监测工具,比较简单 ...
- iOS: Xcode7安装KSImageNamed插件,自动读取图片名称
官方文档: ## How do I use it? Build the KSImageNamed target in the Xcode project and the plug-in wil ...
- D3.js系列——布局:弦图和集群图/树状图
一.弦图 1.弦图是什么 弦图(Chord),主要用于表示两个节点之间的联系的图表.两点之间的连线,表示谁和谁具有联系. 2.数据 初始数据为: var city_name = [ "北京& ...
- struts2文件上传时获取上传文件的大小
利用struts2框架上传文件时,如果想要获取上传文件的大小可以利用下面的方式进行: FileInputStream ins = new FileInputStream(file); if (ins. ...
- 一起來玩鳥 Starling Framework(6)Juggler、Tween、以及DelayCall
這篇開始來講Starling裡的Animation.Juggle是個簡單的Class,用來控制動畫的進行.他負責管理經由add()加進來的實現IAnimatable介面的物件,然後當Juggler的a ...
- onbeforepaste事件用法
onkeyup="value=value.replace(/[^\d]/g,'')" onbeforepaste="clipboardData.setData('text ...