题目 :点击打开链接
1001 ACperience
tag:数学推导
题意:给你一堆数w, 让你确定一个参数a以一个{1, -1}向量b使得|w-ab|^2最小。 我们直接展开这个公式即可发现其中的奥秘。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
typedef unsigned long long LL;
const int maxn = 100000 + 100;
int n;
LL w[maxn];
LL gcd(LL m, LL n) {
if(n == 0) return m;
else return gcd(n, m%n);
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
LL wi2 = 0, sumw = 0;
for(int i=0; i<n; i++) {
int w; scanf("%d", &w);
wi2 += w*w;
sumw += abs(w);
}
sumw *= sumw;
//cout<<"wi2 = " << wi2 << "sumw = "<<sumw<<endl;
wi2 *= (LL)n;
LL b = wi2 - sumw, a = n;
LL g = gcd(b, a);
cout<<b/g<<'/'<<a/g<<endl;
}
return 0;
}
1005 Eureka
tag: 分类计数
这个题目的意思是给你平面上的一堆点,定义集合p为完美集合当且仅当这个集合中含有一个完美对, 完美队的定义是u v是一个完美对当且 w∈P , f(u,v)≥g(u,v,w) , where f(u,v)=(xu−xv)2+(yu−yv)2−−−−−−−−−−−−−−−−−−−√ and g(u,v,w)=f(u,v)+f(v,w)+f(w,u)2 .。现在求这些点集中完美集合的个数。
分析:将上面的公式化简一下即可得到f(u,v) = f(u, w) + f(w, v),由此可得到完美集合中的点共线。因此问题就转化为了给你一些点集, 问你共线的点集的个数。 我们考虑所有合法的点集,每一个点集中肯定有一个点位于坐下方, 因此我们可以枚举最左下方的点开始计数, 这样就可以保证不会重复计数, 对于有多个点位于同一位置的情况我们还需要统计他自身构成的点集对答案的贡献。。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> Pt;
const int maxn = 1000 + 100;
const LL M = 1000000000 + 7;
int n;
Pt pt[maxn];
LL poww[maxn];
map<pair<LL, LL>, int> mp;
LL gcd(LL m, LL n) {
if(n == 0) return m;
else return gcd(n, m%n);
}
int main() {
int T;
scanf("%d", &T);
poww[0] = 1;
for(int i=1; i<=1000; i++) poww[i] = poww[i-1]*2%M;
while(T--) {
scanf("%d", &n);
for(int i=0; i<n; i++){
int x, y; scanf("%d%d", &x, &y);
pt[i].first = x; pt[i].second = y;
}
sort(pt, pt+n);
LL res = 0;
for(int i=0; i<n; i++) {
int j;
for(j=i+1; j<n&&pt[j]==pt[j-1]; j++);
int selfnum = j - i;
//printf("i = %d, j = %d selfnum = %d\n", i, j, selfnum);
mp.clear();
for(j=i+selfnum; j<n; j++) {
LL dx = pt[j].first - pt[i].first;
LL dy = pt[j].second - pt[i].second;
LL g = gcd(dx, dy);
if(g) dx /= g, dy /= g;
mp[make_pair(dx, dy)]++;
}
map<pair<LL, LL>, int>::iterator it;
for(it=mp.begin(); it!=mp.end(); ++it) {
int num = it->second;
res += (poww[selfnum]-1) * (poww[num]-1);
res %= M;
}
res += poww[selfnum]-1-selfnum;
res %= M;
i = i + selfnum - 1;
}
printf("%lld\n", res);
}
return 0;
}