QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#798228#3596. New MathsCryingAC ✓501ms3864kbC++142.0kb2024-12-04 09:54:292024-12-04 09:54:31

Judging History

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

  • [2024-12-04 09:54:31]
  • 评测
  • 测评结果:AC
  • 用时:501ms
  • 内存:3864kb
  • [2024-12-04 09:54:29]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 25,M = 7,C = 1e4+10;
typedef array<int,M> vec;
int z[N],val[N],n,m,k,pw[M+1],tmp[N];

vec p[C],q[C]; int pc,qc;

int main(){
    pw[0] = 1; for(int i=1;i<=M;i++)pw[i] = pw[i-1] * 10;

    string s; cin>>s; n = s.length();
    for(int i=0;i<n;i++)z[i] = s[n-1-i] - '0';
    if(n%2==0){cout<<"-1\n"; return 0;}
    if(n==1){
        if(z[0] == 1)cout<<"1\n";
        else if(z[0] == 4)cout<<"2\n";
        else if(z[0] == 9)cout<<"3\n";
        else if(z[0] == 6)cout<<"4\n";
        else if(z[0] == 5)cout<<"5\n";
        else cout<<"-1\n";
        return 0;
    }
    m = (n+1)/2,k = m/2; //[0,k) 和 [k,m) 两部分

    for(int i=0;i<pw[k];i++){
        vec now = {};
        int num = i; for(int x=0;x<k;x++)now[x] = num%10,num /= 10;
        memset(tmp,0,sizeof tmp);
        for(int x=0;x<k;x++)for(int y=0;x+y<k;y++)tmp[x+y] += now[x] * now[y];
        int flag = 1; for(int x=0;x<k;x++)flag &= (tmp[x]%10) == z[x];
        if(!flag)continue;
        q[++qc] = now;
    }
    for(int i=0;i<pw[m-k];i++){
        vec now = {};
        int num = i; for(int x=0;x<m-k;x++)now[x] = num%10,num /= 10;
        memset(tmp,0,sizeof tmp); int lim = m+k-1;
        for(int x=k;x<m;x++)for(int y=lim-x;y<m;y++)tmp[x+y] += now[x-k] * now[y-k];
        int flag = 1; for(int x=lim;x<n;x++)flag &= (tmp[x]%10) == z[x];
        if(!flag)continue;
        p[++pc] = now;
    }
    for(int i=1;i<=pc;i++)for(int j=1;j<=qc;j++){
        memset(val,0,sizeof val); memset(tmp,0,sizeof tmp); int cur = m;
        for(int x=m-k-1;x>=0;x--)val[--cur] = p[i][x];
        for(int x=k-1;x>=0;x--)val[--cur] = q[j][x];
        for(int x=0;x<m;x++)for(int y=0;y<m;y++)tmp[x+y] += val[x] * val[y];
        int flag = 1;
        for(int x=0;x<n;x++)flag &= (tmp[x]%10) == z[x];
        if(flag){
            for(int x=m-1;x>=0;x--)cout<<val[x];
            return 0;
        }
    }

    cout<<"-1\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

149

output:

17

result:

ok single line: '17'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3572kb

input:

123476544

output:

11112

result:

ok single line: '11112'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3624kb

input:

15

output:

-1

result:

ok single line: '-1'

Test #5:

score: 0
Accepted
time: 1ms
memory: 3632kb

input:

255768423183

output:

-1

result:

ok single line: '-1'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3796kb

input:

45665732555

output:

-1

result:

ok single line: '-1'

Test #7:

score: 0
Accepted
time: 1ms
memory: 3628kb

input:

129862724660409

output:

11450607

result:

ok single line: '11450607'

Test #8:

score: 0
Accepted
time: 45ms
memory: 3628kb

input:

423019449793954427977

output:

-1

result:

ok single line: '-1'

Test #9:

score: 0
Accepted
time: 0ms
memory: 3536kb

input:

27121

output:

-1

result:

ok single line: '-1'

Test #10:

score: 0
Accepted
time: 7ms
memory: 3632kb

input:

1062062012267214086

output:

1086309746

result:

ok single line: '1086309746'

Test #11:

score: 0
Accepted
time: 7ms
memory: 3624kb

input:

7121439534552006831

output:

-1

result:

ok single line: '-1'

Test #12:

score: 0
Accepted
time: 0ms
memory: 3516kb

input:

2331

output:

-1

result:

ok single line: '-1'

Test #13:

score: 0
Accepted
time: 0ms
memory: 3828kb

input:

664

output:

42

result:

ok single line: '42'

Test #14:

score: 0
Accepted
time: 4ms
memory: 3624kb

input:

48960202843634584

output:

270467972

result:

ok single line: '270467972'

Test #15:

score: 0
Accepted
time: 1ms
memory: 3824kb

input:

950191368742123

output:

-1

result:

ok single line: '-1'

Test #16:

score: 0
Accepted
time: 501ms
memory: 3560kb

input:

6838729832825874768072709

output:

4195767732753

result:

ok single line: '4195767732753'

Test #17:

score: 0
Accepted
time: 0ms
memory: 3628kb

input:

126412441

output:

16521

result:

ok single line: '16521'

Test #18:

score: 0
Accepted
time: 7ms
memory: 3864kb

input:

4466447004844686806

output:

2609442604

result:

ok single line: '2609442604'

Test #19:

score: 0
Accepted
time: 0ms
memory: 3824kb

input:

4

output:

2

result:

ok single line: '2'

Test #20:

score: 0
Accepted
time: 4ms
memory: 3620kb

input:

90074858599988014

output:

-1

result:

ok single line: '-1'

Test #21:

score: 0
Accepted
time: 0ms
memory: 3668kb

input:

9498578088490196330623

output:

-1

result:

ok single line: '-1'

Test #22:

score: 0
Accepted
time: 0ms
memory: 3520kb

input:

82

output:

-1

result:

ok single line: '-1'

Test #23:

score: 0
Accepted
time: 7ms
memory: 3624kb

input:

8925123681749359895

output:

-1

result:

ok single line: '-1'

Test #24:

score: 0
Accepted
time: 0ms
memory: 3624kb

input:

9008004

output:

3008

result:

ok single line: '3008'