HDU5734 Acperience(数学推导)
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∥=sqrt(x12+⋯+xn2) where X=(x1,x2,...,xn)).
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).
Sample Input Sample Output
/
/
/
题意:
已知一种计算方式||X||=sqrt(x12+x22+...+xn2),现给出W的值,求||W-aB||的最小值,其中B只能取-1和1,a为缩放因子,其中a>=0。
思路:
实际上求(W1-ab1)2+(W2-ab2)2+...+(Wn-abn)2的最小值,将这个公式展开,经过化简可以得到(Wi的平方和)+na2-2a(Wi绝对值的和)
求出a的值,然后将其带入即可,相当于求一元二次方程的最小值。即a=b/2a。
最后注意分子、分母分开计算,最后GCD求最大公因数。GCD用long long。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
using namespace std;
long long gcd(long long a,long long b)//一定记得用longlong
{
if(b==)
return a;
return gcd(b,a%b);
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[];
long long sum1,sum;
sum1=sum=;//sum1为平方和,sum为和
for(int i=; i<n; i++)
{
cin>>a[i];
if(a[i]<)//一定取正值,因为b为-1或者+1,为了使最后结果最小,sum1应该最大
a[i]=-a[i];
sum1+=a[i];
sum+=(a[i]*a[i]);
}
/*接下来注意不能用b/2a 因为他不一定为整数,最后求得是分数,所以要将最后的公式化作分子分母形式*/
long long nb=n*sum-sum1*sum1 ;
long long x;
x=gcd(nb,n);
cout<<nb/x<<"/"<<n/x<<endl;
}
return ;
}
HDU5734 Acperience(数学推导)的更多相关文章
- hdu-5734 Acperience(数学)
题目链接: Acperience Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5734 Acperience(数学推导)
Problem Description Deep neural networks (DNN) have shown significant improvements in several applic ...
- 借One-Class-SVM回顾SMO在SVM中的数学推导--记录毕业论文5
上篇记录了一些决策树算法,这篇是借OC-SVM填回SMO在SVM中的数学推导这个坑. 参考文献: http://research.microsoft.com/pubs/69644/tr-98-14.p ...
- 关于不同进制数之间转换的数学推导【Written By KillerLegend】
关于不同进制数之间转换的数学推导 涉及范围:正整数范围内二进制(Binary),八进制(Octonary),十进制(Decimal),十六进制(hexadecimal)之间的转换 数的进制有多种,比如 ...
- UVA - 10014 - Simple calculations (经典的数学推导题!!)
UVA - 10014 Simple calculations Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- 『sumdiv 数学推导 分治』
sumdiv(POJ 1845) Description 给定两个自然数A和B,S为A^B的所有正整数约数和,编程输出S mod 9901的结果. Input Format 只有一行,两个用空格隔开的 ...
- LDA-线性判别分析(二)Two-classes 情形的数学推导
本来是要调研 Latent Dirichlet Allocation 的那个 LDA 的, 没想到查到很多关于 Linear Discriminant Analysis 这个 LDA 的资料.初步看了 ...
- leetcode 343. Integer Break(dp或数学推导)
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [hdu5307] He is Flying [FFT+数学推导]
题面 传送门 思路 看到这道题,我的第一想法是前缀和瞎搞,说不定能$O\left(n\right)$? 事实证明我的确是瞎扯...... 题目中的提示 这道题的数据中告诉了我们: $sum\left( ...
- ZOJ3329(数学推导+期望递推)
要点: 1.期望的套路,要求n以上的期望,则设dp[i]为i分距离终点的期望步数,则终点dp值为0,答案是dp[0]. 2.此题主要在于数学推导,一方面是要写出dp[i] = 什么,虽然一大串但是思维 ...
随机推荐
- SpringMVC中使用Swagger2整合
Swagger2是什么 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 W ...
- Normalize.css源码注释翻译&浏览器css兼容问题的理解
版本v5.0.0源码地址: https://necolas.github.io/normalize.css/5.0.0/normalize.css 翻译版: /*! normalize.css v5. ...
- 用JS来实现于截取中英文混合字符串方法(转载)
网站制作过程中,提示层文字超出,需要JS做字符串截取,但是呢,我们常常会烦恼文字中英文混合如何判断,因为我们知道在JS中 string.length这个值是不考虑中英文的,但是计算机对中英文的识别是 ...
- Linux下php安装memcache
Linux下php安装memcache说明:php安装目录:/phpstudy/server/php/bin/phpphp.ini配置文件路径:/phpstudy/server/php/etc/php ...
- USACO The Castle
首先看一下题目. The CastleIOI'94 - Day 1 In a stroke of luck almost beyond imagination, Farmer John was sen ...
- 从Java熟练到Android入门
刚刚从学校出来,唉,从Java转入Android. 当初老师告诉我们Android不重要,结果,Android的所有课不是在玩手机就是在说话,没认真听也没认真看,作业也没认真做,现在想想好后悔啊,以至 ...
- script标签跨域的缺点
1,只支持GET,不支持其他例如:put,delete,post等 2,想拿到数据需要服务器端做出相应处理,必须在window域下面有对应的执行函数.例如:window.callbackHandler ...
- 计算机浏览器存储技术cookie、sessionStorage、localStorage
HTTP无状态协议是指协议对于事务处理没有记忆能力.会话跟踪协议的状态是指下一次传输可以"记住"这次传输信息的能力,无状态是指同一个会话(注意什么叫同一个会话)的连续两个请求互相不 ...
- [USACO09OCT]热浪Heat Wave
未经同意,不得转载. The good folks in Texas are having a heatwave this summer. Their Texas Longhorn cows make ...
- 英文版windows7中文软件显示乱码的解决办法
一.打开控制面板,修改语言的归属地为China 修改完成之后重启,一般能解决大部分问题,如果依然有部分显示乱码,就需要去修改注册表