Codeforces Round #633(Div.2) E. Perfect Triples

E. Perfect Triples

Title link

Summary of topics:

There is an infinite array \ (s \) . (Start empty)
Each time you find the three smallest numbers in lexicographic order \ ((a, b, c) \) satisfy:

  • \(a\oplus b\oplus c=0\).
  • \ (a, b, c \ notin s \) .
    \ (t \) times, each time ask the number of items in the array \ (n \) .

Ideas:

After playing the table, we found that the first item of the triple is always a consecutive number after \ (4 ^ x \) . In this way, we try to transfer two bits under two bits
(that is, quaternary, in order to make XOR look convenient, we describe it this way ):
the value that each two bits can take is \ (00, 01, 10, 11 \) .
First assume that the number of \ (1 \ to 4 ^ n-1 \) has been used up , now consider \ (4 ^ n \ to 4 ^ {n + 1} -1 \) .
To get \ (a <b <c \) , the highest two digits can only be \ (01, 10 , 11 \) , which means that the highest two digits of \ (a, b, c \) are different from each other.
Now that the first two are known, let's continue to look down.
Let us first show the items (binary) given in the sample in the form of the following table:

\(a\) \(b\) \(a\oplus b\)
\(00\) \(00\) \(00\)
\(01\) \(10\) \(11\)
\(10\) \(11\) \(01\)
\(11\) \(01\) \(10\)

We boldly guess that (in two-digit units) in quaternary, all the values ​​meet the above conditions.

  • In the case of two digits, \ ((a, b, c) \) only has \ (1,2,3 \) , which is obviously satisfied.
  • Assuming that \ ( a, b, c \) is satisfied under \ (2k \) bits , verify \ (2k + 2 \) bits:
    we first shift this \ (2k \) bit to the left by two (set any of them A pair of values ​​is \ (pa, pb, pc \) , obviously \ (pa \ oplus pb = pc \) ), in the case of the smallest lexicographic order, we first consider \ (a \) :
    \ (00 \) : obviously \ (pa + 00, pb + 00, pc + 00 \) established.
    \ (01 \) : For \ (b \) , \ (00 , 01 \) is not true ( \ (b, c \) repeats), then it should be \ (pa + 01, pb + 10, pc + 11 \) .
    ... the
    rest of the hands will find that the table above is indeed satisfied.

As long as we will Determination \ (n-\) triples the number belongs \ ((a, b, c ) \) a \ (A \) to determine this value.

Code:

#include "iostream"
#include "stdio.h"
#include "string.h"
#include "algorithm"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int N=1e6+5;
const ll mod=998244353;
const double eps=1e-5;
//const double pi=acos(-1);

#define ls p<<1
#define rs p<<1|1
ll f[3][4]={{0,3,1,2},{},{0,2,3,1}};
void solve(ll x,int y)
{
    if(y==1)
    {
        printf("%lld\n",x);
        return;
    }
    ll ans=0,p=1;
    while(x)
    {
        ans=ans+f[y][x%4]*p;
        x>>=2;
        p<<=2;
    }
    printf("%lld\n",ans);
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int _;
    ll n;
    cin>>_;
    while(_--)
    {
        cin>>n;
        ll j=1,a;
        while(j<=n) j<<=2;
        j>>=2;
        if(j+2>=n) a=j;
        else a=j+(n-j)/3;
        solve(a,n%3);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Suiyue-Li/p/12693020.html