YT02-简单数学课堂题-1004 Fibonacci Again -(5.31日-烟台大学ACM预备队解题报告)
2015-06-01 15:02
489 查看
Fibonacci Again
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 62 Accepted Submission(s) : 47
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
Author
Leojay计145-李英杰
但是没有一个数学公式,难死了= =!,我暴力了很多遍还是没有成功,我知道不能用暴力破解,最终才知道有这么个余数公式:
(a+b)%3= (a%3+b%3)%3
有了这个公式,那真是手到擒来了!
这题也体现出ACM与数学那不可分割的关系啊~
一般思路:
#include <iostream> using namespace std; int fib[1000001]; int main() { int i,n; fib[0]=7,fib[1]=11;//这为斐波那契的前两个数 for(i=2;i<1000001;i++) fib[i]=(fib[i-1]%3+fib[i-2]%3)%3;//算出前1000001个斐波那契数是否为3的倍数 while(cin>>n)//输入数 { if(fib ==0) cout<<"yes"<<endl; //当为3的倍数时输出yes else cout<<"no"<<endl; //当不为的倍数时输出no } return 0; }
但是仔细思考过后,才发现这道题有种更简单的方法,写出斐波那契数列的多位数后,你会发现能被整除三的数序列加二以后正好为四的倍数
第二种思路:
#include <iostream> using namespace std; int main() { long n; while(cin>>n) { if(n%4==2) //当输入数的序列加二 为四的倍数时,则此斐波那契数即为 三的倍数 cout<<"yes"<<endl; else cout<<"no"<<endl; } return 0; }
我从这道题上想说的是,数学的思想很重要,编程的逻辑思考,我觉得首先要转化为数学问题,用自己能想到的最简单的数学方法来实现编程,所以说数学很重要。
相关文章推荐
- Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的权利字符串" UserInfo=0x1bad30 {NSLocalizedDescription=未找到应用程序的“aps-environment”的权利字符串}
- Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER 解决方案
- HackerRank - "Detecting Valid Latitude and Longitude Pairs"
- http error 503. the service is unavailable. sharepoint
- Leetcode Problem.172—Factorial Trailing Zeroes
- AIX下的nmon的使用
- 在可以调用OLE之前,必须将当前线程设置为单线程单元(STA)模式,请确保您的Main函数带有STAThreadAttribute
- shrio 权限管理filterChainDefinitions过滤器配置
- httpd: Could not reliably determine the server's fully qualified domain name
- leetcode_Contains Duplicate II_easy
- EAI和ESB
- leetcode_Contains Duplicate_easy
- AEAI CRM客户关系管理V1.0.3版发版说明
- 【译】使用Rails 4.2+ 测试异步邮件系统
- CodeForces 538E Demiurges Play Again(博弈dp)
- mail 命令指定smtp服务器发送邮件
- hdu 1022 Train Problem I (栈的应用)
- DELL服务器装2003系统
- Codeforces PROB training 31.MAY
- DELL 12代 服务器 集成S110 RAID卡 装 2003 32bit系统