首页 > 代码库 > [HDOJ5738]Eureka(组合数学)

[HDOJ5738]Eureka(组合数学)

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5738

题意:给出n个点,问满足两个点形成一条直线后,其他点在这条直线上形成的集合的个数。

先排序后枚举,由于可能有重点,所以把重点先解决掉,枚举点,假如有一共k个重点,则重点可作为结果的一部分贡献,即2^k-1个子集(删掉空集)。之后枚举当前点与其他非重点的线,一开始我想的是找他们的斜率,但是精度有问题,所以我直接保存的一个元组,用这个元组来表示斜率的分母形式,当然要化简到最简,所以要除以gcd。随后枚举,看看有几个点的斜率相同,相同的时候子集数量是2^k * (2^g-1),k为之前重点子集数,再加上一个在直线上的子集(除去空集)就是这次的贡献。

  1 /*  2 ━━━━━┒ギリギリ♂ eye!  3 ┓┏┓┏┓┃キリキリ♂ mind!  4 ┛┗┛┗┛┃\○/  5 ┓┏┓┏┓┃ /  6 ┛┗┛┗┛┃ノ)  7 ┓┏┓┏┓┃  8 ┛┗┛┗┛┃  9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorithm> 18 #include <iostream> 19 #include <iomanip> 20 #include <cstring> 21 #include <climits> 22 #include <complex> 23 #include <fstream> 24 #include <cassert> 25 #include <cstdio> 26 #include <bitset> 27 #include <vector> 28 #include <deque> 29 #include <queue> 30 #include <stack> 31 #include <ctime> 32 #include <set> 33 #include <map> 34 #include <cmath> 35 using namespace std; 36 #define fr first 37 #define sc second 38 #define cl clear 39 #define BUG puts("here!!!") 40 #define W(a) while(a--) 41 #define pb(a) push_back(a) 42 #define Rint(a) scanf("%d", &a) 43 #define Rs(a) scanf("%s", a) 44 #define Cin(a) cin >> a 45 #define FRead() freopen("in", "r", stdin) 46 #define FWrite() freopen("out", "w", stdout) 47 #define Rep(i, len) for(int i = 0; i < (len); i++) 48 #define For(i, a, len) for(int i = (a); i < (len); i++) 49 #define Cls(a) memset((a), 0, sizeof(a)) 50 #define Clr(a, x) memset((a), (x), sizeof(a)) 51 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 52 #define lrt rt << 1 53 #define rrt rt << 1 | 1 54 #define pi 3.14159265359 55 #define RT return 56 #define lowbit(x) x & (-x) 57 #define onecnt(x) __builtin_popcount(x) 58 typedef long long LL; 59 typedef long double LD; 60 typedef unsigned long long ULL; 61 typedef pair<int, int> pii; 62 typedef pair<string, int> psi; 63 typedef pair<LL, LL> pll; 64 typedef map<string, int> msi; 65 typedef vector<int> vi; 66 typedef vector<LL> vl; 67 typedef vector<vl> vvl; 68 typedef vector<bool> vb; 69  70 typedef struct P { 71     LL x, y; 72     P() {} 73     P(LL xx, LL yy) : x(xx), y(yy) {} 74 }P; 75  76 const LL mod = 1000000007; 77 const LL maxn = 1010; 78 P p[maxn]; 79 LL n; 80 vector<P> tmp; 81  82 LL gcd(LL x, LL y) { 83     return y == 0 ? x : gcd(y, x % y); 84 } 85  86 bool cmp(P a, P b) { 87     if(a.x == b.x) return a.y < b.y; 88     return a.x < b.x; 89 } 90 LL mul[maxn]; 91  92 int main() { 93     // FRead(); 94     mul[0] = 1; 95     For(i, 1, maxn) mul[i] = (2 * mul[i-1]) % mod; 96     int T; 97     Rint(T); 98     W(T) { 99         LL ret = 0;100         cin >> n;101         For(i, 1, n+1) {102             cin >> p[i].x >> p[i].y;103         }104         sort(p+1, p+n+1, cmp);105         For(i, 1, n+1) {106             LL cnt = 0; tmp.clear();107             For(j, i+1, n+1) {108                 if(p[i].x == p[j].x && p[i].y == p[j].y) cnt++;109                 else {110                     LL x = p[j].x - p[i].x;111                     LL y = p[j].y - p[i].y;112                     LL ex = gcd(x, y);113                     tmp.push_back(P(x/ex, y/ex));114                 }115             }116             ret = (ret+(mul[cnt])-1)%mod;117             sort(tmp.begin(), tmp.end(), cmp);118             int k;119             for(LL j = 0; j < tmp.size(); j=k) {120                 for(k = j; k < tmp.size(); k++) {121                     if(!(tmp[k].x == tmp[j].x && tmp[k].y == tmp[j].y))122                         break;123                 }124                 ret = (ret+(mul[cnt]*(mul[k-j]-1)%mod)%mod)%mod;125             }126         }127         cout << ret << endl;128     }129     RT 0;130 }

 

[HDOJ5738]Eureka(组合数学)