結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー furafura
提出日時 2020-02-14 22:11:09
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 115 ms / 2,000 ms
コード長 1,060 bytes
コンパイル時間 2,655 ms
コンパイル使用メモリ 202,340 KB
最終ジャッジ日時 2025-01-09 00:17:21
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 19
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:26:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   26 |         rep(j,m) scanf("%lld",&b[j]), b[j]%=k;
      |                  ~~~~~^~~~~~~~~~~~~~
main.cpp:27:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   27 |         rep(i,n) scanf("%lld",&a[i]), a[i]%=k;
      |                  ~~~~~^~~~~~~~~~~~~~

ソースコード

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); }

vector<long long> divisors(long long a){
	vector<long long> res;
	for(long long i=1;i*i<=a;i++) if(a%i==0) {
		res.emplace_back(i);
		if(i*i<a) res.emplace_back(a/i);
	}
	sort(res.begin(),res.end());
	return res;
}

int main(){
	int n,m;
	lint k;
	char op;
	cin>>n>>m>>k>>op;
	vector<lint> b(m),a(n);
	rep(j,m) scanf("%lld",&b[j]), b[j]%=k;
	rep(i,n) scanf("%lld",&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{
		auto D=divisors(k);
		vector<int> cnt(D.size());
		rep(j,m){
			b[j]=gcd(b[j],k);
			cnt[lower_bound(D.begin(),D.end(),b[j])-D.begin()]++;
		}
		rep(i,D.size()) rep(j,i) if(D[i]%D[j]==0) cnt[j]+=cnt[i];

		rep(i,n){
			ans+=cnt[lower_bound(D.begin(),D.end(),k/gcd(a[i],k))-D.begin()];
		}
	}
	printf("%lld\n",ans);

	return 0;
}
0