0
点赞
收藏
分享

微信扫一扫

【Codeforces - 769D】k-Interesting Pairs Of Integers(暴力,统计,思维,数学,异或)

A邱凌 2022-06-15 阅读 65

题干:

Vasya has the sequence consisting of n integers. Vasya consider the pair of integers x and y k-interesting, if their binary representation differs from each other exactly in k bits. For example, if k = 2, the pair of integers x = 5 and y = 3 is k-interesting, because their binary representation x=101 and y=011 differs exactly in two bits.

Vasya wants to know how many pairs of indexes (ij) are in his sequence so that i < j and the pair of integers ai and aj is k-interesting. Your task is to help Vasya and determine this number.

Input

The first line contains two integers n and k (2 ≤ n ≤ 105, 0 ≤ k ≤ 14) — the number of integers in Vasya's sequence and the number of bits in which integers in k-interesting pair should differ.

The second line contains the sequence a1, a2, ..., an (0 ≤ ai ≤ 104), which Vasya has.

Output

Print the number of pairs (ij) so that i < j and the pair of integers ai and aj is k-interesting.

Examples

Input

4 1
0 3 2 1

Output

4

Input

6 0
200 100 100 100 200 200

Output

6

Note

In the first test there are 4 k-interesting pairs:

  • (1, 3),
  • (1, 4),
  • (2, 3),
  • (2, 4).

In the second test k = 0. Consequently, integers in any k-interesting pair should be equal to themselves. Thus, for the second test there are 6 k-interesting pairs:

  • (1, 5),
  • (1, 6),
  • (2, 3),
  • (2, 4),
  • (3, 4),
  • (5, 6).

题目大意:

给你N个数的一个序列a,如果有两个数二进制中有K个位置不同,那么对应就算作一对可行解,问一共有多少对可行解。

(2 ≤ n ≤ 1e5, 0 ≤ k ≤ 14, 0 ≤ ai ≤ 1e4)

解题报告:

  介绍这个题的两种写法。首先很显然看x和y有多少个位子不同,直接看x^y对应的二进制中1的个数即可。

所以第一种想法啊十分好想,预处理1~20000所有数字的1的个数,然后暴力枚举1e4以内的每个数字i和j,如果i^1的1的个数等于k,则直接统计i,j两个数的出现次数。我这里偷懒了没预处理,直接用内置函数了。这种方法复杂度O(1e8)

第二种方法就是读入完k之后,先暴力枚举所有1的个数等于k的数,打表可以发现对任意一个k,1e4以内不会超过3000个数。问题等价于求x^y=z,x和y符合的二元组个数,已知x<=1e4,z最多3000个,所以直接枚举x和z来反求y。复杂度O(3e8)左右。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 1<<15;
int n,k;
int dp[MAX];
vector<int> vv;
int cnt[MAX],mx;
ll ans;
int main()
{
// cout << (1<<14) << endl;
cin >> n >> k;
for(int x,i = 1; i<=n; i++) {
scanf("%d",&x); dp[x]++; mx = max(mx,x);
}
if(k == 0) {
for(int i = 0; i<=mx; i++) ans += 1LL*dp[i]*(dp[i]-1)/2;
cout << ans << endl; return 0;
}
for(int i = 0; i<=mx; i++) {
for(int j = i+1; j<=mx; j++) {
if(__builtin_popcount(i^j) == k) {
ans += 1LL*dp[i] * dp[j];
}
}
} cout << ans << endl;
return 0 ;
}

 


举报

相关推荐

0 条评论