結果

問題 No.1880 Many Ways
ユーザー 37zigen37zigen
提出日時 2021-12-30 10:30:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 168 ms / 2,000 ms
コード長 5,329 bytes
コンパイル時間 2,602 ms
コンパイル使用メモリ 85,676 KB
実行使用メモリ 42,668 KB
最終ジャッジ日時 2024-04-21 06:29:16
合計ジャッジ時間 16,882 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
40,112 KB
testcase_01 AC 115 ms
41,752 KB
testcase_02 AC 137 ms
42,504 KB
testcase_03 AC 137 ms
41,956 KB
testcase_04 AC 129 ms
42,180 KB
testcase_05 AC 130 ms
41,800 KB
testcase_06 AC 142 ms
42,632 KB
testcase_07 AC 143 ms
42,192 KB
testcase_08 AC 149 ms
42,316 KB
testcase_09 AC 153 ms
42,592 KB
testcase_10 AC 168 ms
42,668 KB
testcase_11 AC 153 ms
42,512 KB
testcase_12 AC 151 ms
42,448 KB
testcase_13 AC 147 ms
41,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Scanner;

public class Main { 
    public static void main(String[] args) throws IOException {
//    	new Main().judge(args);
    	Scanner sc=new Scanner(System.in);
    	build(sc.nextLong());
//    	new Main().random();
    	sc.close();
    }
    
    void random() {
    	long A=0;
    	
    	Random rnd=new Random();
    	for (int i=0;i<40;++i) {
    		if (rnd.nextBoolean())
    			A|=1L<<i;
    	}
    	System.out.println(A);
    }
    
    void judge(String[] args) throws IOException {
		File file_in = new File(args[0]);
		BufferedReader br_in = new BufferedReader(new FileReader(file_in));
		long A=Long.parseLong(br_in.readLine());
		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		int[][] a=new int[n][n];
		for (int i=0;i<m;++i) {
			int u=sc.nextInt()-1;
			int v=sc.nextInt()-1;
			assert(u != v);
			assert(a[u][v]==0 && a[v][u]==0);
			a[u][v]=a[v][u]=1;
		}
		assert(calc(n, a) == A);
		br_in.close();
	}

	static void build(long A) {
		if (A == 0) {
			System.out.println("2 0");
			return;
		} else if (A == 1) {
			System.out.println("2 1");
			System.out.println("1 2");
			return;
		}
		int n=1;
		while ((1L<<n)<=A) ++n;
		int N=2+3*(n-1);
		int[][] a=new int[N][N];
		for (int i=1;i<=3;++i) {
			a[0][i]=a[i][0]=1;
		}
		for (int i=1;i<3;++i)
			a[3*(n-2)+i][N-1]=a[N-1][3*(n-2)+i]=1;
		for (int i=0;i<n-2;++i) {
			int p=3*i+1;
			int q=3*i+2;
			int r=3*(i+1)+1;
			int s=3*(i+1)+2;
			for (int u : new int[] {p, q}) {
				for (int v : new int[] {r, s}) {
					a[u][v]=a[v][u]=1;
				}	
			}
		}
		for (int i=0;i<=n-2;++i) {
			a[3*i][3*(i+1)]=a[3*(i+1)][3*i]=1;
		}
		for (int i=0;i<=n-2;++i) {
			if ((A>>i)%2==1) {
				int u=3*(n-1-i)+1;
				int v=3*(n-1-i)+2;
				int p=3*(n-1-i-1)+3;
				u=Math.min(u, N-1);
				v=Math.min(v, N-1);
				a[u][p]=a[p][u]=1;
				a[v][p]=a[p][v]=1;
			}
		}
		int M=0;
		for (int i=0;i<N;++i) for (int j=i+1;j<N;++j) {
			if (a[i][j]==1) ++M;
		}
		System.out.println(N + " " + M);
		for (int i=0;i<N;++i) for (int j=i+1;j<N;++j) {
			if (a[i][j]==1) System.out.println((i+1)+" "+(j+1));
		}
	}
	
	static long calc(int n, int[][] a) {
		int[][] d=new int[n][n];
		for (int i=0;i<n;++i) for (int j=0;j<n;++j) if (i!=j) d[i][j]=n;
		for (int i=0;i<n;++i) {
			for (int j=0;j<n;++j) {
				if (a[i][j] == 1) d[i][j]=1;
			}
		}
		for (int k=0;k<n;++k) {
			for (int i=0;i<n;++i) {
				for (int j=0;j<n;++j) {
					d[i][j]=Math.min(d[i][j], d[i][k]+d[k][j]);
				}
			}
		}
		boolean[] vis=new boolean[n];
		ArrayDeque<Integer> que=new ArrayDeque<>();
		int src = 0;
		que.add(src);
		long[] cnt=new long[n];
		cnt[src]=1;
		vis[src]=true;
		while (!que.isEmpty()) {
			int v = que.pollFirst();
			for (int u=0;u<n;++u) {
				if (a[v][u] == 1 && d[src][u] == d[src][v] + 1) {
					if (!vis[u]) que.addLast(u);
					vis[u]=true;
					cnt[u]+=cnt[v];
				}
			}
		}
		return cnt[n-1];
	}
		
    static void tr(Object...o) {
    	System.out.println(Arrays.deepToString(o));
    }
}



class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() { return Double.parseDouble(next());}
}
0