結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
4,348 KB
testcase_01 AC 13 ms
4,348 KB
testcase_02 AC 33 ms
4,348 KB
testcase_03 AC 32 ms
4,348 KB
testcase_04 AC 74 ms
4,348 KB
testcase_05 AC 68 ms
4,348 KB
testcase_06 AC 72 ms
4,348 KB
testcase_07 AC 73 ms
4,348 KB
testcase_08 AC 110 ms
4,348 KB
testcase_09 AC 111 ms
4,348 KB
testcase_10 AC 109 ms
4,348 KB
testcase_11 AC 72 ms
4,348 KB
testcase_12 AC 75 ms
4,348 KB
testcase_13 AC 75 ms
4,348 KB
testcase_14 AC 63 ms
4,348 KB
testcase_15 AC 18 ms
4,348 KB
testcase_16 AC 18 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 11 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 71 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:112:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  112 | main()
      | ^~~~
main.cpp: In function 'long long int garner_helper(std::vector<int>, std::vector<int>, int, bool)':
main.cpp:98:31: warning: 'res' may be used uninitialized [-Wmaybe-uninitialized]
   98 |                         if(res%q.first!=q.second)return-1;
      |                            ~~~^~~~~~~~
main.cpp:79:26: note: 'res' was declared here
   79 |                 int ma=1,res;
      |                          ^~~

ソースコード

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
{
	if(x.empty())return 0LL;
	vector<long long>v(x.size());
	v[0]=x[0];
	for(int i=1;i<x.size();i++)
	{
		long long X=x[i];
		long long M=1;
		for(int j=0;j<i;j++)
		{
			(X-=v[j]*M)%=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<algorithm>
long long garner_helper(vector<int>A,vector<int>B,const int mod=0,bool zero=true)//allow zero?
//make B disjoint. NA => -1
{
	vector<int>primes;
	for(int i=0;i<A.size();i++)
	{
		int b=B[i];
		for(int j=2;j*j<=b;j++)
		{
			if(b%j==0)
			{
				primes.push_back(j);
				while(b%j==0)b/=j;
			}
		}
		if(b>1)primes.push_back(b);
	}
	sort(primes.begin(),primes.end());
	primes.erase(unique(primes.begin(),primes.end()),primes.end());
	vector<long long>a,b;
	bool flag=!zero;
	long long ret=1;
	for(int p:primes)
	{
		int ma=1,res;
		vector<pair<int,int> >x;
		for(int i=0;i<A.size();i++)
		{
			if(B[i]%p==0)
			{
				int t=1;
				while(B[i]%p==0)
				{
					B[i]/=p;
					t*=p;
				}
				x.push_back(make_pair(t,A[i]%t));
				if(ma<t)ma=t,res=x.back().second;
				A[i]%=B[i];
			}
		}
		for(pair<int,int>q:x)
		{
			if(res%q.first!=q.second)return-1;
		}
		a.push_back(res);
		b.push_back(ma);
		flag&=res==0;
		if(flag)
		{
			ret*=ma;
			if(mod!=0)ret%=mod;
		}
	}
	if(!flag)ret=garner(a,b,mod);
	return ret;
}
main()
{
	int N;cin>>N;
	vector<int>A(N),B(N);
	for(int i=0;i<N;i++)cin>>A[i]>>B[i];
	cout<<garner_helper(A,B,1e9+7,false)<<endl;
}
0