Submission #335526

# Submission time Handle Problem Language Result Execution time Memory
335526 2020-12-13T01:26:49 Z KWang31 Nice sequence (IZhO18_sequence) Java 11
100 / 100
917 ms 62040 KB
import java.io.*; import java.util.*;
public class sequence{
  static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        
    } 
    public static class Pair implements Comparable<Pair>{
        int vtx; int val;
        public Pair(int a, int b){
            this.vtx=a; this.val=b;
        }
        public int compareTo(Pair other){
            if(this.val<other.val)return -1;
            if(this.val>other.val)return 1;
            if(this.vtx<other.vtx)return -1;
            return 1;
        }
    }
    static int MOD=1000000;
    
    public static void main(String[] args){
        
        FastReader br=new FastReader();
        int T=br.nextInt();
        while(T>0){
            T--;
            int N=br.nextInt(); int M=br.nextInt();
            
            int d=gcd(N,M); int n=N/d; int m=M/d;
            
            if((long) n*m==1){
                
                System.out.println(M-1);
                StringBuilder sb=new StringBuilder();
                for (int i = 1; i < M; i++) {
                    sb.append(1+" ");
                }
                System.out.println(sb.toString()); continue;
            }
            int[] psa=new int[n+m-1]; //psa[0]=0
            int cur=n-1;
            for (int i = 0; i < n+m-2; i++) {
                
                if(cur>=m){
                    psa[cur-m]=psa[cur]-1; cur-=m;
                }else{
                    psa[cur+n]=psa[cur]-1; cur+=n;
                }
            }
            for (int i = n+m-2; i >=0; i--) {
                psa[i]-=psa[0];
            }
            long[] ans=new long[N+M-d];
            
            boolean[] pos=new boolean[n+m-1];
            boolean[] neg=new boolean[n+m-1];
            for (int i = 1; i < n+m-1; i++) {
                if(psa[i]-psa[i-1]>0){
                    pos[psa[i]-psa[i-1]]=true;
                }else{
                    neg[psa[i-1]-psa[i]]=true;
                }
            }
            
            if(d==1){
                for (int j = 0; j < n+m-1; j++) {
                    for (int i = 0; i < d; i++) {
                        ans[j*d+i]=(long) MOD*i+psa[j];
                    
                    }
                }
            }else{
                int c=1; while((d-1)*c<n+m-1 && pos[(d-1)*c])c++;
                int z=1; while((d-1)*z<n+m-1 && neg[(d-1)*z])z++;
                c=Math.min(c,-z);
                for (int j = 0; j < n+m-1; j++) {
                    for (int i = 0; i < d; i++) {
                        ans[j*d+i]=(long) c*i+psa[j];
                    
                    }
                }
            }
            
            System.out.println(N+M-d-1);
            
            if(N+M-d>1){
                StringBuilder sb=new StringBuilder();
                for (int i = 1; i < N+M-d; i++) {
                    sb.append((long) (ans[i]-ans[i-1])+" ");
                }
                System.out.println(sb.toString());
            }
        }
        
        
    }
    public static int gcd(int a, int b){//Assumes a>b
        if(b==0)return a;
        return gcd(b,a%b);
    }
    
    
}
//Debugging:
//Are you sure your algorithm is correct?
//Bounds: long
//Special cases: n=0,1?
//Make sure you remove your debugging code before you submit!
# Verdict Execution time Memory Grader output
1 Correct 117 ms 9700 KB Ok
2 Correct 124 ms 9708 KB Ok
3 Correct 119 ms 9600 KB Ok
4 Correct 117 ms 9708 KB Ok
5 Correct 122 ms 9708 KB Ok
6 Correct 120 ms 9708 KB Ok
7 Correct 119 ms 9708 KB Ok
8 Correct 118 ms 9708 KB Ok
9 Correct 118 ms 9708 KB Ok
10 Correct 118 ms 9708 KB Ok
11 Correct 119 ms 9708 KB Ok
12 Correct 73 ms 8556 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 117 ms 9708 KB Ok
2 Correct 118 ms 9708 KB Ok
3 Correct 119 ms 9652 KB Ok
4 Correct 118 ms 9708 KB Ok
5 Correct 119 ms 9680 KB Ok
6 Correct 324 ms 17180 KB Ok
7 Correct 460 ms 18668 KB Ok
8 Correct 356 ms 17352 KB Ok
9 Correct 434 ms 18220 KB Ok
10 Correct 343 ms 17772 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 120 ms 9708 KB Ok
2 Correct 113 ms 9704 KB Ok
3 Correct 115 ms 9708 KB Ok
4 Correct 116 ms 9708 KB Ok
5 Correct 113 ms 9708 KB Ok
6 Correct 119 ms 9708 KB Ok
7 Correct 115 ms 9708 KB Ok
8 Correct 117 ms 9972 KB Ok
9 Correct 114 ms 9708 KB Ok
10 Correct 119 ms 9708 KB Ok
11 Correct 116 ms 9728 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 115 ms 9708 KB Ok
2 Correct 114 ms 9708 KB Ok
3 Correct 117 ms 9880 KB Ok
4 Correct 116 ms 9836 KB Ok
5 Correct 122 ms 9708 KB Ok
6 Correct 563 ms 27192 KB Ok
7 Correct 612 ms 25952 KB Ok
8 Correct 616 ms 31584 KB Ok
9 Correct 683 ms 27896 KB Ok
10 Correct 357 ms 23840 KB Ok
11 Correct 657 ms 27256 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 117 ms 9700 KB Ok
2 Correct 124 ms 9708 KB Ok
3 Correct 119 ms 9600 KB Ok
4 Correct 117 ms 9708 KB Ok
5 Correct 122 ms 9708 KB Ok
6 Correct 120 ms 9708 KB Ok
7 Correct 119 ms 9708 KB Ok
8 Correct 118 ms 9708 KB Ok
9 Correct 118 ms 9708 KB Ok
10 Correct 118 ms 9708 KB Ok
11 Correct 119 ms 9708 KB Ok
12 Correct 73 ms 8556 KB Ok
13 Correct 120 ms 9708 KB Ok
14 Correct 113 ms 9704 KB Ok
15 Correct 115 ms 9708 KB Ok
16 Correct 116 ms 9708 KB Ok
17 Correct 113 ms 9708 KB Ok
18 Correct 119 ms 9708 KB Ok
19 Correct 115 ms 9708 KB Ok
20 Correct 117 ms 9972 KB Ok
21 Correct 114 ms 9708 KB Ok
22 Correct 119 ms 9708 KB Ok
23 Correct 116 ms 9728 KB Ok
24 Correct 356 ms 17940 KB Ok
25 Correct 369 ms 17824 KB Ok
26 Correct 355 ms 17848 KB Ok
27 Correct 349 ms 16932 KB Ok
28 Correct 328 ms 16500 KB Ok
29 Correct 228 ms 11864 KB Ok
30 Correct 333 ms 16628 KB Ok
31 Correct 337 ms 17400 KB Ok
32 Correct 354 ms 17548 KB Ok
33 Correct 342 ms 17572 KB Ok
34 Correct 343 ms 17636 KB Ok
35 Correct 305 ms 17328 KB Ok
36 Correct 367 ms 17400 KB Ok
37 Correct 314 ms 17596 KB Ok
38 Correct 339 ms 17412 KB Ok
39 Correct 362 ms 17632 KB Ok
40 Correct 426 ms 18196 KB Ok
41 Correct 339 ms 17356 KB Ok
42 Correct 363 ms 17696 KB Ok
43 Correct 363 ms 17440 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 117 ms 9700 KB Ok
2 Correct 124 ms 9708 KB Ok
3 Correct 119 ms 9600 KB Ok
4 Correct 117 ms 9708 KB Ok
5 Correct 122 ms 9708 KB Ok
6 Correct 120 ms 9708 KB Ok
7 Correct 119 ms 9708 KB Ok
8 Correct 118 ms 9708 KB Ok
9 Correct 118 ms 9708 KB Ok
10 Correct 118 ms 9708 KB Ok
11 Correct 119 ms 9708 KB Ok
12 Correct 73 ms 8556 KB Ok
13 Correct 117 ms 9708 KB Ok
14 Correct 118 ms 9708 KB Ok
15 Correct 119 ms 9652 KB Ok
16 Correct 118 ms 9708 KB Ok
17 Correct 119 ms 9680 KB Ok
18 Correct 324 ms 17180 KB Ok
19 Correct 460 ms 18668 KB Ok
20 Correct 356 ms 17352 KB Ok
21 Correct 434 ms 18220 KB Ok
22 Correct 343 ms 17772 KB Ok
23 Correct 120 ms 9708 KB Ok
24 Correct 113 ms 9704 KB Ok
25 Correct 115 ms 9708 KB Ok
26 Correct 116 ms 9708 KB Ok
27 Correct 113 ms 9708 KB Ok
28 Correct 119 ms 9708 KB Ok
29 Correct 115 ms 9708 KB Ok
30 Correct 117 ms 9972 KB Ok
31 Correct 114 ms 9708 KB Ok
32 Correct 119 ms 9708 KB Ok
33 Correct 116 ms 9728 KB Ok
34 Correct 356 ms 17940 KB Ok
35 Correct 369 ms 17824 KB Ok
36 Correct 355 ms 17848 KB Ok
37 Correct 349 ms 16932 KB Ok
38 Correct 328 ms 16500 KB Ok
39 Correct 228 ms 11864 KB Ok
40 Correct 333 ms 16628 KB Ok
41 Correct 337 ms 17400 KB Ok
42 Correct 354 ms 17548 KB Ok
43 Correct 342 ms 17572 KB Ok
44 Correct 343 ms 17636 KB Ok
45 Correct 305 ms 17328 KB Ok
46 Correct 367 ms 17400 KB Ok
47 Correct 314 ms 17596 KB Ok
48 Correct 339 ms 17412 KB Ok
49 Correct 362 ms 17632 KB Ok
50 Correct 426 ms 18196 KB Ok
51 Correct 339 ms 17356 KB Ok
52 Correct 363 ms 17696 KB Ok
53 Correct 363 ms 17440 KB Ok
54 Correct 450 ms 25192 KB Ok
55 Correct 452 ms 25220 KB Ok
56 Correct 510 ms 26680 KB Ok
57 Correct 503 ms 25168 KB Ok
58 Correct 519 ms 26492 KB Ok
59 Correct 443 ms 25120 KB Ok
60 Correct 438 ms 23544 KB Ok
61 Correct 510 ms 25700 KB Ok
62 Correct 444 ms 25456 KB Ok
63 Correct 502 ms 25808 KB Ok
64 Correct 519 ms 26608 KB Ok
65 Correct 455 ms 25620 KB Ok
66 Correct 692 ms 27884 KB Ok
67 Correct 507 ms 24912 KB Ok
68 Correct 441 ms 25024 KB Ok
69 Correct 533 ms 27292 KB Ok
70 Correct 528 ms 28156 KB Ok
71 Correct 506 ms 26780 KB Ok
72 Correct 598 ms 28760 KB Ok
73 Correct 535 ms 28316 KB Ok
74 Correct 493 ms 26292 KB Ok
75 Correct 519 ms 26476 KB Ok
76 Correct 515 ms 27988 KB Ok
77 Correct 488 ms 26076 KB Ok
78 Correct 516 ms 27976 KB Ok
79 Correct 590 ms 28464 KB Ok
80 Correct 580 ms 28496 KB Ok
81 Correct 599 ms 29920 KB Ok
82 Correct 586 ms 29564 KB Ok
83 Correct 495 ms 26676 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 117 ms 9700 KB Ok
2 Correct 124 ms 9708 KB Ok
3 Correct 119 ms 9600 KB Ok
4 Correct 117 ms 9708 KB Ok
5 Correct 122 ms 9708 KB Ok
6 Correct 120 ms 9708 KB Ok
7 Correct 119 ms 9708 KB Ok
8 Correct 118 ms 9708 KB Ok
9 Correct 118 ms 9708 KB Ok
10 Correct 118 ms 9708 KB Ok
11 Correct 119 ms 9708 KB Ok
12 Correct 73 ms 8556 KB Ok
13 Correct 117 ms 9708 KB Ok
14 Correct 118 ms 9708 KB Ok
15 Correct 119 ms 9652 KB Ok
16 Correct 118 ms 9708 KB Ok
17 Correct 119 ms 9680 KB Ok
18 Correct 324 ms 17180 KB Ok
19 Correct 460 ms 18668 KB Ok
20 Correct 356 ms 17352 KB Ok
21 Correct 434 ms 18220 KB Ok
22 Correct 343 ms 17772 KB Ok
23 Correct 120 ms 9708 KB Ok
24 Correct 113 ms 9704 KB Ok
25 Correct 115 ms 9708 KB Ok
26 Correct 116 ms 9708 KB Ok
27 Correct 113 ms 9708 KB Ok
28 Correct 119 ms 9708 KB Ok
29 Correct 115 ms 9708 KB Ok
30 Correct 117 ms 9972 KB Ok
31 Correct 114 ms 9708 KB Ok
32 Correct 119 ms 9708 KB Ok
33 Correct 116 ms 9728 KB Ok
34 Correct 115 ms 9708 KB Ok
35 Correct 114 ms 9708 KB Ok
36 Correct 117 ms 9880 KB Ok
37 Correct 116 ms 9836 KB Ok
38 Correct 122 ms 9708 KB Ok
39 Correct 563 ms 27192 KB Ok
40 Correct 612 ms 25952 KB Ok
41 Correct 616 ms 31584 KB Ok
42 Correct 683 ms 27896 KB Ok
43 Correct 357 ms 23840 KB Ok
44 Correct 657 ms 27256 KB Ok
45 Correct 356 ms 17940 KB Ok
46 Correct 369 ms 17824 KB Ok
47 Correct 355 ms 17848 KB Ok
48 Correct 349 ms 16932 KB Ok
49 Correct 328 ms 16500 KB Ok
50 Correct 228 ms 11864 KB Ok
51 Correct 333 ms 16628 KB Ok
52 Correct 337 ms 17400 KB Ok
53 Correct 354 ms 17548 KB Ok
54 Correct 342 ms 17572 KB Ok
55 Correct 343 ms 17636 KB Ok
56 Correct 305 ms 17328 KB Ok
57 Correct 367 ms 17400 KB Ok
58 Correct 314 ms 17596 KB Ok
59 Correct 339 ms 17412 KB Ok
60 Correct 362 ms 17632 KB Ok
61 Correct 426 ms 18196 KB Ok
62 Correct 339 ms 17356 KB Ok
63 Correct 363 ms 17696 KB Ok
64 Correct 363 ms 17440 KB Ok
65 Correct 450 ms 25192 KB Ok
66 Correct 452 ms 25220 KB Ok
67 Correct 510 ms 26680 KB Ok
68 Correct 503 ms 25168 KB Ok
69 Correct 519 ms 26492 KB Ok
70 Correct 443 ms 25120 KB Ok
71 Correct 438 ms 23544 KB Ok
72 Correct 510 ms 25700 KB Ok
73 Correct 444 ms 25456 KB Ok
74 Correct 502 ms 25808 KB Ok
75 Correct 519 ms 26608 KB Ok
76 Correct 455 ms 25620 KB Ok
77 Correct 692 ms 27884 KB Ok
78 Correct 507 ms 24912 KB Ok
79 Correct 441 ms 25024 KB Ok
80 Correct 533 ms 27292 KB Ok
81 Correct 528 ms 28156 KB Ok
82 Correct 506 ms 26780 KB Ok
83 Correct 598 ms 28760 KB Ok
84 Correct 535 ms 28316 KB Ok
85 Correct 493 ms 26292 KB Ok
86 Correct 519 ms 26476 KB Ok
87 Correct 515 ms 27988 KB Ok
88 Correct 488 ms 26076 KB Ok
89 Correct 516 ms 27976 KB Ok
90 Correct 590 ms 28464 KB Ok
91 Correct 580 ms 28496 KB Ok
92 Correct 599 ms 29920 KB Ok
93 Correct 586 ms 29564 KB Ok
94 Correct 495 ms 26676 KB Ok
95 Correct 726 ms 30820 KB Ok
96 Correct 555 ms 30336 KB Ok
97 Correct 565 ms 30164 KB Ok
98 Correct 606 ms 29968 KB Ok
99 Correct 643 ms 32388 KB Ok
100 Correct 558 ms 29980 KB Ok
101 Correct 650 ms 32292 KB Ok
102 Correct 605 ms 33128 KB Ok
103 Correct 569 ms 31436 KB Ok
104 Correct 636 ms 32564 KB Ok
105 Correct 635 ms 31928 KB Ok
106 Correct 609 ms 30980 KB Ok
107 Correct 742 ms 33348 KB Ok
108 Correct 586 ms 30424 KB Ok
109 Correct 544 ms 33336 KB Ok
110 Correct 757 ms 56340 KB Ok
111 Correct 883 ms 62040 KB Ok
112 Correct 866 ms 58368 KB Ok
113 Correct 916 ms 60524 KB Ok
114 Correct 836 ms 61772 KB Ok
115 Correct 903 ms 60500 KB Ok
116 Correct 874 ms 59532 KB Ok
117 Correct 917 ms 60364 KB Ok
118 Correct 804 ms 58284 KB Ok
119 Correct 847 ms 61104 KB Ok
120 Correct 879 ms 59124 KB Ok
121 Correct 879 ms 57956 KB Ok
122 Correct 902 ms 59972 KB Ok
123 Correct 855 ms 61776 KB Ok
124 Correct 752 ms 54508 KB Ok
125 Correct 698 ms 41332 KB Ok