提出 #72041479


ソースコード 拡げる

/**
	author:  shobonvip
	created: 2025.12.27 21:36:56
**/

#include<bits/stdc++.h>
using namespace std;

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)
#define all(v) v.begin(), v.end()

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

mint solve(ll n, ll m, ll k, vector<int> a, vector<int> b) {

	if (n > m) {
		swap(n, m);
		swap(a, b);
	}

	ll gcd = __gcd(n, m);

	if (gcd > 1) {
		vector ag(gcd, vector<int>(n / gcd));
		vector bg(gcd, vector<int>(m / gcd));
		rep(i,0,n) ag[i % gcd][i / gcd] = a[i];
		rep(i,0,m) bg[i % gcd][i / gcd] = b[i];
		mint ans = 0;
		rep(i,0,gcd) {
			ll times = (k - i + gcd - 1) / gcd;
			ans += solve(n/gcd, m/gcd, times, ag[i], bg[i]);
		}
		return ans;
	}

	vector<int> p;
	vector<bool> seen(m);
	rep(i,0,m) {
		if (seen[i]) continue;
		int x = i;
		while (!seen[x]) {
			seen[x] = 1;
			p.emplace_back(x);
			x += n;
			x %= m;
		}
	}

	vector<int> q(m);
	rep(i,0,m) q[p[i]] = i;

	int sz = 1;
	while (sz < 2*m) sz *= 2;
	vector<mint> fw1(2*sz);

	ll lcm = n * m;
	ll kuri = k / lcm;
	ll amari = k % lcm;
	vector<pair<ll,ll>> piv;
	rep(i,0,n) piv.emplace_back(pair(a[i], 0));
	rep(i,0,m) piv.emplace_back(pair(b[i], 1));
	sort(all(piv));
	reverse(all(piv));
	mint kurisum = 0;
	mint anum = 0;
	mint bnum = 0;

	rep(i,0,n+m) {
		if (piv[i].second == 0) {
			kurisum += bnum * piv[i].first;
			anum++;
		}
		if (piv[i].second == 1) {
			kurisum += anum * piv[i].first;
			bnum++;
		}
	}

	mint ans = kurisum * kuri;
	vector que(2*sz, vector<pair<ll,int>>(0));
	rep(i,0,2*m) {
		int x = i + sz;
		while (x > 0) {
			que[x].emplace_back(b[p[i%m]], 1);
			fw1[x] += b[p[i%m]];
			x >>= 1;
		}
	}

	rep(i,0,n) {
		int f = i % m;
		ll var = (amari - i + n - 1) / n;
		ll l = q[f];
		ll r = q[f] + var;

		//cout << l << ' ' << r << endl;
		l += sz, r += sz;
		while (l < r) {
			if (l & 1) {
				que[l].emplace_back(a[i], 0);
				l++;
			}
			if (r & 1) {
				r--;
				que[r].emplace_back(a[i], 0);
			}
			l >>= 1;
			r >>= 1;
		}
	}

	rep(i,0,2*sz) {
		sort(all(que[i]));
		reverse(all(que[i]));
	}

	rep(i,0,2*sz) {
		mint now_nums = 0;
		mint now_sums = fw1[i];
		for (auto [x,t]: que[i])  {
			if (t == 1) {	
				now_sums -= x;
				now_nums += 1;
			} else {
				ans += now_sums + now_nums * x;
			}
		}
	}

	return ans;

}

mint naive(ll n, ll m, ll k, vector<int> a, vector<int> b) {
	mint ans=0;
	rep(i,0,k) {
		ans+=min(a[i%n],b[i%m]);
	}
	return ans;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	ll n, m, k;
	cin >> n >> m >> k;
	vector<int> a(n), b(m);
	rep(i,0,n) {
		cin >> a[i];
	}
	rep(i,0,m) {
		cin >> b[i];
	}
	
	mint ans = solve(n, m, k, a, b);
	cout << ans.val() << endl;
	//cout << naive(n,m,k,a,b).val()<<endl;
	
}

提出情報

提出日時
問題 G - Sum of Min
ユーザ shobonvip
言語 C++23 (GCC 15.2.0)
得点 575
コード長 3793 Byte
結果 AC
実行時間 653 ms
メモリ 234844 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 575 / 575
結果
AC × 3
AC × 33
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3584 KiB
00_sample_01.txt AC 1 ms 3548 KiB
00_sample_02.txt AC 1 ms 3516 KiB
01_random_00.txt AC 1 ms 3604 KiB
01_random_01.txt AC 1 ms 3568 KiB
01_random_02.txt AC 1 ms 3516 KiB
01_random_03.txt AC 247 ms 175992 KiB
01_random_04.txt AC 84 ms 28320 KiB
01_random_05.txt AC 83 ms 28332 KiB
01_random_06.txt AC 249 ms 175952 KiB
01_random_07.txt AC 251 ms 177364 KiB
01_random_08.txt AC 264 ms 175948 KiB
01_random_09.txt AC 267 ms 177912 KiB
01_random_10.txt AC 247 ms 175868 KiB
01_random_11.txt AC 322 ms 66040 KiB
01_random_12.txt AC 569 ms 94700 KiB
01_random_13.txt AC 321 ms 20020 KiB
01_random_14.txt AC 55 ms 25136 KiB
01_random_15.txt AC 532 ms 43180 KiB
01_random_16.txt AC 637 ms 114976 KiB
01_random_17.txt AC 299 ms 99956 KiB
01_random_18.txt AC 617 ms 45204 KiB
01_random_19.txt AC 551 ms 166996 KiB
01_random_20.txt AC 653 ms 103392 KiB
01_random_21.txt AC 83 ms 28312 KiB
01_random_22.txt AC 84 ms 28276 KiB
01_random_23.txt AC 84 ms 28368 KiB
01_random_24.txt AC 586 ms 234844 KiB
01_random_25.txt AC 190 ms 46740 KiB
01_random_26.txt AC 43 ms 11640 KiB
01_random_27.txt AC 67 ms 16384 KiB
01_random_28.txt AC 65 ms 24440 KiB
01_random_29.txt AC 574 ms 176876 KiB