Codeforces 1339 E. Perfect Triples(打表找规律)

添加链接描述
题目大意:
给定一个构造方法,询问这个数列的第i个值是多少。
解题思路:
直接打表找规律,按三个一行打可以发现第一个数字总是2 x ^x 到2 x + 1 1 ^{x+1}-1 递增,其中x为偶数{0,2,4…},所以对查询的x可以先查询所在行的第一个数字。然后按把这个表按四进制打出来可以很明显的看出规律,假如一行的第一个数四进制是:12230,则第二个数为:23310,第三个数为:31120,就是按位处理如果当前位是0,则不变,如果是1,2,3则加1,超过4就-4就行了。
解题代码:

/*
    除了0  其他如果是i 则第二个是i+1  第三个 i+2 如果大于4 -=4
*/
#include<bits/stdc++.h>
using namespace std;
mt19937 rng_32(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
const int maxn=2e5+10;
bool visit[maxn];
string getFour(ll x)
{
    string ret="";
    while (x)
    {
        ret+='0'+x%4;
        x/=4;
    }
    reverse(ret.begin(),ret.end());
    return ret;
}
void print(string x)
{
    reverse(x.begin(),x.end());
    ll cur=1;
    ll ret=0;
    for (int i=0;i<x.length();i++)
    {
        ret+=(x[i]-'0')*cur;
        cur*=4ll;
    }
    cout<<ret<<" ";
    return ;
}
int main()
{
    int cas;cin>>cas;
    ll t1;
    string a,b,c;
    while (cas--)
    {
        a=b=c="";
        cin>>t1;
        ll tmpt=t1;
        t1=(t1+2ll)/3ll;
        ll cur=1;
        while (true)
        {
            if (t1<=cur)
            break;
            t1-=cur;
            cur*=4ll;
        }
        a = getFour(cur+t1-1ll);
        for (int i=0;i<a.length();i++)
        {
            if (a[i]=='0')
            {
                b+="0";
                c+="0";
            }
            else
            {
                int t=a[i]-'0';
                if (t==1)
                {
                    b+="2";
                    c+="3";
                }
                else if (t==2)
                {
                    b+="3";
                    c+="1";
                }
                else
                {
                    b+="1";
                    c+="2"; 
                }
            }
        }
        if (tmpt%3ll==1ll)
        print(a);
        else if (tmpt%3ll==2)
        print(b);
        else
        print(c);
        cout<<endl;
    }
    return 0;
}
发布了12 篇原创文章 · 获赞 1 · 访问量 327

猜你喜欢

转载自blog.csdn.net/qq_41818939/article/details/105484505