ural 1932 The Secret of Identifier 容斥

题目链接:点击打开链接

stl+容斥

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <set>
using namespace std;
#define N 65540
#define ll __int64
ll n;
ll a[N][4], mul[4]={1,16,256,4096};
ll h[N];
vector<ll>G[N];
set<ll>myset;
set<ll>::iterator p;
ll find1(ll x){ //设除了x位,其他3位相同
	myset.clear();
	for(ll i = 0; i < n; i++){
		ll now = 0;
		for(ll j = 0; j < 4; j++)if(j!=x)
		now += a[i][j];
		G[now].push_back(i);
		myset.insert(now);
	}
	ll ans = 0;
	for(p = myset.begin(); p!=myset.end(); p++) {
		ll siz = G[*p].size();
		ans += (siz*(siz-1))>>1;
		G[*p].clear();
	}
	return ans;
}

ll find2(ll x,ll y){
	myset.clear();
	for(ll i = 0; i < n; i++) {
		ll now = 0;
		for(ll j = 0; j < 4; j++)if(j!=x&&j!=y)
			now += a[i][j];
		G[now].push_back(i);
		myset.insert(now);
	}
	ll ans = 0;
	for(p = myset.begin(); p!=myset.end(); p++) {
		ll siz = G[*p].size();
		ans += (siz*(siz-1))>>1;
		G[*p].clear();
	}
	return ans;
}
ll find3(ll x){
	myset.clear();
	for(ll i = 0; i < n; i++){
		ll now = 0;
		now += a[i][x];
		G[now].push_back(i);
		myset.insert(now);
	}
	ll ans = 0;
	for(p = myset.begin(); p!=myset.end(); p++) {
		ll siz = G[*p].size();
		ans += (siz*(siz-1))>>1;
		G[*p].clear();
	}
	return ans;
}
int main(){
	ll i,j;
	for(ll i = 0; i < N; i++)G[i].clear();
	while(cin>>n) {
		for(i=0;i<n;i++)
		{
			char s[10]; scanf("%s",s);
			for(j=0;j<4;j++) {
				a[i][j] = ('0'<=s[j]&&s[j]<='9')?s[j]-'0':s[j]-'a'+10;
				a[i][j] *= mul[j];
			}
		}
		ll ans[5] = {0};
		ans[1] = find1(0) + find1(1) + find1(2) + find1(3);
		ans[2] = find2(0,1) + find2(0,2) + find2(0,3) + find2(1,2) + find2(1,3) + find2(2,3) - ans[1]*3;
		ans[3] = find3(0) + find3(1) + find3(2) + find3(3) - ans[1]*3 - ans[2]*2;
		ll all = (ll) ((n*(n-1))/2);
		ans[4] = all - ans[1] - ans[2] - ans[3];
		cout<<ans[1]<<" "<<ans[2]<<" "<<ans[3]<<" "<<ans[4]<<endl;

	}
	return 0;
}
/*
4
abcd
abca
abce
abcf
10
0000
1111
2222
3333
4444
5555
6666
7777
8888
9999

*/

ural 1932 The Secret of Identifier 容斥,布布扣,bubuko.com

时间: 2024-11-17 12:47:40

ural 1932 The Secret of Identifier 容斥的相关文章

ural 1932 The Secret of Identifier (容斥原理)

题目大意: 求出给的n个串中. 精确到只有一个字符不同,两个字符不同,三个字符不同,四个字符不同的对数. 思路分析: 枚举状态. dp[i] [j] ...表示当前串取出 i 状态下的所有字符转化成十进制数为 j 的出现的次数. 这样的话,就记录了所有串的子串的状态. 然后计数就得到了所有的状态. 然后我们要得到精确不同的,可以用补集的思想,如果要精确到三个不相同,意味着要精确到1 个是相同的. 注意的问题是 在最后要运用容斥去重. #include <cstdio> #include <

URAL 1932 The Secret of Identifier 题解

http://acm.timus.ru/problem.aspx?space=1&num=1932 B - The Secret of Identifier Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1932 Description Davy Jones: You've been captain of the Black Pearl for

URAL 1091. Tmutarakan Exams 容斥

从1到s选出k个数 他们的最大公约数大于1 求方案数 容斥 S(1)-S(2)+S(3) S(x)为选出k个数的公因子个数为x的数量 #include <cstdio> #include <cmath> #include <cstring> using namespace std; typedef long long LL; const int maxn = 55; int prime[maxn], vis[maxn]; int n, m; int get_primes

hdu 5514 Frogs 容斥思想+gcd 银牌题

Frogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1509    Accepted Submission(s): 498 Problem Description There are m stones lying on a circle, and n frogs are jumping over them.The stones a

hdu 5514 Frogs(容斥)

Frogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1315    Accepted Submission(s): 443 Problem Description There are m stones lying on a circle, and n frogs are jumping over them.The stones a

Lucky HDU - 5213 (莫队,容斥)

WLD is always very lucky.His secret is a lucky number . is a fixed odd number. Now he meets a stranger with numbers:.The stranger asks him questions.Each question is like this:Given two ranges and ,you can choose two numbers and to make .The you can

POJ 2773 Happy 2006 二分+容斥(入门

题目链接:点击打开链接 题意: 输入n ,k 求与n互质的第k个数(这个数可能>n) 思路: solve(mid)表示[1,mid]中有多少个和n互质,然后二分一下最小的mid 使得互质个数==k solve(x) 实现: 与n互质的个数=所有数-与n不互质的数=所有数-(与n有一个因子-与n有2个因子的+与n有3个因子的) 状压n的因子个数,然后根据上面的公式容斥得到. #include <stdio.h> #include <iostream> #include <

hdu1695(莫比乌斯)或欧拉函数+容斥

题意:求1-b和1-d之内各选一个数组成数对,问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个可以简化成1-b/k 和1-d/k 的互质有序数对的个数.假设b=b/k,d=d/k,b<=d.欧拉函数可以算出1-b与1-b之内的互质对数,然后在b+1到d的数i,求每个i在1-b之间有多少互质的数.解法是容斥,getans函数参数的意义:1-tool中含有rem位置之后的i的质因子的数的个数. 在 for(int j=rem;j<=factor[i

HDU 4135 Co-prime(容斥+数论)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5526    Accepted Submission(s): 2209 Problem Description Given a number N, you are asked to count the number of integers between A and B