結果
| 問題 |
No.321 (P,Q)-サンタと街の子供たち
|
| コンテスト | |
| ユーザー |
夕叢霧香(ゆうむらきりか)
|
| 提出日時 | 2018-03-12 17:58:39 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 95 ms / 2,000 ms |
| コード長 | 1,259 bytes |
| コンパイル時間 | 598 ms |
| コンパイル使用メモリ | 69,604 KB |
| 最終ジャッジ日時 | 2025-01-05 09:15:23 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 41 |
ソースコード
#include<iostream>
using namespace std;
typedef long long lint;
typedef pair<lint,lint>pll;
// a/bに一番近い整数を返す。
lint quo_nearest(lint a,lint b){
lint q=a/b;
lint r=a-q*b;
if(abs(r)>abs(r-b))return q+1;
if(abs(r)>abs(r+b))return q-1;
return q;
}
// ガウス整数の剰余を計算する。一番絶対値が小さいものを返す。
pll rem_gaussian(pll a,pll b){
lint bnorm=b.first*b.first+b.second*b.second;
lint x=a.first*b.first+a.second*b.second;
lint y=-a.first*b.second+a.second*b.first;
// a/bに一番近いガウス整数をqとして、a-q*bを計算する。a/b=(x+yi)/bnorm なので、x/bnorm,y/bnormに一番近い整数がわかればよい。
lint qx=quo_nearest(x,bnorm);
lint qy=quo_nearest(y,bnorm);
return pll(a.first-qx*b.first+qy*b.second,a.second-qx*b.second-qy*b.first);
}
// ガウス整数の最大公約数を計算する。
pll gcd_gaussian(pll a,pll b){
while(b!=pll(0,0)){
pll c=rem_gaussian(a,b);
a=b;b=c;
}
return a;
}
int main(){
lint p,q;
int n;
cin>>p>>q>>n;
pll g=gcd_gaussian(pll(p,q),pll(p,-q));
int ans=0;
while(n--){
lint x,y;
cin>>x>>y;
ans+=g==pll(0,0)?(x==0&&y==0):rem_gaussian(pll(x,y),g)==pll(0,0);
}
cout<<ans<<endl;
}
夕叢霧香(ゆうむらきりか)