日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 1339E Perfect Triples(打表找规律)

發布時間:2024/4/11 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 1339E Perfect Triples(打表找规律) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:序列 s 是一個無限數列,現在給出構造方法:

  • 選擇三個數 a b c ,將其依次加到序列 s 的最后面,三個數需要滿足:
  • a b c 在序列 s 中均未出現過
  • a b c 是字典序最小的數列
  • a ^ b ^ c = 0
  • 現在給出一個 n ( <= 1e16 ) ,求出數列的第 n 項

    題目分析:乍一看可能沒什么思路,不過看起來可以打表,于是打表觀察一下,打表代碼放在最后

    打出表后可以發現,以 a b c 為整體的數對被分成了好幾大段,每一段的長度分別為 1,4,16,64....4^n,其中在每一大段中,a 都是依次增大的,這是一個比較明顯的規律

    因為這是對于異或的操作,所以將所有數都轉換為二進制再看一看,會發現 b 和 a 有著某種微妙的關系,仔細觀察觀察或者大膽猜想一下,可以知道這個與四進制有關

    將所有數轉換為四進制后,就能看出 a 與 b 其中的相同位上:

  • a == 0?時 b =0
  • a == 1?時 b = 2
  • a == 2 時 b = 3
  • a == 3 時 b = 1
  • 這樣就能在知道 n 的基礎上,求出第 n 個數的行與列的坐標,然后求出 a 和 b ,根據 a^b^c=0,得到 c=a^b,這樣就能求出答案了

    代碼:

    AC代碼:

    #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<unordered_map> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e5+100;const int b[]={0,2,3,1};LL get_num1(LL x) {LL base=1;while(base<x){x-=base;base<<=2;}return base+x-1; }LL get_num2(LL x) {LL num1=get_num1(x);LL ans=0;for(int i=0;i<=60;i+=2)ans+=(1LL<<i)*b[(num1>>i)%4];return ans; }int main() { #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif // ios::sync_with_stdio(false);int w;cin>>w;while(w--){LL n;scanf("%lld",&n);LL row=(n-1)/3+1;int col=(n-1)%3;LL num1=get_num1(row);LL num2=get_num2(row);LL num3=num1^num2;if(col==0)printf("%lld\n",num1);else if(col==1)printf("%lld\n",num2);elseprintf("%lld\n",num3);}return 0; }

    打表代碼:

    #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<unordered_map> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e5+100;bool vis[N];void print(int num)//輸出四進制 {string ans;for(int i=1;i<=5;i++){ans+=to_string(num%4);num/=4;}reverse(ans.begin(),ans.end());cout<<ans<<' '; }int main() { #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif // ios::sync_with_stdio(false);for(int k=1;k<=100;k++){for(int i=1;i<=1000;i++){if(!vis[i]){for(int j=i+1;j<=1000;j++){if(!vis[j]&&!vis[i^j]){vis[i]=vis[j]=vis[i^j]=true;printf("%d %d %d ",i,j,i^j);print(i),print(j),print(i^j);puts("");goto end;}}}}end:;}return 0; }

    ?

    總結

    以上是生活随笔為你收集整理的CodeForces - 1339E Perfect Triples(打表找规律)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。