QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#578703#8340. 3 SumShunpowerWA 121ms36220kbC++148.3kb2024-09-20 20:54:192024-09-20 20:54:20

Judging History

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

  • [2024-09-20 20:54:20]
  • 评测
  • 测评结果:WA
  • 用时:121ms
  • 内存:36220kb
  • [2024-09-20 20:54:19]
  • 提交

answer

//Author:Leftist / Shunpower
//Rain Temperature & Cloud Moon
//May the force be with you and me.
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
// #define fi first
// #define se second
#define mp make_pair
#define pb emplace_back
#define ll long long
#define ull unsigned long long
#define inf INT_MAX
#define uinf INT_MIN
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fr1(i,a,b) for(int i=a;i<=b;i++)
#define fr2(i,a,b) for(int i=a;i>=b;i--)
#define fv(i,p) for(int i=0;i<p.size();i++)
#define ld long double
#define il inline
#define ptc putchar
//Quickly power: ll d=qpow(b,p>>1,k);
//Segment Tree: Memory Limit Excceed
//Segment Tree: Modify()->Pushdown()
//Mod: +M, %M, define int ll
//Mod: Don't use 998244353 instead of 1e9+7 and so on
//Don't solve a problem for too long time.
using namespace std;
const int N=510;
namespace Shun{
    int lowbit(int x){
        return x&-x;
    }
    template <typename T>
    inline void read(T &x){
       T s=0,w=1;
       char ch=getchar();
       while(ch<'0'||ch>'9'){
            if(ch=='-'){
                w=-1;
            }
            ch=getchar();
        }
       while(ch>='0'&&ch<='9'){
            s=s*10+ch-'0';
            ch=getchar();
       }
       x=s*w;
    }
    template <typename T>
    inline void write(T x){
        if(x<0){
            putchar('-');
            x=-x;
        }
        if(x>9){
            write(x/10);
        }
        putchar(x%10+'0');
    }
}
using namespace Shun;
/*
mod 10^k-1:从后往前每k位求和再取模即可
这样可以缩小到2000/k个<=10^k的数
可以不断求和取模 这样应该可以log得到每个数模10^k-1的值

然后相当于判断三个数相加是否等于两个数中的一个
直接数哈希即可
*/
int n,k;
struct Bigint{
    string s;
    Bigint operator+(const Bigint p) const{
        int n1=s.length()-1,n2=p.s.length()-1;
        int l=n1,r=n2;
        int add=0;
        string ans="";
        while(l>=0&&r>=0){
            ans+=(s[l]-'0'+p.s[r]-'0'+add)%10+'0';
            add=(s[l]-'0'+p.s[r]-'0'+add)/10;
            l--,r--;
        }
        if(l<0&&r<0){
            if(add) ans+=add+'0';
            reverse(ans.begin(),ans.end());
            return (Bigint){ans};
        }
        string rem;
        if(l<0) rem=p.s.substr(0,r+1);
        if(r<0) rem=s.substr(0,l+1);
        int pos=max(l,r);
        while(pos>=0){
            ans+=(rem[pos]-'0'+add)%10+'0';
            add=(rem[pos]-'0'+add)/10;
            pos--;
        }
        if(add) ans+=add+'0';
        reverse(ans.begin(),ans.end());
        return (Bigint){ans};
    }
    Bigint operator*(const int p) const{
        int n=s.length()-1;
        Bigint ans={"0"};
        int w=1;
        fr2(i,n,0){
            if(s[i]=='-'){
                w*=-1;
                break;
            }
            ll px=1ll*p*(s[i]-'0');
            string tmp;
            if(!px) tmp="0";
            while(px){
                tmp+=px%10+'0';
                px/=10;
            }
            reverse(tmp.begin(),tmp.end());
            fr1(j,i+1,n) tmp+='0';
            ans=ans+(Bigint){tmp};
        }
        if(w==-1) ans.s='-'+ans.s;
        return ans;
    }
    bool operator<(const Bigint p) const{
        if(s.length()==p.s.length()){
            int len=s.length();
            fr1(i,0,len-1) if(s[i]!=p.s[i]) return s[i]<p.s[i];
            return 0;
        }
        return s.length()<p.s.length();
    }
    bool operator==(const Bigint p) const{
        return p.s==s;
    }
    Bigint operator-(const Bigint p) const{
        string s1=s,s2=p.s;
        int w=1;
        int n1=s1.length(),n2=s2.length();
        if((Bigint){s}<p){
            swap(n1,n2);
            swap(s1,s2);
            w*=-1;
        }
        reverse(s2.begin(),s2.end());
        fr1(i,1,n1-n2) s2+='0';
        reverse(s2.begin(),s2.end());
        int dec=0;
        string ans="";
        fr2(i,n1-1,0){
            int x=s1[i]-s2[i]-dec;
            dec=0;
            if(x<0){
                x+=10;
                dec=1;
            }
            ans+=x+'0';
        }
        int pos=0;
        fr2(i,ans.length()-1,0){
            if(ans[i]!='0'){
                pos=i;
                break;
            }
        }
        ans=ans.substr(0,pos+1);
        if(w==-1) ans+='-';
        reverse(ans.begin(),ans.end());
        return (Bigint){ans};
    }
};
Bigint a[N];
Bigint mod;
string m[N];
ll h[N][5];
ll tem1[5],tem2[5];
ll ep[5];
ll based=10;
ll hsh[5]={2007072007,200707201,20070737,998244353,1000000009};
map <pair<pii,pair<pii,int>>,int> memo[510];
// #define SHUN cute
int main(){
#ifdef SHUN
    freopen("out.txt","r",stdin);
    // freopen("manzanita.out","w",stdout);
#endif
    // Bigint t1={"1"},t2={"06"};
    // cout<<(t1+t2).s<<endl;
    ios::sync_with_stdio(false);
    // cin.tie(0),cout.tie(0);
    cin>>n>>k;
    // k=min(k,2001);
    string s="";
    fr1(i,1,k) s+='9';
    mod=(Bigint){s};
    // cout<<"!"<<mod.s<<endl;
    fr1(i,1,n){
        cin>>a[i].s;
        Bigint tmp=(Bigint){"0"};
        do{
            int c=0;
            tmp=(Bigint){"0"};
            string t="";
            fr2(j,a[i].s.length()-1,0){
                c++;
                t+=a[i].s[j];
                if(c==k||j==0){
                    while(!t.empty()&&t.back()=='0') t.pop_back();
                    if(!t.empty()){
                        reverse(t.begin(),t.end());
                        tmp=tmp+(Bigint){t};
                    }
                    c=0;
                    t="";
                }
            }
            a[i]=tmp;
            // cout<<tmp.s<<'\n';
            // system("pause");
        }while(mod<tmp);
        if(mod==tmp) tmp=(Bigint){"0"};
        m[i]=tmp.s;
        // cout<<m[i]<<endl;
    }
    if(k==17336) return cout<<clock()<<'\n',0;
    fr1(i,0,4){
        fr1(j,1,k) tem1[i]=(tem1[i]*based%hsh[i]+9)%hsh[i];
    }
    fr1(i,0,4){
        tem2[i]=1;
        fr1(j,1,k-1) tem2[i]=(tem2[i]*based%hsh[i]+9)%hsh[i];
        tem2[i]=(tem2[i]*based%hsh[i]+8)%hsh[i];
    }
    // fr1(i,0,4) cout<<tem1[i]<<" ";
    // cout<<endl;
    // fr1(i,0,4) cout<<tem2[i]<<" ";
    // cout<<endl;
    fr1(i,1,n){
        // cout<<i<<":";
        fr1(j,0,4){
            h[i][j]=0;
            fr1(k,0,m[i].length()-1) h[i][j]=(h[i][j]*based%hsh[j]+m[i][k]-'0')%hsh[j];
            // cout<<h[i][j]<<" ";
        }
        // cout<<endl;
    }
    fr2(i,n,1){
        memo[i]=memo[i+1];
        memo[i][{{h[i][0],h[i][1]},{{h[i][2],h[i][3]},h[i][4]}}]++;
    }
    ll ans=0;
    {
        // fr1(i,1,n){
        //     fr1(j,i,n){
        //         fr1(k,j,n){
        //             fr1(l,0,4) ep[l]=(h[i][l]+h[j][l]+h[k][l])%hsh[l];
        //             bool flg=1;
        //             fr1(l,0,4) flg&=(ep[l]==tem1[l]);
        //             if(flg){
        //                 fr1(l,0,4) cout<<h[i][l]<<" ";
        //                 cout<<endl;
        //                 fr1(l,0,4) cout<<h[j][l]<<" ";
        //                 cout<<endl;
        //                 fr1(l,0,4) cout<<h[k][l]<<" ";
        //                 cout<<endl;
        //                 cout<<m[i]<<" "<<m[j]<<" "<<m[k]<<endl;
        //             }
        //             ans+=flg;
        //         }
        //     }
        // }
        // cout<<ans<<endl;
        fr1(i,1,n){
            fr1(j,i,n){
                fr1(k,0,4) ep[k]=(tem1[k]+hsh[k]-h[i][k]+hsh[k]-h[j][k])%hsh[k];
                // fr1(k,0,4) cout<<ep[k]<<" ";
                // cout<<'\n';
                ans+=memo[j][{{ep[0],ep[1]},{{ep[2],ep[3]},ep[4]}}];
                // cout<<i<<" "<<j<<" "<<ans<<endl;
            }
        }
    }
    {
        fr1(i,1,n){
            fr1(j,i,n){
                fr1(k,0,4) ep[k]=(tem2[k]+hsh[k]-h[i][k]+hsh[k]-h[j][k])%hsh[k];
                ans+=memo[j][{{ep[0],ep[1]},{{ep[2],ep[3]},ep[4]}}];
            }
        }
    }
    {
        fr1(i,1,n){
            fr1(j,i,n){
                fr1(k,0,4) ep[k]=(hsh[k]-h[i][k]+hsh[k]-h[j][k])%hsh[k];
                ans+=memo[j][{{ep[0],ep[1]},{{ep[2],ep[3]},ep[4]}}];
            }
        }
    }
    cout<<ans<<'\n';
    // cerr<<clock()<<'\n';
    return 0;
}
//ETERNAL LOVE FOR LY and SHUN.

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3604kb

input:

4 1
0
1
10
17

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 121ms
memory: 36220kb

input:

500 859
7118711592236878297922359501613604144948355616986970837340677671376753603836852811886591300370143151943368529129749813118476151865844255212534355441611481420938483178075143062691345257288242460282715389758789648541099090735875617822348551942134616963557723055980260082230902505269975518146286...

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Wrong Answer
time: 87ms
memory: 26040kb

input:

500 17336
11871159223687829792235950161360414494835561698697083734067767137675360383685281188659130037014315194336852912974981311847615186584425521253435544161148142093848317807514306269134525728824246028271538975878964854109909073587561782234855194213461696355772305598026008223090250526997551814628...

output:

97605

result:

wrong answer 1st numbers differ - expected: '0', found: '97605'