QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#142590#12. SumNOI_AK_ME100 ✓5ms3624kbC++232.9kb2023-08-19 13:22:472023-08-19 13:22:51

Judging History

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

  • [2023-08-19 13:22:51]
  • 评测
  • 测评结果:100
  • 用时:5ms
  • 内存:3624kb
  • [2023-08-19 13:22:47]
  • 提交

answer

#define ssh(x)<x>
#include ssh(bits/stdc++.h)
#define rfor(x...)for(register x)
#define rep(i,l,r)rfor(ll i=l;i<=r;i++)
#define per(i,l,r)rfor(ll i=l;i>=r;i--)
#define ref(i,l,r)rfor(ll i=l;i<r;i++)
#define rev(i,u,v)rfor(ll i=head[u],v;v=to[i],i;i=next[i])
#define rej(i,s)rfor(ll i=s;i;i=(i-1)&(s))
#define red(i,n)rfor(ll i=n;i;i>>=1)
#define IO(x...)*#x&&freopen(#x".in","r",stdin)&&freopen(#x".out","w",stdout),gc=fgc,pc=fpc,atexit(fout)
typedef long long ll;
typedef const ll cll;
typedef unsigned long long ull;
cll N=1e6+7,mod=3e9+7,iz=1<<21,oz=1<<22;
typedef ll aN[N];
char is[iz],*i1=is,*i2=is,os[oz],*o1=os,*o2=os+oz;
int fgc(){
    return i1==i2&&is==(i2=is+fread(i1=is,1,iz,stdin))?-1:*i1++;
}
void fout(){
    fwrite(os,o1-os,1,stdout),o1=os;
}
int fpc(int c){
    if(o1==o2)fout();
    return*o1++=c;
}
int(*gc)()=getchar,(*pc)(int)=putchar,wt[126];
int readchar(int l=33,int r=126){
    if(l>r)std::swap(l,r);
    int c=gc();
    for(;c<l||c>r;c=gc());
    return c;
}
ll readll(){
   ll x=0,w=1;
   int c=gc();
   for(;c<48||c>57;c=gc())c-45||(w=-w);
   for(;c>47&&c<58;c=gc())x=x*10+c-48;
   return x*w;
}
void writell(cll u,int c=32){
    int t=0;
    rfor(ull n=u<0?pc(45),0ull-u:u;n;n/=10)wt[++t]=n%10^48;
    for(t||pc(48);t;)pc(wt[t--]);
    c&&pc(c);
}
int mygets(char*s,int c=0){
    char*t=s+1;
    while((*s=gc())<33);
    while((*t=gc())>32)t++;
    c&&(*t++=c),*t=0;
    return t-s;
}
int myputs(const char*s,int c=10){
    const char*t=s;
    while(*t)pc(*t++);
    c&&pc(c);
    return t-s+(c!=0);
}
typedef long double ld;
ll r;
ld s;
static inline ll gcd(ll a,ll b){
    while(b){
        cll t=a/b,lb=b;
        b=a-t*b,a=lb;
    }
    return a;
}
struct node{
    ll a,b,c;
    ld v()const{
        return(a*s+b)/c;
    }
    node operator/(cll n)const{
        return(node){a,b,c*n};
    }
    node&calc(cll n){
        return n&1?b=c*n+c-b,a=-a:b-=c*n,*this;
    }
    node&operator-=(cll n){
        return b-=c*n,*this;
    }
    void inv(){
        cll h=a*a*r-b*b;
        if(h%c==0)b=-b,c=h/c;
        else a*=c,b*=-c,c=h;
    }
};
static inline ll get(cll h,ll u){
    return h?-u:u;
}
static inline ll getans(node k,ll n){
    ll res=0,h=0;
    while(n){
        cll x=k.v();
        k.calc(x),h^=x&1;
        cll m=n*k.v();
        k.inv();
        ld kv=k.v();
        cll l=(m&~1)*kv;
        if(m&1){
            cll z=m*kv;
            res+=get(h,z-n+z-l);
        }else res+=get(h,n-l);
        k-=kv,n=(m&~1)*k.v(),k.inv();
    }
    return res;
}
void calc(){
    cll n=readll(),r=::r=readll(),s=::s=sqrtl(r);
    if(s*s==r)return writell(s&1?n&1?-1:0:n,10);
    writell(getans((node){1,0,1},n),10);
}
int main(){
    IO();
    //gc=getchar;
    cll T=readll();
    rep(i,1,T)calc();
    return 0;
}
/*
3
806045030 886
683448424 6915
959503440 8335
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 10
Accepted
time: 0ms
memory: 3448kb

input:

10
37708 7
69538 9
38134 80
3473 16
92954 13
29430 44
17426 96
15663 11
75821 83
47428 54

output:

-2
0
2
3473
-2
0
-4
-5
-5
2

result:

ok 10 lines

Test #2:

score: 10
Accepted
time: 1ms
memory: 3452kb

input:

10
59376 73
9360 29
30305 23
10559 2
73528 58
41269 86
40784 19
43858 53
39002 87
95066 41

output:

0
10
-3
-1
0
3
2
4
-16
18

result:

ok 10 lines

Test #3:

score: 10
Accepted
time: 1ms
memory: 3448kb

input:

10
60076 48
38847 70
59252 1
74762 70
7812 19
90811 58
11634 94
96907 88
20652 84
16802 17

output:

4
3
0
2
-10
-3
2
5
0
12

result:

ok 10 lines

Test #4:

score: 10
Accepted
time: 1ms
memory: 3412kb

input:

100
5540012 1
7902151 435
2104636 10
9874979 944
9491828 422
1658084 527
7456856 362
5162458 955
9651886 169
5078624 193
8057854 280
1938272 94
5067001 550
4903551 893
893512 764
2162009 623
332836 482
4740700 404
3261229 117
576808 403
3649120 902
8373985 714
4615642 63
4768081 913
9387232 200
7555...

output:

0
-29
-2
-1
0
-4
-2
-4
0
8
6
-2
9
3
-4
-17
-18
20
5
-18
22
3
-18
-19
8
-6
-5
34
2
-3
-1
-4
4
0
12
10
4
-9
1
-44
1
4
-7
-1
-1
-1
3
-53
-18
13
-7
4
2
0
32
-2
-6
-2
10
0
0
3
-23
-1
-3
0
-8
-12
-45
0
-4
-5
15
4
-12
5
-18
3693162
-30
-6
-20
-21
4632970
1
2
-2
9
-54
1
-3
1
4
12
-28
-5
-8
16
17
6
-9

result:

ok 100 lines

Test #5:

score: 10
Accepted
time: 0ms
memory: 3456kb

input:

100
4630212 87
9248078 426
6891066 219
4247609 960
1866556 929
9255581 91
3083606 560
2453102 63
6439983 356
2689201 554
7120847 38
7118187 525
18218 701
6992930 97
26476 15
3704161 725
6145345 847
8807336 286
3922305 463
18109 881
5995126 694
9032662 375
7221174 552
5554424 767
4939652 64
2379949 1...

output:

-20
-4
-18
1
-8
3
-2
-14
3
3
13
3
42
-10
-14
3
-9
4
-3
-3
2
-22
0
-52
4939652
17
10
0
3
4117917
15
6
-30
-38
0
27
-5
6
-1
-3
-5
12
-7
28
0
4
-29
4
-3
-46
14
0
-1
13
-13
-28
23
-2
-17
-3
0
10
-9
8
5
-6
5436599
-67
-28
-5
30
-2
1
-3
2
0
-3
-98
-49
7
9
4
11
7
-1
3
13
-31
6
0
-33
-5
8
16
-16
-2
-6
-23
5...

result:

ok 100 lines

Test #6:

score: 10
Accepted
time: 1ms
memory: 3340kb

input:

100
568277 485
1449753 186
2430838 69
6097682 1000
4979129 54
6573210 139
9897598 981
4458804 115
8493265 267
5503960 958
7754834 581
6738607 452
370002 483
2640861 505
3616507 143
8969813 79
2747402 602
5366043 787
8679164 727
6657432 521
189605 861
1660254 865
9967933 931
1481056 74
8602856 448
78...

output:

61
-1
6
8
13
-2
6
-2
-3
-10
-6
1
-36
5
-39
-9
4
1
-50
4
7
4
-1
0
0
0
-1
7
2
-16
-19
-3
-25
-21
4
-14
8
1
9
-3
-19
-14
-2
7
-15
-4
-8
5
-4
-2
2
3
-3
0
10
-5
13
22
27
-30
-1
0
7
-1
-12
-1
6
-3
3
-1
3
-5
-22
1
0
-14
-11
4
-2
5
7
3
3
-1
0
26
-5
9
-2
21
25
10
-5
2
-26
26
6
18
4
7

result:

ok 100 lines

Test #7:

score: 10
Accepted
time: 5ms
memory: 3488kb

input:

10000
631007895 60
383922033 63
280016433 29
347027013 58
428062961 85
819829956 78
275768954 25
778215153 94
740955184 73
77014552 44
224648232 20
388215231 86
375284766 58
198095921 73
336210501 25
333543741 89
449063074 80
289962625 90
556278511 72
637661903 7
952148968 29
447686921 10
251154897 ...

output:

-9
-27
9
-1
23
8
0
-1
6
0
8
13
4
3
-1
21
2
1
7
-1
24
-3
11
16
5
-10
-26
11
-11
-3
-6
-1
-15
-4
-49
38
5
18
6
-1
-2
-1
2
-6
816132226
14
-6
-4
5
-7
-2
-5
-45
27
-14
-7
-6
10
17
39402409
-4
22
-24
816412577
-3
1
2
1
13
4
9
-7
813848979
-2
7
4
288769046
-12
17
-1
-6
8
-1
0
11
-6
-3
254614147
2
3
2
3141...

result:

ok 10000 lines

Test #8:

score: 10
Accepted
time: 5ms
memory: 3616kb

input:

10000
503319017 335
413716092 897
661093208 958
661306042 634
124257053 680
513264774 946
522922726 782
748686430 782
670208115 681
353040453 833
20125598 380
176913280 343
869661296 824
664105797 498
186457774 77
127964044 727
40653113 554
528554968 387
495945817 308
303462004 664
353009120 131
832...

output:

-39
-26
-34
-4
33
10
-38
-46
-11
7
2
-24
2
31
4
-30
1
-8
-7
-2
-10
-1
0
39
-11
-42
-1
3
-17
-19
5
0
-3
5
-3
-6
4
11
-1
38
5
-6
-5
3
12
-6
30
1
-10
3
3
77
-47
1
-46
0
-1
-15
5
1
-23
-89
-16
-12
-4
4
-2
17
0
5
-13
4
3
-40
-2
-2
-3
6
-35
-1
-13
4
8
-40
1
13
-4
1
11
21
-46
-9
7
15
-5
-1
-28
-85
-5
-4
-3...

result:

ok 10000 lines

Test #9:

score: 10
Accepted
time: 5ms
memory: 3624kb

input:

10000
478973133 8220
646685093 6929
319219559 7366
310897610 3205
801884864 4934
961964821 5580
843127736 8427
705144026 1221
621403018 3726
27088125 9386
54900007 4707
769719411 8840
265661605 2561
117532764 166
237176664 6938
800894259 8674
871899887 3096
58053871 8536
590953398 1380
982566506 606...

output:

-53
-5
-3
6
22
7
-178
2
4
27
-3
61
5
4
0
-11
-3
-1
0
-6
-11
-65
13
-21
0
0
-8
11
-5
47
5
2
9
742169774
-1
0
2
-2
-15
-10
7
15
1
131
-4
24
-1
0
88
-12
18
-4
-180
-31
10
9
-10
-96
4
2
-28
4
-5
0
-13
0
-42
-46
-18
-5
-11
-4
5
80
1
-34
-12
-3
3
-10
20
8
-8
22
-27
19
-21
-13
-1
-1
-97
-35
-7
-25
-19
4
6
...

result:

ok 10000 lines

Test #10:

score: 10
Accepted
time: 2ms
memory: 3600kb

input:

10000
424217816 96
605089831 52
732654064 30
669647487 57
595568934 7
62523527 46
938890183 63
837791474 95
383966699 6
686681698 73
42834085 23
375710745 69
912358250 96
804815989 22
254041255 11
879623314 88
730358325 3
690503483 16
766302978 55
548354317 1
560081953 5
497037581 50
569516360 14
64...

output:

-12
3
-2
-3
-2
-1
-29
-6
11
-10
-1
1
-6
11
-9
-2
-5
690503483
-2
-1
15
1
-4
2
1
-6
9
-19
-3
7
6
-14
-4
2
3
20
-8
-1
7
-7
667243127
13
2
2
10
0
3
-1
5
-7
-7
979146442
-22
-18
-8
0
10
1
1
2
-8
0
11
224413155
1
25
7
-2
23
-10
-1
-9
5
6
-2
6
8
-4
0
-25
10
-12
-5
10
5
11
1
7
10
10
6
-10
-22
-2
8
9
-12
-1...

result:

ok 10000 lines