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

添加链接描述
题目大意:
给定一个构造方法,询问这个数列的第i个值是多少。
解题思路:
直接打表找规律,按三个一行打可以发现第一个数字总是2 x ^x x到2 x + 1 − 1 ^{x+1}-1 x+11 递增,其中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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值