結果

問題 No.187 中華風 (Hard)
ユーザー kotatsugamekotatsugame
提出日時 2019-09-26 23:43:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,983 bytes
コンパイル時間 967 ms
コンパイル使用メモリ 89,736 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 15:51:52
合計ジャッジ時間 2,912 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 76 ms
4,348 KB
testcase_14 AC 63 ms
4,348 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:56:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   56 | 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=m[i-1]%m[i];
		for(int j=0;j<i-1;j++)
		{
			(X-=v[j+1]*m[j])%=m[i];
			(M*=m[j])%=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<map>
map<int,int>M,S;
map<int,bool>isp;
main()
{
	int N;cin>>N;
	for(int i=0;i<N;i++)
	{
		int a,b;cin>>a>>b;
		for(int j=2;j*j<=b;j++)
		{
			if(b%j==0)
			{
				int p=1;
				while(b%j==0)
				{
					p*=j;
					b/=j;
				}
				isp[j]=true;
				S[j]=max(S[j],p);
				if(j!=p)S[p]=j;
				if(M.find(p)!=M.end())
				{
					if(M[p]!=a%p)
					{
						cout<<-1<<endl;
						return 0;
					}
				}
				else
				{
					M[p]=a%p;
				}
			}
		}
		if(b>1)
		{
			S[b]=max(S[b],b);
			isp[b]=true;
			if(M.find(b)!=M.end())
			{
				if(M[b]!=a%b)
				{
					cout<<-1<<endl;
					return 0;
				}
			}
			else M[b]=a%b;
		}
	}
	vector<long long>A,B;
	for(map<int,int>::iterator it=M.begin();it!=M.end();it++)
	{
		int b=it->second,a=it->first;
		int t=a;
		if(isp.find(t)==isp.end())t=S[t];
		t=S[t];
		if(M[t]%a!=b)
		{
			cout<<-1<<endl;
			return 0;
		}
	}
	for(map<int,bool>::iterator it=isp.begin();it!=isp.end();it++)
	{
		int a=S[it->first];
		int b=M[a];
		A.push_back(b);
		B.push_back(a);
	}
	//for(int i=0;i<A.size();i++)cout<<A[i]<<" "<<B[i]<<endl;
	cout<<garner(A,B,1e9+7)<<endl;
}
0