您的位置:首页 > 大数据 > 人工智能

2016 Multi-University Training Contest 2 1001 Acperience

2016-07-22 09:24 483 查看
题目连接:点击打开链接

题目大意:给定一个n维点w,和一个只含有1和-1的n维点b,求
∥W−αB∥2
其中,∥X∥=x21+⋯+x2n−−−−−−−−−−√

解题思路:其实对于αB本质上是一条射线,要求的答案其实也就是n维平面内点到直线的距离。这题如果不求分数答案其使用二分做也可以(以二维平面距离其实是一个一元二次方程,求顶点就好),由于答案的格式我们倾向于用分式直接表示答案,任何平面下点到直线的距离都是,

上面为叉乘,但是我们又不希望有三角函数的存在,毕竟三角函数用分数表示还是笨麻烦的,于是这样表示

,由于最后算的是平方所以答案是对于ob的值的确定方法

,参照二维平面,b.x=(ow.x>=0?1:-1),b.y=(ow.y>=0?1:-1),所以只要这位是非负数b对应位置就是1,否则就是-1

代码:

#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<ctime>
#include "cstdio"
#include "string"
#include "string.h"
#include "map"
using namespace std;
const int maxn = 100001;
long long w[maxn], b[maxn];
long long gcd(long long a, long long b)
{
return (b>0) ? gcd(b, a%b) : a;
}
int main()
{
int T,n;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = 0;i < n;i++)
{
scanf("%lld", &w[i]);
if (w[i] >= 0)
b[i] = 1;
else
b[i] = -1;
}
long long fenmu = n,fenzi=0;
long long temp1 = 0, temp2 = 0, temp3 = 0;
for (int i = 0;i < n;i++)
{
temp1 += w[i] * w[i];
temp2 += b[i] * b[i];
temp3 += b[i] * w[i];
}
fenzi = temp1 * temp2 - temp3*temp3;
if (fenzi == 0)
{
printf("0/1\n");
continue;
}
long long temp = gcd(fenzi, fenmu);
printf("%lld/%lld\n", fenzi / temp, fenmu / temp);

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息