0
点赞
收藏
分享

微信扫一扫

ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)

原题链接: ​​https://zoj.pintia.cn/problem-sets/91827364500/problems/91827370503​​

ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_斐波拉契数列
测试样例:

Sample Input
6
1 2
1 3
1 4
1 5
123456 12345678987654321
123 20190427201904272019042720190427
Sample Output
0
0
1
0
0
1
Sample Input
6
1 2
1 3
1 4
1 5
123456 12345678987654321
123 20190427201904272019042720190427
Sample Output
0
0
1
0
0
1

题意: 给你一个斐波拉契数列,要你判断从第ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_ios_02项到第ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_ios_03项的奇偶性。注意:ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_思维_04

解题思路: 由于这道题我们并不是要计算第几项的数值,我们只是要判断奇偶性而已。那么对于奇偶性,我们就可以分析一下了,众所周知,斐波拉契数列ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_斐波拉契数列_05,之后的项为ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_#define_06,那么我们发现之后的项数奇偶性完全由前两项决定,而前两项特殊的也都已给出,即第三项为奇+奇为偶,那么之后就是奇+偶为奇,再后面就是偶+奇为奇,再后面就又是奇+奇为偶了。这奇偶性在不断重复。用1表示奇,0表示偶,那这个规律不就是110110110·······,那么我们要求的是从第ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_ios_02项到第ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_ios_03项的奇偶性,这段和我们不就可以用前ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_ios_03项减去前ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_思维_10项来表示吗?那么这个奇偶性也同样是由它们来决定的,我们看表格分析,用ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_ios_11表示第ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_斐波拉契数列_12项的值,ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_斐波拉契数列_13表示前ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_斐波拉契数列_12项的只。

ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_斐波拉契数列_15

ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_ios_16

ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_#define_17

1

1

1

2

1

2

3

2

4

4

3

7

5

5

12

````

经过这张表,我想你们也应该知道其中关系了,没错,由于数列中元素的奇偶规律也必然导致了前缀和的奇偶规律:011011011011,那么我们此题则易解,用前ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_ios_03项减去前ZOJ——4108 Fibonacci in the Pocket(思维好题!!!)_思维_10项来表示我们想要的结果,分析情况即可:奇-偶为奇,奇-奇为偶,偶-奇为奇,偶-偶为偶。这里还要注意的一点就是我们判断奇偶性是通过求余周期获取的,同样,由于数目过大,我们应该要用字符串存储数值,再利用变量去累加位数上的值判断余数。具体看代码。

AC代码:

/*

*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t;
string sa,sb;
int cnt1,cnt2;
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
cnt1=cnt2=0;
cin>>sa>>sb;
int len1=sa.size();
rep(i,0,len1-1){
cnt1+=sa[i]-'0';
}
int len2=sb.size();
rep(i,0,len2-1){
cnt2+=sb[i]-'0';
}
//由于我们要判断中间的奇偶性。
cnt1--;
if((cnt1%3==1&&cnt2%3!=1)||(cnt1%3!=1&&cnt2%3==1))
cout<<1<<endl;
else
cout<<0<<endl;
}
}
return 0;
}


举报

相关推荐

0 条评论