QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#605613 | #8932. Bingo | mitthu | WA | 3ms | 24084kb | C++17 | 4.6kb | 2024-10-02 18:11:07 | 2024-10-02 18:11:07 |
Judging History
answer
#include<bits/stdc++.h>
const int maxn=1e6+50;
using namespace std;
struct node{
int a[maxn],n;
void clear(){
for(int i=0;i<=n;i++)a[i]=0;
n=0;
}
void print(){
for(int i=n;i>=1;i--)printf("%d",a[i]);
puts("");
}
}A,B,zero,one,ans,tmp;
node operator+(const node &x,const node &y){
node z;z.n=max(x.n,y.n);
for(int i=0;i<=z.n+1;i++)z.a[i]=0;
for(int i=1;i<=z.n;i++){
z.a[i]+=x.a[i]+y.a[i];
z.a[i+1]+=z.a[i]/10;
z.a[i]%=10;
}
if(z.a[z.n+1])z.n++;
return z;
}
node operator*(const node &x,const int &y){
node z;z.n=x.n;
for(int i=0;i<=z.n+20;i++)z.a[i]=0;
long long res=0;
for(int i=1;i<=z.n;i++){
res+=1LL*x.a[i]*y;
z.a[i]=res%10;
res/=10;
}
while(res)z.a[++z.n]=res%10,res/=10;
return z;
}
node operator/(const node &x,const int &y){
node z;z.n=x.n;
for(int i=0;i<=z.n;i++)z.a[i]=0;
long long res=0;
for(int i=z.n;i>=1;i--){
res=res*10+x.a[i];
z.a[i]=res/y;
res%=y;
}
while(z.a[z.n]==0&&z.n>0)z.n--;
return z;
}
bool operator <= (const node &x,const node &y){
if(x.n!=y.n)return x.n<y.n;
for(int i=x.n;i>=1;i--){
if(x.a[i]<y.a[i])return true;
if(x.a[i]>y.a[i])return false;
}
return true;
}
int check(int l,int r){
int cnt=0;
for(int i=l;i<=r;i++)
if(B.a[i-l+1]==A.a[i])cnt++;
if(cnt==r-l+1)return 0;
for(int i=r;i>=l;i--)
if(B.a[i-l+1]>A.a[i])return 1;
else if(B.a[i-l+1]<A.a[i])return 2;
return 2;
}
node make_1(int x){
node z;z.n=A.n;
int r=x+B.n-1;
for(int i=1;i<x;i++)z.a[i]=0;
for(int i=x;i<=r;i++)z.a[i]=B.a[i-x+1];
z.a[r+1]++;
for(int i=r+1;i<=z.n;i++)
if(z.a[i]>=10)z.a[i+1]++,z.a[i]-=10;
if(z.a[z.n+1])z.n++;
return z;
}
node make_2(int x){
node z;z.n=A.n;int r=x+B.n-1;
for(int i=1;i<=z.n;i++)z.a[i]=A.a[i];
for(int i=x;i<=x+B.n-1;i++)z.a[i]=B.a[i-x+1];
int flag=0;
for(int i=x-1;i>=1;i--){
if(z.a[i]!=9){
flag=i;
break;
}
}
if(flag)z.a[flag]++;
else {
for(int i=1;i<x;i++)z.a[i]=0;
z.a[r+1]++;
for(int i=r+1;i<=z.n;i++)
if(z.a[i]>=10)z.a[i+1]++,z.a[i]-=10;
if(z.a[z.n+1])z.n++;
}
return z;
}
node make_3(int l){
node z;z.n=A.n;
int r=l+B.n-1;
for(int i=1;i<l;i++)z.a[i]=0;
for(int i=l;i<=r;i++)z.a[i]=B.a[i-l+1];
for(int i=r+1;i<=z.n;i++)z.a[i]=A.a[i];
return z;
}
int t,m,len,M;
string s;
void solve(){
cin>>s>>m;len=s.size();A.n=len;M=m;
for(int i=0;i<len;i++)A.a[len-i]=s[i]-'0';
ans=A/m;
ans.a[1]++;
if(!ans.n)ans.n=1;
else{
for(int i=1;i<=ans.n;i++)
if(ans.a[i]>=10)ans.a[i]-=10,ans.a[i+1]++;
if(ans.a[ans.n+1])ans.n++;
}
long long res=0;
for(int i=1;i<=ans.n;i++){
res+=1LL*M*ans.a[i];
ans.a[i]=res%10;
res/=10;
}
while(res)ans.a[++ans.n]=res%10,res/=10;
// ans=ans*m;
while(m){
B.a[++B.n]=m%10;
m/=10;
}
if(A<=B)
B.print();
else{
int d1,d2,d3;
d1=d2=d3=A.n+1;
for(int l=1;l<=A.n-B.n+1;l++){
int r=l+B.n-1;
int num=check(l,r);
if(num==2)d1=min(d1,l);
else if(num==0)d2=min(d2,l);
else d3=min(d3,l);
}
if(d1!=A.n+1){
int r=d1+B.n-1;tmp.n=A.n;
for(int i=1;i<d1;i++)tmp.a[i]=0;
for(int i=d1;i<=r;i++)tmp.a[i]=B.a[i-d1+1];
tmp.a[r+1]++;
for(int i=r+1;i<=tmp.n;i++)
if(tmp.a[i]>=10)tmp.a[i+1]++,tmp.a[i]-=10;
if(tmp.a[tmp.n+1])tmp.n++;
if(tmp<=ans){
ans.clear();ans=tmp;
}
}
if(d2!=A.n+1){
tmp.n=A.n;int r=d2+B.n-1;
for(int i=1;i<=tmp.n;i++)tmp.a[i]=A.a[i];
for(int i=d2;i<=d2+B.n-1;i++)tmp.a[i]=B.a[i-d2+1];
int flag=0;
for(int i=d2-1;i>=1;i--){
if(tmp.a[i]!=9){
flag=i;
break;
}
}
if(flag)tmp.a[flag]++;
else {
for(int i=1;i<d2;i++)tmp.a[i]=0;
tmp.a[r+1]++;
for(int i=r+1;i<=tmp.n;i++)
if(tmp.a[i]>=10)tmp.a[i+1]++,tmp.a[i]-=10;
if(tmp.a[tmp.n+1])tmp.n++;
}
if(tmp<=ans){
ans.clear();ans=tmp;
}
}
if(d3!=A.n+1){
for(int l=d3;l<=min(d3+10,A.n-B.n+1);l++){
if(check(l,l+B.n-1)!=1)continue;
int r=l+B.n-1;
tmp.n=A.n;
for(int i=1;i<l;i++)tmp.a[i]=0;
for(int i=l;i<=r;i++)tmp.a[i]=B.a[i-l+1];
for(int i=r+1;i<=tmp.n;i++)tmp.a[i]=A.a[i];
if(tmp<=ans){
ans.clear();ans=tmp;
}
}
}
ans.print();
}
A.clear();B.clear();
}
int main(){
scanf("%d",&t);
A.n=B.n=zero.n=one.n=1e6;
A.clear();B.clear();one.clear();zero.clear();
one.n=zero.n=1;one.a[1]=1;
for(;t;t--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 24084kb
input:
6 7 3 12 3 9 10 249 51 1369 37 2 1
output:
9 13 10 251 0637 11
result:
wrong answer 5th lines differ - expected: '1370', found: '0637'