当前位置: 代码迷 >> 综合 >> 第八届山东省赛题 I Parity check 【找规律】
  详细解决方案

第八届山东省赛题 I Parity check 【找规律】

热度:13   发布时间:2023-11-11 11:35:41.0

I Parity check

时间限制: 3 Sec  内存限制: 128 MB
提交: 6  解决: 4
[ 提交][ 状态][ 讨论版]

题目描述

Fascinated with the computer games, Gabriel even forgets tostudy. Now she needs to finish her homework, and there is an easy problem:



She is required to calculate f(n)mod 2 for eachgiven n. Can youhelp her?


输入

Multiple test cases. Each test case is aninteger  in asingle line.

输出

For each test case, output the answer of f(n)mod2.

样例输入

2

样例输出

1


刚开始觉得这题是大数啥的。。。然后在队友的指点下发现这题有规律可找。。最后发现这题其实特别简单。。。


首先我们先看一个小程序的测试结果:

#include <bits/stdc++.h>
using namespace std;int F[1000];int main()
{F[1]=1;printf("%d ",F[1]%2);for(int i=2; i<30; i++){F[i]=F[i-1]+F[i-2];printf("%d ",F[i]%2);}return 0;
}

在这个程序中,我们依次输出每一个斐波那契数对2取模的结果:

结果如图:

这个数列是有规律的。。

当n是3的倍数时。F【n】%2==0其余情况下为1。那么在这题中,只需要对n进行讨论即可。只需要判断n是不是3的倍数即可。

上代码

#include <bits/stdc++.h>
using namespace std;
int main()
{char re[1005];while(~scanf("%s",re)){int len=strlen(re);long long sum=0;for(int i=0;i<len;i++){sum+=re[i]-'0';}if(sum%3==0){printf("0\n");}else{printf("1\n");}}return 0;
}




  相关解决方案