QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#607514 | #8932. Bingo | Zaria | WA | 96ms | 16312kb | C++17 | 4.6kb | 2024-10-03 15:08:06 | 2024-10-03 15:08:11 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll N=3e6+10,M=1e6+10;
ll t,m;
char str1[N],str2[N],str3[N];
ll num1[M],num2[M],num3[M];
ll val[N];
ll fail[100];
void get_val(ll len1){
val[0]=1e12;
val[len1]=10-str1[len1]+'0';
for(int i=len1-1;i>=1;i--){
ll num=str1[i]-'0';
ll pre_num=str1[i+1]-'0';
if(pre_num==9||num==9){
val[i]=val[i+1];
}
else{
if(len1-i-1<=10){
val[i]=val[i+1]+pow(10,len1-i-1);
}
else val[i]=1e12;
}
}
}
ll add(ll len)
{
ll idx=0;
for(int i=0;i<=len;i++) num1[i]=num2[i]=num3[i]=0;
ll length1,length2;
bool judge=false;
length1=strlen(str1);
length2=strlen(str2);
for(int i=length1-1,j=0;i>=0;i--,j++)
num1[j]=str1[i]-48;
for(int i=length2-1,j=0;i>=0;i--,j++)
num2[j]=str2[i]-48;
if(length1>=length2)
{
for(int i=0;i<length1;i++)
{
if(num1[i]+num2[i]+num3[i]<10)
{
num3[i]+=num1[i]+num2[i];
}
else if(num1[i]+num2[i]+num3[i]>=10)
{
num3[i]=num3[i]+num1[i]+num2[i]-10;
num3[i+1]+=1;
}
}
for(int i=length1;i>=0;i--)
{
if(num3[i]!=0)
judge=true;
if(judge==true)
str1[++idx]=num3[i]+'0';
}
}
else
{
for(int i=0;i<length2;i++)
{
if(num1[i]+num2[i]+num3[i]<10)
{
num3[i]+=num1[i]+num2[i];
}
else if(num1[i]+num2[i]+num3[i]>=10)
{
num3[i]=num3[i]+num1[i]+num2[i]-10;
num3[i+1]+=1;
}
}
for(int i=length2;i>=0;i--)
{
if(num3[i]!=0)
judge=true;
if(judge==true)
str1[++idx]=num3[i]+'0';
}
}
return idx;
}
ll kmp(ll len1,ll len2){
bool flag=false;
ll id=0;
ll res=1e12;
for(int i=2,j=0;i<=len2;i++)
{
while(j&&str2[i]!=str2[j+1]) j=fail[j];
if(str2[i]==str2[j+1]) j++;
fail[i]=j;
}
for(int i=1,j=0;i<=len1;i++)
{
while(j&&str1[i]!=str2[j+1]) j=fail[j];
if(str1[i]==str2[j+1]) j++;
if(j==len2){
flag=true;
break;
}
if(j==len2-1){
if(i+1<=len1)
res=min(res,val[i+1]);
}
}
if(flag) return 0;
if(len1<len2) return -1;
for(int i=min(len2*2,len1);i>=len2;i--){
ll ans=0;
for(int j=1;j<=len2;j++){
ans=ans*10+str2[j]-str1[len1-i+1+j-1];
}
ll pre=ans;
ans=ans*pow(10,i-len2);
if(pre>ans) continue;
if(ans>0) res=min(res,ans);
}
return res;
}
int main(){
cin>>t;
while(t--){
string str;
cin>>str>>m;
ll len=str.size();
for(int i=0;i<len;i++){
str1[i]=str[i];
}
str2[0]='1';
len=add(len);
get_val(len);
if(m==1){
printf("%s\n",str1+1);
for(int i=0;i<=len;i++) str1[i]=0;
for(int i=0;i<=50;i++) str3[i]=fail[i]=str2[i]=0;
continue;
}
ll ans=0;
for(int i=1;i<=len;i++){
ans=((ans*10 + str1[i] - '0')%m);
}
ans=m-ans;
ll len2=0;
while(m){
str2[++len2]=m%10+'0';
m=m/10;
}
for(int i=1;i<=len2;i++){
str3[i]=str2[len2-i+1];
}
for(int i=1;i<=len2;i++){
str2[i]=str3[i];
}
ll res=kmp(len,len2);
if(res==-1){
res=ans;
}
else{
res=min(ans,res);
}
len2=0;
str1[0]=0;
for(int i=1;i<=len;i++){
str3[i]=str1[i];
str1[i-1]=str3[i];
}
for(int i=1;i<=20;i++) str2[i]=0;
while(res){
str2[++len2]=res%10+'0';
res=res/10;
}
for(int i=1;i<=len2;i++){
str3[i]=str2[len2-i+1];
}
for(int i=1;i<=len2;i++){
str2[i]=str3[i];
}
if(res==0) str2[0]='0';
str1[len]=0;
ll aaa=add(len);
printf("%s\n",str1+1);
for(int i=0;i<=len;i++) str1[i]=0;
for(int i=0;i<=50;i++) str3[i]=fail[i]=str2[i]=0;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 16312kb
input:
6 7 3 12 3 9 10 249 51 1369 37 2 1
output:
9 13 10 251 1370 3
result:
ok 6 lines
Test #2:
score: -100
Wrong Answer
time: 96ms
memory: 16200kb
input:
100000 3196282243 28 7614814237 33 2814581084 97 1075124401 58 7822266214 100 1767317768 31 7189709841 75 9061337538 69 6552679231 38 9946082148 18 5497675062 54 7787300351 65 4310767261 68 4811341953 100 3265496130 31 8294404054 62 2845521744 90 1114254672 26 6442013672 13 3744046866 40 3289624367 ...
output:
3196282244 7614814240 2814581097 1075124424 7822266300 1767317769 7189709850 9061337569 6552679238 9946082150 5497675063 7787300365 4310767268 4811342000 3265496131 8294404062 2845521790 1114254674 6442013673 3744046867 3289624375 6477935360 1292587541 5504674689 2898829161 7882736025 2846033387 923...
result:
wrong answer 2nd lines differ - expected: '7614814251', found: '7614814240'