跳到正文

题解

题解:P15754 [JAG 2025 Summer Camp #1] Dyad

xiaozhengguoaaa约 3 分钟
原文

这是 2026年3月14日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年3月14日
收录
2026年3月14日
更新
2026年11月14日
篇幅
约 3 分钟
获赞
1
题目
P15754

版本

  1. v2当前
  2. v1

签到题。

题目大意

有 NN 种产品,每种包含两颗糖果,选择两种不同产品使得四颗糖果能平分给双胞胎且两人糖果类型完全相同。

主要思路

将每对糖果排序保证 x≤yx \leq y,用 map 统计每对 (x,y)(x, y) 出现次数,另用 map 统计相同类型对 (x,x)(x, x) 出现次数。

相同类型对 (x,x)(x, x) 内部组合:从 cntcnt 个中选 22 个。

不同类型对 (x,y)(x, y) 内部组合:从 cntcnt 个中选 22 个。

不同类型对 (x,x)(x, x) 和 (y,y)(y, y) 之间的组合:用总和与平方和计算所有不同 x,yx, y 的组合数。

AC code

C++
#include <bits/stdc++.h>
#define int long long
#define fst ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 4e6 + 7;
const int MOD = 998244353;
const int INF = 0X3F3F3F3F;
const int dx[] = {-1, 1, 0, 0, -1, -1, +1, +1};
const int dy[] = {0, 0, -1, 1, -1, +1, -1, +1};

int a[N] , c[N] , b[N] ;

bool v[N] ;

int n , k , m ;

int t ;

map<PII, int> mp;

map<int, int> sm;

inline int read ()
{
	int s = 0 , w = 1 ;
	char ch = getchar () ;
	while (ch < '0' || ch > '9')
	{
		if (ch == '-')
			w = -1;
		ch = getchar () ;
	}
	while (ch >= '0' && ch <= '9')
	{
		s = s * 10 + ch - '0' ;
		ch = getchar ();
	}
	return s * w ;
}
inline void write (int x)
{
	if (x < 0) putchar ('-') , x = -x ;
	if (x > 9) write (x / 10) ;
	putchar (x % 10 + '0') ;
	return ;
}

signed main()
{
	fst ;
	cin >> n;
	for(int i = 1; i <= n; i ++)
	{
		int x, y;
		cin >> x >> y;
		if(x > y) swap(x, y);
		mp[{x, y}] ++;
		if(x == y) sm[x] ++;
	}
	int ans = 0;
	int sum = 0, sum2 = 0;
	for(auto &p : sm)
	{
		int c = p.second;
		if(c >= 2) ans += c * (c - 1) / 2;
		sum += c;
		sum2 += c * c;
	}
	ans += (sum * sum - sum2) / 2;
	for(auto &p : mp)
	{
		int x = p.first.first, y = p.first.second, c = p.second;
		if(x == y) continue;
		if(c >= 2) ans += c * (c - 1) / 2;
	}
	cout << ans;
    return 0;
}