HDU1021 Fibonacci Again

题意:

F(0)= 7, F(1) = 11

F(n) = ( F(n-1) + F(n-2) ) (n>=2).

输入n,若F(n)(mod3)为0则输出yes,否则输出no

题解:

如果直接暴力求解,由于n可以达到1,000,000的规模,必定会超时或栈溢出。因此采用找规律的方法。

n               0  1  2  3  4  5  6  7  8  9  10  11  12  13

F(n)mod3    1   2  0  2  2  1  0  1  1  2   0   2   2  1

从n=2开始每隔4个出现一次符合条件的,即将所有的n分为8个一组,每组的第3个(n%8==2)和第7个(n%8==6)符合条件。

代码:

 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     int n;
 5     while(scanf("%d", &n) != EOF)
 6     {
 7         if(n%8==2 || n%8==6)
 8             printf("yes\n");
 9         else
10             printf("no\n");
11     }
12     return 0;
13 }  
时间: 2024-10-06 01:31:14

HDU1021 Fibonacci Again的相关文章

HDU1021 Fibonacci Again 规律

Fibonacci Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 37002 Accepted Submission(s): 17876 Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n

快速幂 HDU-1021 Fibonacci Again , HDU-1061 Rightmost Digit , HDU-2674 N!Again

1.   Fibonacci Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 77116    Accepted Submission(s): 34896 Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11,

hdu1021 数学题 并不是说难,而是数学题那种简单朴素的思想get不到

hdu1021 Fibonacci Again 一个斐波那契数列,求f[n]能否被3 整除. #include<stdio.h> int main() { long n; while(scanf("%ld",&n) != EOF) if (n%8==2 || n%8==6) printf("yes\n"); else printf("no\n"); return 0; } #include <iostream> #

NYOJ 480 Fibonacci Again!

Fibonacci Again! 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 求第n个斐波那契数是否是一个素数,n为整数 f[n]=f[n-1]+f[n-2] (2<n<30) f[1]=3,f[2]=7 输入 输入整数m,0<m<30,输入-1表示结束输入 输出 假设f[m]是素数 则输出Yes,否则输出No, 每行输出占一行. 例子输入 2 3 例子输出 Yes No #include<stdio.h> int f[35]={0

HDU1848 Fibonacci again and again

Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8198    Accepted Submission(s): 3412 Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1)=1;F(2)=2;

hdu 5167 Fibonacci(DFS)

hdu 5167 Fibonacci 问题描述 斐波那契数列的递归定义如下: Fi=???01Fi?1+Fi?2i = 0i = 1i > 1 现在我们需要判断一个数是否能表示为斐波那契数列中的数的乘积. 输入描述 有多组数据,第一行为数据组数T(T≤100,000). 对于每组数据有一个整数n,表示要判断的数字. 0≤n≤1,000,000,000 输出描述 对于每组数据,如果可以输出"Yes",否则输出"No". 输入样例 3 4 17 233 输出样例

HDU 4099 Revenge of Fibonacci

Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 204800/204800 K (Java/Others) Total Submission(s): 2027    Accepted Submission(s): 475 Problem Description The well-known Fibonacci sequence is defined as following: Here w

Fibonacci斐波拉契数列----------动态规划DP

n==10 20 30 40 50 46 体验一下,感受一下,运行时间 #include <stdio.h>int fib(int n){ if (n<=1)     return 1; else            return fib(n-1)+fib(n-2); }int main( ){ int n; scanf("%d",&n); printf("%d\n" ,fib(n) );} 先 n==10 20 30 40 50 46

HUDJ 1021 Fibonacci Again

Fibonacci Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 36028    Accepted Submission(s): 17385 Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n)