QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#607450#8932. BingoKomorebie#WA 0ms16164kbC++174.5kb2024-10-03 14:57:172024-10-03 14:57:19

Judging History

你现在查看的是最新测评结果

  • [2024-10-03 14:57:19]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:16164kb
  • [2024-10-03 14:57:17]
  • 提交

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[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){
            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){
            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;
}

详细

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 16164kb

input:

6
7 3
12 3
9 10
249 51
1369 37
2 1

output:

8
13
10
251
1370
3

result:

wrong answer 1st lines differ - expected: '9', found: '8'