cf1208A A. XORinacci

A. XORinacci
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows:

f(0)=a;
f(1)=b;
f(n)=f(n−1)⊕f(n−2) when n>1, where ⊕ denotes the bitwise XOR operation.
You are given three integers a, b, and n, calculate f(n).

You have to answer for T independent test cases.

Input
The input contains one or more independent test cases.

The first line of input contains a single integer T (1≤T≤103), the number of test cases.

Each of the T following lines contains three space-separated integers a, b, and n (0≤a,b,n≤109) respectively.

Output
For each test case, output f(n).

Example
input

3
3 4 2
4 5 0
325 265 1231232
output
7
4
76
Note
In the first example, f(2)=f(0)⊕f(1)=3⊕4=7.
題意: 求異或斐波那契數列。
**思路:**打表找規律,發現會是3的循環,對應取模輸出即可,可參考以下打表程序和ac代碼。
打表程序:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
typedef long long ll;
const ll mod = 998244353;
int main() {
	ll g[N];
	ll a, b, n;
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%lld%lld%lld", &a, &b, &n);
		ll c = a ^ b;
		g[0] = a, g[1] = b;
		for (int i = 2; i < 100; i++) {
			g[i] = g[i - 1] ^ g[i - 2];
		}
		for (int i = 0; i < 100; i++) {
			printf("%lld\n", g[i]);
		}
	}	
	return 0;
}

ac代碼:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
typedef long long ll;
const ll mod = 998244353;
int main() {
	ll g[N];
	ll a, b, n;
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%lld%lld%lld", &a, &b, &n);
		ll c = a ^ b;
		ll num = n % 3;
		if (num == 1) {
			printf("%lld\n", b);
		} else if (num == 2) {
			printf("%lld\n", c);
		} else {
			printf("%lld\n", a);
		}
	}	
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章