結果

問題 No.186 中華風 (Easy)
ユーザー kotatsugamekotatsugame
提出日時 2020-09-17 16:54:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,112 bytes
コンパイル時間 828 ms
コンパイル使用メモリ 71,552 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-27 01:02:03
合計ジャッジ時間 1,608 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "a.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/186"
#include<iostream>
using namespace std;
#line 1 "/home/kotatsugame/library/math/linear_congruence.cpp"
//require extgcd
#include<vector>
#line 1 "/home/kotatsugame/library/math/extgcd.cpp"
template<typename T>
T extgcd(T a,T b,T&x,T&y)
{
	if(b==0)
	{
		x=1;
		y=0;
		return a;
	}
	T q=a/b;
	T g=extgcd(b,a-q*b,y,x);
	y-=q*x;
	return g;
}
#line 4 "/home/kotatsugame/library/math/linear_congruence.cpp"
pair<long long,long long>linear_congruence(const vector<int>&A,const vector<int>&B,const vector<int>&M)
//find Ax=B mod M. NA=>(0,-1)
{
	long long x=0,m=1;
	for(int i=0;i<A.size();i++)
	{
		long long a=m*A[i],b=B[i]-x*A[i],u,v;
		long long d=extgcd(a,(long long)M[i],u,v);
		if(b%d!=0)return make_pair(0LL,-1LL);
		long long t=b/d*u%(M[i]/d);
		x+=m*t;
		m*=M[i]/d;
	}
	x%=m;
	if(x<0)x+=m;
	return make_pair(x,m);
}
#line 5 "a.cpp"
int main()
{
	vector<int>r(3),m(3);
	for(int i=0;i<3;i++)cin>>r[i]>>m[i];
	pair<long,long>p=linear_congruence({1,1,1},r,m);
	if(p.second==-1)cout<<-1<<endl;
	else cout<<(p.first==0?p.second:p.first)<<endl;
}
0