結果

問題 No.321 (P,Q)-サンタと街の子供たち
ユーザー 37zigen37zigen
提出日時 2016-05-21 15:03:50
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 2,785 bytes
コンパイル時間 2,442 ms
コンパイル使用メモリ 77,524 KB
実行使用メモリ 56,888 KB
最終ジャッジ日時 2024-04-16 04:44:19
合計ジャッジ時間 26,956 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 137 ms
41,312 KB
testcase_02 AC 137 ms
41,464 KB
testcase_03 AC 138 ms
41,192 KB
testcase_04 AC 137 ms
41,576 KB
testcase_05 RE -
testcase_06 AC 147 ms
41,380 KB
testcase_07 AC 143 ms
41,352 KB
testcase_08 AC 143 ms
41,192 KB
testcase_09 AC 144 ms
41,304 KB
testcase_10 AC 146 ms
41,364 KB
testcase_11 AC 150 ms
41,428 KB
testcase_12 AC 144 ms
41,300 KB
testcase_13 AC 145 ms
41,472 KB
testcase_14 AC 698 ms
49,588 KB
testcase_15 AC 869 ms
55,988 KB
testcase_16 AC 636 ms
48,960 KB
testcase_17 AC 188 ms
42,156 KB
testcase_18 AC 685 ms
49,280 KB
testcase_19 AC 756 ms
50,816 KB
testcase_20 AC 919 ms
55,272 KB
testcase_21 AC 738 ms
49,288 KB
testcase_22 AC 485 ms
48,388 KB
testcase_23 AC 800 ms
53,404 KB
testcase_24 AC 529 ms
48,380 KB
testcase_25 AC 681 ms
49,312 KB
testcase_26 AC 885 ms
54,796 KB
testcase_27 AC 807 ms
52,716 KB
testcase_28 AC 778 ms
52,276 KB
testcase_29 AC 903 ms
56,888 KB
testcase_30 AC 232 ms
45,612 KB
testcase_31 AC 212 ms
43,236 KB
testcase_32 AC 664 ms
49,492 KB
testcase_33 AC 683 ms
49,988 KB
testcase_34 AC 585 ms
48,868 KB
testcase_35 AC 800 ms
52,964 KB
testcase_36 AC 245 ms
45,996 KB
testcase_37 AC 747 ms
50,604 KB
testcase_38 AC 698 ms
49,160 KB
testcase_39 AC 425 ms
48,372 KB
testcase_40 AC 870 ms
54,164 KB
testcase_41 AC 537 ms
48,496 KB
testcase_42 AC 780 ms
53,100 KB
testcase_43 AC 649 ms
48,748 KB
testcase_44 AC 784 ms
52,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		new Main().solve();
	}
	void solve(){
		Scanner sc=new Scanner(System.in);
		int p=sc.nextInt();
		int q=sc.nextInt();
		int n=sc.nextInt();
		long[] x=new long[n];
		long[] y=new long[n];
		if(p==0){
			int ret=0;
			for(int i=0;i<n;i++){
				x[i]=sc.nextInt();
				y[i]=sc.nextInt();
				if(x[i]%q==0&&y[i]%q==0)ret++;
			}
			System.out.println(ret);
			return;
		}else if(q==0){
			int ret=0;
			for(int i=0;i<n;i++){
				x[i]=sc.nextInt();
				y[i]=sc.nextInt();
				if(x[i]%p==0&&y[i]%p==0)ret++;
			}
			System.out.println(ret);
			return;
		}
		long gcd=gcd(Math.abs(p),Math.abs(q));	
		for(int i=0;i<n;i++){
			x[i]=sc.nextInt();
			y[i]=sc.nextInt();
		}
		p/=gcd;q/=gcd;
		if(p%2==0||q%2==0){
			int ret=0;
			for(int i=0;i<n;i++){
				if(x[i]%gcd==0&&y[i]%gcd==0)ret++;
			}
			System.out.println(ret);
		}
		else{
			int ret=0;
			for(int i=0;i<n;i++){
				if(x[i]%gcd==0&&y[i]%gcd==0){
					x[i]/=gcd;
					y[i]/=gcd;
					if((x[i]+y[i])%2==0)ret++;
				}
			}
			System.out.println(ret);
		}

		/*
		 * 座標x,yがそれぞれ
		 * x=P(n+l)+Q(m+k)
		 * y=Q(n-l)+P(m-k)
		 * (n,mは任意の整数)の形で表すことができるなら、プレゼントが配られる。
		 * ここで、Pn+Qmはgcd(P,Q)*整数という形ですべて尽くされ、両者の表す整数の集合は一致する。
		 * よって、問題は、座標x,yが両方gcd(P,Q)で割り切れるものについてのみ考えればよい。
		 * P,Qをそれぞれgcd(P,Q)で割ったものをp,qと置く。x,yもgcdで割ってあるとする。
		 * ここで、(2P,0),(2Q,0)には必ずたどり着けることに注意する。すると、訪れることが可能な点から、
		 * x,yにそれぞれ任意の偶数を加えた点にも移動できることが分かる。
		 * よって(1,1),(1,0)に到達できるかのみ調べればよい。
		 * ①x=1=pn+qm
		 * ②1or0=qn+pm+(任意の偶数)
		 * これを満たすある整数k,l,m,nが存在すればよい。
		 * ①は必ず存在する。
		 * pが奇数,qが偶数のとき、mが偶数で、nは偶奇どちらの形にすることもできる。このとき②は1,0どちらも取れる。
		 * p,qが両方奇数の時、m,nの一方は奇数でもう一方は偶数。このとき、②は1しか取れない。
		 * よって(p+q)mod 2=0のとき、カラマツ模様に移動でき、
		 * p+q mod 2=1 のとき、すべての点に移動できる。
		 * 
		 */

	}
	long lcm(long t1,long t2){
		long a=gcd(t1,t2);
		long tt1=t1/a;
		long tt2=t2/a;
		return tt1*tt2*a;
	}
	long gcd(long t1,long t2){
		if(t1<t2){
			long d=t2;
			t2=t1;
			t1=d;
		}
		if(t2==0)return t1;
		return gcd(t2,t1%t2);
	}
}
0