結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー furafura
提出日時 2020-02-14 21:48:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,676 bytes
コンパイル時間 2,142 ms
コンパイル使用メモリ 220,716 KB
実行使用メモリ 48,636 KB
最終ジャッジ日時 2024-04-27 19:31:52
合計ジャッジ時間 5,042 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
7,688 KB
testcase_01 AC 19 ms
46,580 KB
testcase_02 AC 17 ms
46,552 KB
testcase_03 AC 9 ms
7,688 KB
testcase_04 RE -
testcase_05 AC 9 ms
7,560 KB
testcase_06 AC 18 ms
46,748 KB
testcase_07 AC 17 ms
46,712 KB
testcase_08 AC 9 ms
7,564 KB
testcase_09 AC 8 ms
7,568 KB
testcase_10 AC 38 ms
8,204 KB
testcase_11 AC 57 ms
47,896 KB
testcase_12 RE -
testcase_13 AC 47 ms
47,148 KB
testcase_14 AC 64 ms
8,844 KB
testcase_15 AC 38 ms
8,200 KB
testcase_16 AC 47 ms
8,536 KB
testcase_17 AC 34 ms
7,944 KB
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 99 ms
9,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

template<class T> T gcd(const T& a,const T& b){ return b==0?a:gcd(b,a%b); }

class linear_sieve{
	vector<int> lpf,p;
public:
	linear_sieve(int n):lpf(n+1){
		for(int i=2;i<=n;i++){
			if(lpf[i]==0){
				lpf[i]=i;
				p.emplace_back(i);
			}
			for(int j=0;j<p.size()&&p[j]<=lpf[i]&&i*p[j]<=n;j++) lpf[i*p[j]]=p[j];
		}
	}

	const vector<int>& primes()const{ return p; }

	bool is_prime(int a)const{
		assert(a<=(int)lpf.size()-1);
		return a>0 && lpf[a]==a;
	}

	map<int,int> prime_factorize(int a)const{
		assert(a<=(int)lpf.size()-1);
		map<int,int> pf;
		for(;a>1;a/=lpf[a]) ++pf[lpf[a]];
		return pf;
	}
};

template<class T>
vector<T> divisors(const map<T,int>& pf){
	vector<T> res={T(1)};
	for(const auto& q:pf){
		int m=res.size();
		T pp=1;
		rep(i,q.second){
			pp*=q.first;
			rep(i,m) res.emplace_back(res[i]*pp);
		}
	}
	sort(res.begin(),res.end());
	return res;
}

int main(){
	linear_sieve LS(1e6);

	int n,m;
	lint k;
	char op;
	cin>>n>>m>>k>>op;
	vector<lint> b(m),a(n);
	rep(j,m) cin>>b[j], b[j]%=k;
	rep(i,n) cin>>a[i], a[i]%=k;

	sort(b.begin(),b.end());

	lint ans=0;
	if(op=='+'){
		rep(i,n){
			auto p=equal_range(b.begin(),b.end(),(k-a[i])%k);
			ans+=p.second-p.first;
		}
	}
	else{
		vector<int> cnt(1e7);
		rep(j,m) for(const auto& d:divisors(LS.prime_factorize(b[j]!=0?b[j]:k))) if(d<1e6) cnt[d]++;

		rep(i,n){
			lint g=gcd(a[i],k);
			if(k/g<1e7){
				ans+=cnt[k/g];
			}
			else{
				for(lint x=0;x<k;x+=k/g){
					auto p=equal_range(b.begin(),b.end(),x);
					ans+=p.second-p.first;
				}
			}
		}
	}
	cout<<ans<<'\n';

	return 0;
}
0