【HDU5734 2016 Multi-University Training Contest 2A】【公式代入推导】Acperience n维向量各有加减最小模长
2016-07-28 09:50
531 查看
Acperience
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 993 Accepted Submission(s): 532
[align=left]Problem Description[/align]
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)).
[align=left]Input[/align]
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).
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
3
4
1 2 3 4
4
2 2 2 2
5
5 6 2 3 4
[align=left]Sample Output[/align]
5/1
0/1
10/1
[align=left]Author[/align]
zimpha
[align=left]Source[/align]
2016 Multi-University Training
Contest 2
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 100010, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int casenum, casei;
LL n;
LL a
;
LL gcd(LL x, LL y)
{
return y == 0 ? x : gcd(y, x%y);
}
/*
n * ∑(wi^2) - (∑|wi|)^2 (1e18-1e16)
-----------------------------------
n
*/
int main()
{
scanf("%d", &casenum);
for (casei = 1; casei <= casenum; ++casei)
{
LL a = 0;
LL b = 0;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
LL x; scanf("%lld", &x);
a += x*x;
b += abs(x);
}
LL top = n*a - b*b;
LL bot = n;
LL g = gcd(top, bot);
printf("%lld/%lld\n", top/g, bot/g);
}
return 0;
}
/*
【trick&&吐槽】
1,很多题目看起来很冗杂,
其实我们读重点的话,就会发现题目是可以入手的。
比赛的时候也不能放弃题目阅读,往往藏了很多简单题。
2,一定要注意数据上限,队友推出了一个爆LL的错误公式
3,java做大数运算是很耗时间的,尽量规避。
【题意】
给你一个n维向量a,
然后我们希望对a向量的每个维度都加减一个相同的值,
使得得到的目标向量b的模尽可能小。
【类型】
公式化简
【分析】
如何做公式化简?
先一股脑地做代入!
(w1-ab1, w2-ab2, ... , wn-ab3),然后得到——
原式=
(w1-ab1)^2
+(w2-ab2)^2
+(w3-ab3)^2
...
+(wn-abn)^2
=(w1^2+...+wn^2)[定值] + (a^2)*(b1^2+b2^2+...+bn^2) - 2a(w1b1+w2b2+...+wnbn)
=[定值]+(a^2)*n-(2a)*(w1b1+w2b2+...+wnbn)
我们希望使其尽可能小,
显然 要使得(w1b1+w2b2+...+wnbn)尽可能大,这个很容易做的。使其变为了给定的定值。
然后,我们最后需要考虑的是一个
(x^2)*n-2x*[定值]+[定值]的一元二次方程。
显然,其最小值为
b^2 (∑|wi|)^2 n * ∑(wi^2) - (∑|wi|)^2 (1e18-1e16)
c- ---- = ∑(wi^2) - ---------- = ----------------------------
4a n n
【时间复杂度&&优化】
O(n)
*/
相关文章推荐
- 【HDU5565 BestCoder Round 62 (div1)C】【STL or 二分答案 or 计数排序】Clarke and baton n个人减肥m次求最后异或值
- 【HDU5564 BestCoder Round 62 (div1)B】【DP转矩阵快速幂】Clarke and digits 长度在[l,r]范围内7倍数数个数要求相邻两位不为K
- 【HDU5544 2015CCPC 南阳国赛E】【树上dfs找本质不同环 高斯消元 时间戳优化】Ba Gua Zhen 连通图上最大异或环
- 【HDU5222 2015赛码冠军杯I】【并查集找双连通 + tarjan求强连通】Exploration 双向边只能走一边是否图上存在环
- 【HDU5188 BestCoder Round 33C】【贪心排序+DP】zhx and contest 考试不被怀疑作弊条件下达到至少m分的最少时间
- 【HDU4560 2013西山居复赛D】【二分答案+网络流拆点】我是歌手 安排演唱会_每人歌不同_每场歌不同_人歌匹配一次
- 【HDU5411 2015 Multi-University Training Contest 10F】【矩阵快速幂 加一行构造法】CRB and Puzzle 矩阵的1次方到n次方的数值和
- 【HDU5570 BestCoder Round 63 (div1)C】【期望DP 公式化简】balls n种求m种颜色,同颜色球数为x贡献为x方 求期望
- 【HDU5569 BestCoder Round 63 (div1)B】【DP】matrix 向右走向下走最大乘积和
- 【HDU5568 BestCoder Round 63 (div1)A】【DP java高精度】sequence2 长度恰好为m的LIS数
- 【HDU5579 2015上海赛区G】【超级大讨论】Game of Arrays a[]+b[]+c[]有些位置可以减一,状态是否可能达成
- 【HDU5583 2015上海赛区L】【找规律 正难则反】LCM Walk 目标状态(x,y)哪些点走公倍数能走到它
- 【HDU5573 2015上海赛区B】【构造 二进制思想】Binary Tree 二叉树上走m层加减数使得最后权值恰为n
- 【HDU5583 2015上海赛区K】【暴力合并】Kingdom of Black and White 连续01串权值贡献为len^2最多改变一次最大权值
- 【HDU5578 2015上海赛区F】【水题】Friendship of Frog 最近的两个相同字符的距离是多少
- 【HDU5586 BestCoder Round 64 (div1)A】【贪心 最大连续子串】Sum 区间函数值变换使得数列权值和最大
- 【HDU5587 BestCoder Round 64 (div1)B】【迭代 前缀和思想】Array 前m个数的二进制中共有多少个1
- 【Codeforces Round 332 (Div 2)D】【数学 公式推导】Spongebob and Squares 正方形数量恰好为x个的所有大矩形
- 【BestCoder Round 65A】【水题】ZYB's Biology DNA与RNA是否匹配
- 【BestCoder Round 65B】【博弈 对称思想】ZYB's Game 范围取数都知道x谁取到x谁必败