HDOJ1021题 Fibonacci Again 应用求模公式
2015-08-21 17:53
465 查看
Problem Description
There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).
Input
Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000).
Output
Print the word “yes” if 3 divide evenly into F(n).
Print the word “no” if not.
Sample Input
0
1
2
3
4
5
Sample Output
no
no
yes
no
no
no
应用求模公式
(1) (a + b) % p = (a % p + b % p) % p
(2) (a - b) % p = (a % p - b % p) % p
(3) (a * b) % p = (a % p * b % p) % p
(4) a ^ b % p = ((a % p)^b) % p
如果不用的话会溢出。
代码:
There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).
Input
Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000).
Output
Print the word “yes” if 3 divide evenly into F(n).
Print the word “no” if not.
Sample Input
0
1
2
3
4
5
Sample Output
no
no
yes
no
no
no
应用求模公式
(1) (a + b) % p = (a % p + b % p) % p
(2) (a - b) % p = (a % p - b % p) % p
(3) (a * b) % p = (a % p * b % p) % p
(4) a ^ b % p = ((a % p)^b) % p
如果不用的话会溢出。
代码:
#include <iostream> #include <stdio.h> #include <stdlib.h> #include<string.h> using namespace std; int main() { int a[1000001],i,j,s; a[0]=7;a[1]=11; for(i=2;i<1000001;i++) { a[i]=(a[i-1]%3+a[i-2]%3)%3;//只写最后那个%3也可以 } while(~scanf("%d",&s)) { if(a[s]%3==0) printf("yes\n"); else printf("no\n"); } return 0; }
相关文章推荐
- Container With Most Water
- Netty之BIO(同步阻塞IO)、PIO(伪异步阻塞IO)、NIO(异步非阻塞IO)、AIO(异步非阻塞IO)、Netty
- poj 1273 Drainage Ditches
- hdu 4300 Clairewd’s message(kmp/扩展kmp)
- hdu 5407 CRB and Candies(组合数+最小公倍数+素数表+逆元)2015 Multi-University Training Contest 10
- vs2010下配置CUDA出现kernel launch failed问题,内核无效
- Windows Server 2012如何部署Domain Controller
- WinMain与wWinMain,win32的字符集问题
- RAID
- hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)
- INSTALL_FAILED_OLDER_SDK
- INSTALL_FAILED_INSUFFICIENT_STORAGE
- 画布Canvas与画笔Paint
- retain,copy,assign,strong,weak,autorelease,nonatomic,atomic等得使用及区别
- POJ - 3846 Mountain Road 动归
- cassandra中对节点失败与否的探测方法, the Phi accrual Failure Dector,附论文
- 血族 第一季 The Strain Season 1 (2014)
- 模仿Airbnb的悬浮搜索框动画
- pygal and matplotlib(again)
- org.hibernate.LazyInitializationException: failed to lazily initialize a collection..的解决方案