D. XORinacci

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.

#include 
using namespace std;

int main()
{
    int a, b, n;
    int t;
    cin>>t;
    for(int i=0; i<t; i++)
    {
        cin>>a>>b>>n;
        int c = a ^ b;
        int num = n % 3;
        if (num == 1)
        {
            cout<<b<<endl;
        }
        else if (num == 2)
        {
            cout<<c<<endl;
        }
        else
        {
            cout<<a<<endl;
        }
    }
    return 0;
}

本来这题想暴力过的,发现内存超限,打表找规律发现它是三组一个循环,该题为数学规律题

测试代码

#include
using namespace std;
#define max1 100005
int x[max1];
int main()
{
    int t;
    cin>>t;
    int a,b,n;
    for(int i=0;i<t;i++)
    {
        memset(x,0,sizeof(x));
        cin>>a>>b>>n;
        x[0]=a;
        x[1]=b;
        for(int i=2;i<max1;i++)
        {
            x[i]=x[i-1]^x[i-2];
        }//用数组存下序列
        for(int i=0;i<50;i++)
        {
            cout<<x[i]<<" ";
        }
        cout<<endl;
    }
}

D. XORinacci_第1张图片

你可能感兴趣的:(oj)