山东省第八届ACM大学生程序设计竞赛 I题 Parity check 数学规律+大数取余+菲波那切数

题意:

求第n项斐波那契数mod2的值, 0 <= n <= 10 1000 ,贼大的数。。。

分析:

n f(n) f(n)%2 n%3
0 0 0 0
1 1 1 1
2 1 1 2
3 2 0 0
4 3 1 1
5 5 1 2
6 8 0 0
7 13 1 1

明显的找规律:0 1 1 ,所以n对3取余,结果为0及输出0,其他的都输出1;但是n比较大,所以加上大数取余,OK啦!

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#define debug cout<<"**********"<<endl;
#define ll long long
using namespace std;
const int maxn = 10000;
const int mod = 1e9+7;
char c[10005];
int a[10005];
int fun(int n)
{
    int rs;
    for(int i=0;i<n-1;i++)
    {
        rs=a[i]%3;
        if(rs)
            a[i+1]+=rs;
    }

    return a[n-1]%3;
}
int main(){
    std::ios::sync_with_stdio(false);
    while(scanf("%s",c)!=EOF){
        int ans = 0;
        for(int i=0;i<strlen(c);i++)
            a[i]=c[i]-'0';
            int k = fun(strlen(c));
            if(k==0){
                ans = 0;
            }
            else{
                ans = 1;
            }
            cout<<ans<<endl;
        memset(a,0,sizeof(a));
        memset(c,0,sizeof(c));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shensiback/article/details/80168245