日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

CodeForces - 1543D2 RPD and Rap Sheet (Hard Version)(交互+模拟)

發布時間:2024/4/11 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 1543D2 RPD and Rap Sheet (Hard Version)(交互+模拟) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:交互題猜密碼,設原密碼為 xxx,猜的密碼為 yyy,如果沒猜到,密碼會自適應變成 zzz,滿足 x⊕z=yx \oplus z=yxz=y ,最多猜 nnn 次,對于本題而言,所有數字是在 kkk 進制下進行的

題目分析:相對于 easyeasyeasy 版本而言,當異或推廣到 kkk 進制時,就不存在自反性了,所以本題還是要稍微推一下公式找找共性

這里的異或就不能稱之為異或了,下文中,⊕\oplus 將視為 “k進制不進位加法”,同樣對應一個 ?\ominus? 為 “k進制不退位減法”,意義為名稱所述。本題中的異或其實就是“k進制不進位加法”,需要提前知道的是,這兩個運算同樣具有基本運算的交換律和結合律的性質。

開始推公式,假設原答案為 xxx,猜的密碼為 yyy,新密碼為 zzz,因為滿足 x⊕z=yx \oplus z=yxz=y,兩側同時“不進位減去x”,得到 z=y?xz=y \ominus xz=y?x

假設第 iii 次詢問的值為 qiq_iqi?,密碼為 passwordpasswordpassword,第一次詢問后,原密碼變成了 q1?passwordq_1 \ominus passwordq1??password;第 kkk 次詢問后,原密碼變成了 qk?(qk?1?...(q2?(q1?password)))q_k \ominus(q_{k-1}\ominus...(q_2 \ominus (q_1 \ominus password)))qk??(qk?1??...(q2??(q1??password))),這里還是令 q1=0q_1=0q1?=0

所以假如我們第 iii 次猜,想要猜原密碼是否和 xxx 相等,也就是 password==xpassword==xpassword==x 是否成立,就必須令 qi=qi?1?(qi?2?...(q2?(q1?x)))q_i=q_{i-1} \ominus(q_{i-2}\ominus...(q_2 \ominus (q_1 \ominus x)))qi?=qi?1??(qi?2??...(q2??(q1??x)))

現在問題是如何快速求出 qiq_iqi?。根據結合律,不難將 xxx 單獨拿出來,然后整個式子就變成了:qi=qi?1?(qi?2?...(q2?q1))opxq_i=q_{i-1} \ominus(q_{i-2}\ominus...(q_2 \ominus q_1))\ \ op\ \ xqi?=qi?1??(qi?2??...(q2??q1?))??op??x,這里的 opopop 代表的是一個符號,這里先按下不表。不難看出 opopop 前面的部分可以線性維護得到。而后面單獨拆出來的 xxx,因為遇到減法拆括號時需要變號,“不進位減法” 在這里也是同理,所以當詢問的下標為奇數時,opopop?\ominus?,當詢問的下標為偶數時,opopop⊕\oplus

然后自己模擬一下 ⊕\oplus?\ominus? 這兩個運算就可以了,按照上面的理論,將 nnn 個數字都枚舉一遍, 時間復雜度是 O(nlogkn)O(nlog_kn)O(nlogk?n)

代碼:

// Problem: D2. RPD and Rap Sheet (Hard Version) // Contest: Codeforces - Codeforces Round #730 (Div. 2) // URL: https://codeforces.com/contest/1543/problem/D2 // Memory Limit: 256 MB // Time Limit: 5000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #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<cassert> #include<bitset> #define lowbit(x) x&-x using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; int k; int add(int a,int b) {int ans=0,base=1;while(a||b) {int tmp=(a%k+b%k)%k;ans+=tmp*base;base*=k;a/=k,b/=k;}return ans; } int sub(int a,int b) {int ans=0,base=1;while(a||b) {int tmp=(a%k-b%k+k)%k;ans+=tmp*base;base*=k;a/=k,b/=k;}return ans; } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int w;cin>>w;while(w--) {int n,sum=0;read(n),read(k);for(int i=0;i<n;i++) {int cur=1;if(i==0) {cur=0;} else if(i&1) {cur=sub(sum,i);} else {cur=add(sum,i);}printf("%d\n",cur);fflush(stdout);int ans;scanf("%d",&ans);if(ans==1) {break;} else {sum=sub(cur,sum);}}}return 0; }

總結

以上是生活随笔為你收集整理的CodeForces - 1543D2 RPD and Rap Sheet (Hard Version)(交互+模拟)的全部內容,希望文章能夠幫你解決所遇到的問題。

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