QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#77195 | #4855. Puzzle: X-Sums Sudoku | JinTianHao | AC ✓ | 118ms | 3548kb | C++17 | 3.0kb | 2023-02-13 10:14:42 | 2023-02-13 10:14:45 |
Judging History
answer
#include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
// #define int long long
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
using namespace std;
template <typename T> void read(T &x){
x=0;char ch=getchar();int fh=1;
while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
x*=fh;
}
template <typename T> void write(T x) {
if (x<0) x=-x,putchar('-');
if (x>9) write(x/10);
putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
write(x);
puts("");
}
int n,m;
long long k;
char d[15];
__int128_t ans;
int pre[35],suf[35];
long long get(long long x,long long y)
{
int a=x/(1<<n),b=x%(1<<n),c=y/(1<<m),d=y%(1<<m);
return (a^d)+(1ll<<m)*(b^c);
}
long long calc(int y,int x)
{
if(x<0) return 0;
for(int i=29;i>=0;--i)
pre[i]=pre[i+1]*2+(x>>(i+1)&1);
for(int i=1;i<=29;++i)
suf[i]=suf[i-1]+((x>>(i-1)&1)<<(i-1));
long long res=0;
for(int i=29;i>=0;--i)
{
int cnt0=(x>>i&1?(pre[i]+1)*(1<<i):pre[i]*(1<<i)+suf[i]+1);
res+=1ll*cnt0*((y>>i&1)<<i);
int cnt1=(x>>i&1?pre[i]*(1<<i)+suf[i]+1:pre[i]*(1<<i));
res+=1ll*cnt1*((y>>i&1^1)<<i);
}
return res;
}
signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int tc;read(tc);
while(tc--)
{
read(n);read(m);scanf("%s",d);read(k);
ans=0;
if(d[0]=='l')
{
long long cnt=get(k-1,0)+1;
int rnd=cnt/(1<<m),rem=cnt%(1<<m);
ans+=(__int128_t)rnd*calc((k-1)/(1<<n),(1<<m)-1);
if(rem) ans+=calc((k-1)/(1<<n),rem-1);
__int128_t sum=0;
if(rnd) sum+=(__int128_t)(1<<m)*calc((k-1)%(1<<n),rnd-1);
sum+=(__int128_t)rem*(((k-1)%(1<<n))^rnd);
ans+=sum*(1<<m);ans+=cnt;
writeln(ans);
}
if(d[0]=='r')
{
long long cnt=get(k-1,(1ll<<n+m)-1)+1;
int rnd=cnt/(1<<m),rem=cnt%(1<<m);
ans+=(__int128_t)rnd*calc((k-1)/(1<<n),(1<<m)-1);
ans+=calc((k-1)/(1<<n),(1<<m)-1)-calc((k-1)/(1<<n),(1<<m)-1-rem);
__int128_t sum=0;
if(rnd) sum+=(__int128_t)(1<<m)*(calc((k-1)%(1<<n),(1<<n)-1)-calc((k-1)%(1<<n),(1<<n)-1-rnd));
sum+=(__int128_t)rem*(((k-1)%(1<<n))^((1<<n)-1-rnd));
ans+=sum*(1<<m);ans+=cnt;
writeln(ans);
}
if(d[0]=='t')
{
long long cnt=get(0,k-1)+1;
int rnd=cnt/(1<<n),rem=cnt%(1<<n);
__int128_t sum=0;
sum+=(__int128_t)rnd*calc((k-1)/(1<<m),(1<<n)-1);
if(rem) sum+=calc((k-1)/(1<<m),rem-1);
ans+=sum*(1<<m);ans+=cnt;
if(rnd) ans+=(__int128_t)(1<<n)*calc((k-1)%(1<<m),rnd-1);
ans+=(__int128_t)rem*(((k-1)%(1<<m))^rnd);
writeln(ans);
}
if(d[0]=='b')
{
long long cnt=get((1ll<<n+m)-1,k-1)+1;
int rnd=cnt/(1<<n),rem=cnt%(1<<n);
__int128_t sum=0;
sum+=(__int128_t)rnd*calc((k-1)/(1<<m),(1<<n)-1);
sum+=calc((k-1)/(1<<m),(1<<n)-1)-calc((k-1)/(1<<m),(1<<n)-1-rem);
ans+=sum*(1<<m);ans+=cnt;
if(rnd) ans+=(__int128_t)(1<<n)*(calc((k-1)%(1<<m),(1<<m)-1)-calc((k-1)%(1<<m),(1<<m)-1-rnd));
ans+=(__int128_t)rem*(((k-1)%(1<<m))^((1<<m)-1-rnd));
writeln(ans);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3404kb
input:
4 2 1 top 1 2 1 bottom 2 2 1 left 3 2 1 right 4
output:
1 34 27 3
result:
ok 4 lines
Test #2:
score: 0
Accepted
time: 2ms
memory: 3392kb
input:
4 11 19 top 1053766555 12 26 top 230781535210 14 10 right 8344647 7 30 right 70120568170
output:
565741033271081135 31719572400444316026492 112693473538824 477453505821905419941
result:
ok 4 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 3384kb
input:
1 1 1 right 1
output:
10
result:
ok single line: '10'
Test #4:
score: 0
Accepted
time: 118ms
memory: 3548kb
input:
100000 20 29 top 257095114618907 13 23 left 54481547025 12 30 left 3617852247968 9 2 top 336 29 5 top 4512541036 11 20 top 1441537922 11 22 bottom 663925222 22 6 bottom 161962542 10 10 top 623031 1 13 right 13855 29 2 right 814001609 7 21 top 258317404 12 16 right 127899460 2 12 right 2608 21 24 bot...
output:
72365878684739299349500249330 588017022058838439666 9662552090602663949581947 239776 37869457663672028144 1548057695947494022 34042524198475305980 14205938085912676 326660065713 102273529 574770226186886766 34663716399392240 27135914299638562 7972435 488424528027186861329373749 852 105967175721525 2...
result:
ok 100000 lines