The 8th ACM College Student Programming Contest in Shandong Province I question Parity check Math rules + remainder of large numbers + Fibonacci number

Title:

Find the value of the nth Fibonacci number mod2, 0 <= n <= 10 1000 , a huge number. . .

analyze:

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

The obvious rule is: 0 1 1 , so n takes the remainder of 3, the result is 0 and outputs 0, and the others all output 1; but n is relatively large, so add a large number to take the remainder, OK!

Code:

#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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325401299&siteId=291194637