結果

問題 No.187 中華風 (Hard)
ユーザー kotatsugamekotatsugame
提出日時 2019-09-27 00:22:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 111 ms / 3,000 ms
コード長 1,727 bytes
コンパイル時間 920 ms
コンパイル使用メモリ 81,936 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 15:53:10
合計ジャッジ時間 2,841 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
4,348 KB
testcase_01 AC 12 ms
4,348 KB
testcase_02 AC 32 ms
4,348 KB
testcase_03 AC 31 ms
4,348 KB
testcase_04 AC 74 ms
4,348 KB
testcase_05 AC 71 ms
4,348 KB
testcase_06 AC 73 ms
4,348 KB
testcase_07 AC 72 ms
4,348 KB
testcase_08 AC 111 ms
4,348 KB
testcase_09 AC 111 ms
4,348 KB
testcase_10 AC 109 ms
4,348 KB
testcase_11 AC 70 ms
4,348 KB
testcase_12 AC 73 ms
4,348 KB
testcase_13 AC 75 ms
4,348 KB
testcase_14 AC 62 ms
4,348 KB
testcase_15 AC 17 ms
4,348 KB
testcase_16 AC 18 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 12 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 61 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 72 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:57:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   57 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
using namespace std;
#include<vector>
long long invmod(long long a,long long m)
{
	long long s=a%m,t=m,sx=1,sy=0,tx=0,ty=1;
	while(s%t!=0)
	{
		long long f=s/t;
		long long u=s-t*f,ux=sx-tx*f,uy=sy-ty*f;
		s=t,sx=tx,sy=ty;
		t=u,tx=ux,ty=uy;
	}
	if(tx<0)tx+=m;
	return tx;
}
long long garner(const vector<long long>&x,const vector<long long>&m,const int mod=0)//*=x mod m
{
	vector<long long>v(x.size());
	v[0]=x[0];
	for(int i=1;i<x.size();i++)
	{
		long long X=(x[i]-x[0])%m[i];
		long long M=1;
		for(int j=0;j<i-1;j++)
		{
			(M*=m[j])%=m[i];
			(X-=v[j+1]*M)%=m[i];
		}
		(M*=m[i-1])%=m[i];
		if(X<0)X+=m[i];
		v[i]=X*invmod(M,m[i])%m[i];
	}
	long long ret=v[0],p=1;
	if(mod==0)
	{
		for(int i=1;i<x.size();i++)
		{
			p*=m[i-1];
			ret+=p*v[i];
		}
	}
	else
	{
		ret%=mod;
		for(int i=1;i<x.size();i++)
		{
			(p*=m[i-1])%=mod;
			(ret+=p*v[i])%=mod;
		}
	}
	return ret;
}
#include<set>
vector<int>A,B;
set<int>pri;
main()
{
	int N;cin>>N;
	for(int i=0;i<N;i++)
	{
		int a,b;cin>>a>>b;
		A.push_back(a);
		B.push_back(b);
		for(int j=2;j*j<=b;j++)
		{
			if(b%j==0)
			{
				pri.insert(j);
				int p=1;
				while(b%j==0)b/=j;
			}
		}
		if(b>1)pri.insert(b);
	}
	vector<long long>a,b;
	bool flag=true;
	long long mod=1e9+7,ret=1;
	for(int p:pri)
	{
		int get=-1,ma=1;
		for(int i=0;i<N;i++)
		{
			if(B[i]%p==0)
			{
				int t=1;
				while(B[i]%p==0)
				{
					B[i]/=p;
					t*=p;
				}
				if(get>=0)
				{
					if(get%p!=A[i]%p)
					{
						cout<<-1<<endl;
						return 0;
					}
				}
				if(ma<t)
				{
					get=A[i]%t;
					ma=t;
				}
				A[i]%=B[i];
			}
		}
		a.push_back(get);
		b.push_back(ma);
		if(get>0)flag=false;
		(ret*=ma)%=mod;
	}
	if(!flag)ret=garner(a,b,mod);
	cout<<ret<<endl;
}
0