結果

問題 No.321 (P,Q)-サンタと街の子供たち
ユーザー 37zigen37zigen
提出日時 2016-05-21 15:05:33
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 2,783 bytes
コンパイル時間 2,345 ms
コンパイル使用メモリ 76,844 KB
実行使用メモリ 58,764 KB
最終ジャッジ日時 2024-04-16 04:44:47
合計ジャッジ時間 26,322 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 137 ms
40,956 KB
testcase_02 AC 136 ms
41,288 KB
testcase_03 AC 137 ms
41,168 KB
testcase_04 AC 139 ms
41,336 KB
testcase_05 RE -
testcase_06 AC 143 ms
41,116 KB
testcase_07 AC 136 ms
41,448 KB
testcase_08 AC 140 ms
41,364 KB
testcase_09 AC 141 ms
41,120 KB
testcase_10 AC 142 ms
41,584 KB
testcase_11 AC 141 ms
41,180 KB
testcase_12 AC 139 ms
41,116 KB
testcase_13 AC 141 ms
41,424 KB
testcase_14 AC 686 ms
51,236 KB
testcase_15 AC 866 ms
56,672 KB
testcase_16 AC 621 ms
49,140 KB
testcase_17 AC 188 ms
41,612 KB
testcase_18 AC 641 ms
49,312 KB
testcase_19 AC 643 ms
51,460 KB
testcase_20 AC 879 ms
54,652 KB
testcase_21 AC 720 ms
49,740 KB
testcase_22 AC 476 ms
48,216 KB
testcase_23 AC 777 ms
53,616 KB
testcase_24 AC 525 ms
48,316 KB
testcase_25 AC 666 ms
49,636 KB
testcase_26 AC 877 ms
55,804 KB
testcase_27 AC 756 ms
52,336 KB
testcase_28 AC 760 ms
52,452 KB
testcase_29 AC 888 ms
58,764 KB
testcase_30 AC 223 ms
45,064 KB
testcase_31 AC 211 ms
42,844 KB
testcase_32 AC 664 ms
48,900 KB
testcase_33 AC 683 ms
49,112 KB
testcase_34 AC 587 ms
48,504 KB
testcase_35 AC 764 ms
53,468 KB
testcase_36 AC 246 ms
45,588 KB
testcase_37 AC 704 ms
50,576 KB
testcase_38 AC 639 ms
49,740 KB
testcase_39 AC 434 ms
48,228 KB
testcase_40 AC 838 ms
54,624 KB
testcase_41 AC 528 ms
48,308 KB
testcase_42 AC 778 ms
53,212 KB
testcase_43 AC 607 ms
48,384 KB
testcase_44 AC 782 ms
52,564 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