hdu 5734 Acperience 水题
Acperience
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5734
Description
Deep neural networks (DNN) have shown significant improvements in several application domains including computer vision and speech recognition. In computer vision, a particular type of DNN, known as Convolutional Neural Networks (CNN), have demonstrated state-of-the-art results in object recognition and detection.
Convolutional neural networks show reliable results on object recognition and detection that are useful in real world applications. Concurrent to the recent progress in recognition, interesting advancements have been happening in virtual reality (VR by Oculus), augmented reality (AR by HoloLens), and smart wearable devices. Putting these two pieces together, we argue that it is the right time to equip smart portable devices with the power of state-of-the-art recognition systems. However, CNN-based recognition systems need large amounts of memory and computational power. While they perform well on expensive, GPU-based machines, they are often unsuitable for smaller devices like cell phones and embedded electronics.
In order to simplify the networks, Professor Zhang tries to introduce simple, efficient, and accurate approximations to CNNs by binarizing the weights. Professor Zhang needs your help.
More specifically, you are given a weighted vector W=(w1,w2,...,wn). Professor Zhang would like to find a binary vector B=(b1,b2,...,bn) (bi∈{+1,−1}) and a scaling factor α≥0 in such a manner that ∥W−αB∥2 is minimum.
Note that ∥⋅∥ denotes the Euclidean norm (i.e. ∥X∥=x21+⋯+x2n−−−−−−−−−−−√, where X=(x1,x2,...,xn)).
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integers n (1≤n≤100000) -- the length of the vector. The next line contains n integers: w1,w2,...,wn (−10000≤wi≤10000).
Output
For each test case, output the minimum value of ∥W−αB∥2 as an irreducible fraction "p/q" where p, q are integers, q>0.
Sample Input
3
4
1 2 3 4
4
2 2 2 2
5
5 6 2 3 4
Sample Output
5/1
0/1
10/1
Hint
题意
让你构造一个β向量,里面的每一维都是1或者-1
然后使得W - αβ的方差最小。
题解:
首先,我们让W中所有为负的,都变成正数,这样我们就相当于把β都当成1了,接下来就开始考虑阿尔法的问题。
把方差的平方项打开,很清楚的看见当阿尔法取平均数的时候就是最小值,这个结论也很容易猜到,所以直接莽一波就好了。
但是答案会爆longlong,所以要么你化简那个式子,要么你就高精度。
我推荐把平方项打开,然后合并就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
long long a[maxn];
int n;
long long gcd(long long aa,long long bb){
if(bb==0)return aa;
return gcd(bb,aa%bb);
}
void solve(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%I64d",&a[i]);
if(a[i]<0)a[i]*=-1;
}
long long sum = 0,down = 0;
for(int i=1;i<=n;i++){
sum = sum + a[i];
}
long long ans1 = 0;
for(int i=1;i<=n;i++){
ans1 = ans1 + n * a[i] * a[i];
}
ans1 = ans1 - sum * sum;
long long ans2 = n;
long long g = gcd(ans1,ans2);
ans1/=g,ans2/=g;
printf("%I64d/%I64d\n",ans1,ans2);
/*
long long g = gcd(up,down);
up/=g,down/=g;
long long ans1 = a[1]*down - up;
long long ans2 = down;
g = gcd(ans1,ans2);
ans1/=g,ans2/=g;
ans1 = ans1 * ans1;
ans2 = ans2 * ans2;
for(int i=2;i<=n;i++){
long long tmp1 = a[i]*down - up;
long long tmp2 = down;
g = gcd(tmp1,tmp2);
tmp1/=g,tmp2/=g;
tmp1 = tmp1 * tmp1;
tmp2 = tmp2 * tmp2;
long long Tmp1 = ans1*tmp2+ans2*tmp1;
long long Tmp2 = tmp2*ans2;
g = gcd(Tmp1,Tmp2);
Tmp1/=g,Tmp2/=g;
ans1 = Tmp1;
ans2 = Tmp2;
}
printf("%I64d/%I64d\n",ans1,ans2);
*/
}
int main(){
int t;
scanf("%d",&t);
while(t--)solve();
return 0;
}
hdu 5734 Acperience 水题的更多相关文章
- HDU 5734 Acperience(返虚入浑)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- hdu 5210 delete 水题
Delete Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5210 D ...
- hdu 1251 (Trie水题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU 5734 Acperience (推导)
Acperience 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5734 Description Deep neural networks (DN ...
- HDU 5703 Desert 水题 找规律
已知有n个单位的水,问有几种方式把这些水喝完,每天至少喝1个单位的水,而且每天喝的水的单位为整数.看上去挺复杂要跑循环,但其实上,列举几种情况之后就会发现是找规律的题了= =都是2的n-1次方,而且这 ...
- HDU 5734 Acperience
Acperience Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 4493 Tutor 水题的收获。。
题目: http://acm.hdu.edu.cn/showproblem.php?pid=4493 题意我都不好意思说,就是求12个数的平均数... 但是之所以发博客,显然有值得发的... 这个题最 ...
- hdu 4802 GPA 水题
GPA Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4802 Des ...
- hdu 4493 Tutor 水题
Tutor Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4493 D ...
随机推荐
- 流媒体技术学习笔记之(七)进阶教程OBS参数与清晰度流畅度的关系
源码地址:https://github.com/Tinywan/PHP_Experience 很多主播问过OBS的参数到底什么影响画质,到底什么影响流畅度,那么本篇教程尽量用通俗的语言解释下一些重要参 ...
- Oozie 生成JMS消息并向 JMS Provider发送消息过程分析
一,涉及到的工程 从官网下载源码,mvn 编译成 Eclipse工程文件:
- 求二叉树中第K层结点的个数
一,问题描述 构建一棵二叉树(不一定是二叉查找树),求出该二叉树中第K层中的结点个数(根结点为第0层) 二,二叉树的构建 定义一个BinaryTree类来表示二叉树,二叉树BinaryTree 又是由 ...
- nginx php上传大文件的设置(php-fpm)
对于lnmp架构的大文件上传的问题,有几个地方需要修改,另外还有几个参数如果更改过需要注意,下面是详细的需要注意的地方: nginx的修改 send_timeout 6 ...
- jsp前端验证(非常好用)
1.在jsp页面中引入<script type="text/javascript" src="${ctxStatic}/js/valid.js">& ...
- 【Udacity并行计算课程笔记】- lesson 1 The GPU Programming Model
一.传统的提高计算速度的方法 faster clocks (设置更快的时钟) more work over per clock cycle(每个时钟周期做更多的工作) more processors( ...
- Hibernate的实体规则、主键生成策略、对象状态
一. hibernate的实体类有一定的规则,类似于mybatis的逆向工程导出的实体类.具体的规则以及原因如下: 1.持久化类需要提供无参的构造方法. 因为hibernate底层采用反射机制创建对象 ...
- 洛谷 P5206: bzoj 5475: LOJ 2983: [WC2019] 数树
一道技巧性非常强的计数题,历年WC出得最好(同时可能是比较简单)的题目之一. 题目传送门:洛谷P5206. 题意简述: 给定 \(n, y\). 一张图有 \(|V| = n\) 个点.对于两棵树 \ ...
- nginx_upstream_check_module监控后端服务器http
nginx_upstream_check_module 是专门提供负载均衡器内节点的健康检查的外部模块,由淘宝的姚伟斌大神开发,通过它可以用来检测后端 realserver 的健康状态.如果后端 re ...
- OracleDBConsoleorcl 启动不了 服务特定错误2【解决办法】
问题描述: 我的oracle不知道为什么OracleDBConsoleorcl 启动不了 出现“服务特定错误2”现在我也不知道原因 估计是因为我电脑的IP经常变动,有时在公司用,有时在家里用! 我的o ...