山東省第八屆ACM大學生程序設計競賽 I題 Parity check 數學規律+大數取餘+菲波那切數

題意:

求第n項斐波那契數mod2的值,0<=n<=101000 ,賊大的數。。。

分析:

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;
}
發佈了86 篇原創文章 · 獲贊 325 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章