Codeforces 769D k-Интересные пары чисел

题目链接:http://codeforces.com/contest/769/problem/D


搜索题

考虑这些数的值域较小,直接${O(2^{k})}$次方枚举每个数字二进制位上是否改变,剪枝一下最不利复杂度为${O(C_{14}^{7})}$,有${10^{4}}$中取值。

最终最不利复杂度为${O(C_{14}^{7}*(10^{4}))}$


 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<vector>
 5 #include<cstdlib>
 6 #include<cmath>
 7 #include<cstring>
 8 using namespace std;
 9 #define maxn 100010
10 #define llg long long 
11 #define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
12 llg n,m,str[maxn],c[maxn],k,v[maxn];
13 vector<llg>a[maxn];
14 llg d[maxn];
15 void ss1(llg wz,llg res,llg x)
16 {
17     if (wz==14)
18     {
19         if (res==0)
20         {
21             llg val=0;
22             for (llg i=0;i<14;i++) val+=(1<<i)*((str[i]+c[i])%2);
23             a[x].push_back(val);
24         }
25         return ;
26     }
27     if (res)
28     {
29     c[wz]=1;
30     ss1(wz+1,res-1,x);
31     }
32     c[wz]=0;
33     ss1(wz+1,res,x);
34 }
35 
36 void ss2(llg wz,llg res,llg x)
37 {
38     if (wz==14)
39     {
40         if (res==0)
41         {
42             llg val=0;
43             for (llg i=0;i<14;i++) val+=(1<<i)*((str[i]+c[i])%2);
44             a[x].push_back(val);
45         }
46         return ;
47     }
48     if (res)
49     {
50         c[wz]=0;
51         ss2(wz+1,res-1,x);
52     }
53     c[wz]=1;
54     ss2(wz+1,res,x);
55 }
56 
57 void change_(llg x)
58 {
59     llg cs=0;
60     while (cs<14)
61     {
62         str[cs]=x%2;
63         x/=2;
64         cs++;
65     }
66 }
67 
68 int main()
69 {
70     yyj("D");
71     cin>>n>>k;
72     for (llg i=0;i<=10000;i++)
73     {
74         change_(i);
75         if (k<=7) ss1(0,k,i);else ss2(0,14-k,i);
76     }
77     for (llg i=1;i<=n;i++) scanf("%lld",&v[i]),d[v[i]]++;
78     llg ans=0;
79     for (llg i=1;i<=n;i++)
80     {
81         llg w=a[v[i]].size(),x=v[i];
82         for (llg j=0;j<w;j++) 
83             if (k==0) ans+=d[a[x][j]]-1; else ans+=d[a[x][j]];
84     }
85     cout<<ans/2;
86     return 0;
87 }
本文作者:xrdog 作者博客:http://www.cnblogs.com/Dragon-Light/ 转载请注明出处,侵权必究,保留最终解释权!
原文地址:https://www.cnblogs.com/Dragon-Light/p/6506507.html