結果

問題 No.1144 Triangles
ユーザー 37zigen37zigen
提出日時 2020-08-04 17:47:20
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 5,061 bytes
コンパイル時間 2,537 ms
コンパイル使用メモリ 82,340 KB
実行使用メモリ 59,900 KB
最終ジャッジ日時 2023-10-12 21:19:39
合計ジャッジ時間 45,250 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 TLE -
testcase_05 WA -
testcase_06 TLE -
testcase_07 WA -
testcase_08 TLE -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.awt.geom.Line2D;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Random;

public class Main {
	
	public static void main(String[] args) {
		new Main().run();
	}
	
	final long MOD=(long)1e9+7;

	long pow(long a,long n) {
		return n!=0?pow(a*a%MOD,n/2)*(n%2==1?a:1)%MOD:1;
	}
	
	long inv(long a) {
		return pow(a,MOD-2);
	}
	
	long ADD(long a,long b) {
		long ret=a+b;
		if (ret<0) ret+=MOD;
		return ret<MOD?ret:ret-MOD;
	}
	
	int quadrant(long x,long y) {
		if (x==0 && y==0) return -1;
		if (y>=0 && x> 0) return 0;
		if (y> 0 && x<=0) return 1;
		if (y<=0 && x< 0) return 2;
		if (y< 0 && x>=0) return 3;
		throw new AssertionError();
	}
	
	
	void run() {
		FastScanner sc=new FastScanner();
		int n=sc.nextInt();
		long[][] a=new long[n][2];
		for (int i=0;i<n;++i) {
			a[i][0]=sc.nextLong();
			a[i][1]=sc.nextLong();
		}
		System.out.println(solve(n, a));
//		rnd();
	}
	
	void rnd() {
		while (true) {
			Random seed=new Random();
			int n=5;
			long[][] a=new long[n][2];
			for (int i=0;i<n;++i) {
				a[i][0]=seed.nextInt(2)-1;
				a[i][1]=seed.nextInt(2)-1;
			}
			long ans1=solve(n,a);
			long ans2=brute(n,a);
			if (ans1!=ans2) {
				System.out.println("ans1 "+ans1);
				System.out.println("ans2 "+ans2);
				tr(a);
				return;
			}
		}
	}
	
	long brute(int n,long[][] a) {
		long ret=0;
		for (int i=0;i<n;++i) {
			for (int j=i+1;j<n;++j) {
				for (int k=j+1;k<n;++k) {
					long x1=a[j][0]-a[i][0];
					long y1=a[j][1]-a[i][1];
					long x2=a[k][0]-a[i][0];
					long y2=a[k][1]-a[i][1];
					long add=x1*y2-x2*y1;
					if (add<0) add=MOD-add%MOD;
					add%=MOD;
					ret=(ret+add)%MOD;
				}
			}
		}
		return ret;
	}
	
	long solve(int n,long[][] a) {
		long[] sumx=new long[2*(n-1)+1];
		long[] sumy=new long[2*(n-1)+1];
		long ans=0;
		for (int i=0;i<n;++i) {
			long[][] p=new long[n-1][2];
			{
				int pointer=0;
				for (int j=0;j<n;++j) {
					if (i==j) continue;
					p[pointer][0]=a[j][0]-a[i][0];
					p[pointer][1]=a[j][1]-a[i][1];
					++pointer;
				}
			}
			Arrays.sort(p,new Comparator<long[]>() {
				@Override
				public int compare(long[] o1, long[] o2) {
					if (quadrant(o1[0], o1[1])==quadrant(o2[0], o2[1])) {
						return Line2D.relativeCCW(0, 0, o1[0], o1[1], o2[0], o2[1]);
					} else {
						return Integer.compare(quadrant(o1[0], o1[1]), quadrant(o2[0], o2[1]));
					}
				}
			});
			
			for (int j=1;j<=2*p.length;++j) {
				sumx[j]=ADD(p[(j-1)%(n-1)][0],sumx[j-1]);
				sumy[j]=ADD(p[(j-1)%(n-1)][1],sumy[j-1]);
			}
			int t=0;
			for (int j=0;j<n-1;++j) {
				t=Math.max(t, j+1);
				long x0=p[j][0];
				long y0=p[j][1];
				if (x0==0&&y0==0) continue;
				while (t!=j+(n-1)) {
					if ((p[j][0]*p[t%(n-1)][1]==p[j][1]*p[t%(n-1)][0])) {
						if (p[j][0]*p[(t+n-2)%(n-1)][1]==p[j][1]*p[(t+n-2)%(n-1)][0]) {
							++t;
						}
						else break;
					} else if (Line2D.relativeCCW(0, 0, p[j][0], p[j][1], p[t%(n-1)][0], p[t%(n-1)][1])<=0) {
						++t;					
					} else {
						break;
					}
				}
				long dsx=sumx[t]-sumx[j+1];
				long dsy=sumy[t]-sumy[j+1];
				long add=x0*dsy%MOD-y0*dsx%MOD;
				if (add<0) add+=MOD;
//				tr("add"+add);
				ans=ADD(ans,add);
			}
		}
		tr(ans);
		return ans*inv(3)%MOD;
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

	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 boolean isPrintableChar(int c) {
			return 33 <= c && c <= 126;
		}

		private void skipUnprintable() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr]))
				ptr++;
		}

		public boolean hasNext() {
			skipUnprintable();
			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() {
			return (int) nextLong();
		}
	}
}
0