NEFU115 斐波那契的整除(数论水题)
链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=115
题目
Time Limit:1000ms Memory Limit:65536K
Description
已知斐波那契数列有如下递归定义,f(1)=1,f(2)=1, 且n>=3,f(n)=f(n-1)+f(n-2),它的前几项可以表示为1, 1,2 ,3 ,5 ,8,13,21,34…,现在的问题是想知道f(n)的值是否能被3和4整除,你知道吗?
Input
输入数据有若干组,每组数据包含一个整数n(1< n <1000000000)。
Output
对应每组数据n,若 f(n)能被3整除,则输出“3”; 若f(n) 能被4整除,则输出“4”;如果能被12整除,输出“YES”;否则输出“NO”。
Sample Input
4
6
7
12
Sample Output
3
4
NO
YES
分析
数据范围1e9,所以直接暴力必然T。由整除性质,可以打表寻找循环节。
寻找得
fn
当且仅当n可以被4整除时可以被3整除。同理当且仅当n可以被6整除时可以被4整除,当且仅当n可以被12整除时可以被12整除。
源码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<utility>
#include<sstream>
#define mem0(x) memset(x,0,sizeof x)
#define mem1(x) memset(x,1,sizeof x)
#define mem11(x) memset(x,-1,sizeof x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x7fffffff;
const int MAXN = 1e6+10;
const int MOD = 1000000007;
int main(){
/*#ifdef LOCAL
freopen("C:\\Users\\JuneLynn Bian\\Desktop\\in.txt","r",stdin);
freopen(" C:\\Users\\JuneLynn Bian\\Desktop\\out.txt","w",stdout);
#endif // LOCAL*/
int n;
while(cin >> n){
if(n%12==0)
cout << "YES" << endl;
else{
if(n%4==0)
cout << "3" << endl;
else{
if(n%6==0)
cout << "4" << endl;
else
cout << "NO" << endl;
}
}
}
return 0;
}
今后开始努力搞数论图论了,立一道数论题作为Rush的开端好了 = ̄ω ̄=