【HDU5734 2016 Multi-University Training Contest 2A】【公式代入推導】Acperience n維向量各有加減最小模長
Acperience
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 993 Accepted Submission(s): 532
Problem 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)).
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).
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
Author
zimpha
Source
#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[N];
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)
*/
相關文章
- 2018 Multi-University Training Contest 3 - HDU ContestAI
- HDU-2016 Multi-University Training Contest 3-Sqrt Bo-大數開方AI
- HDU 6039 Gear Up(2017 Multi-University Training Contest 1)AI
- HDU 5235 Friends (2015 Multi-University Training Contest 2 搜尋+剪枝)AI
- 2018 Multi-University Training Contest 9----hdu 6415 Rikka with Nash EquilibriumAIUI
- 核化線性降維中部分公式的推導公式
- 向量和矩陣求導公式總結矩陣求導公式
- 【HDU5735 2016 Multi-University Training Contest 2B】【暴力做法 + 折半法】Born Slippy 祖先鏈的最大運算權值AI
- Lucene打分公式的推導公式
- 二項式定理公式推導公式
- 【矩陣基礎與維度分析】【公式細節推導】矩陣非線性最小二乘法泰勒展開矩陣公式
- 挑選方案問題(牛客競賽 思維題+推導公式)公式
- 【HDU5738 2016 Multi-University Training Contest 2E】【平面點數計數 共線判定】Eureka 平面有多少個集合滿足貢獻AI
- 四元數旋轉公式推導公式
- LOAM原始碼分析附公式推導原始碼公式
- 三角函式公式推導函式公式
- FlashAttention逐代解析與公式推導公式
- 四元數的旋轉公式推導公式
- 高斯公式對高斯定理的推導公式
- Diffusion系列 - DDIM 公式推導 + 程式碼 -(三)公式
- Python運算子可不只有加減乘除Python
- 在遊戲製作中渲染公式推導(轉)遊戲公式
- python--各種推導式Python
- 推薦系統中嵌入向量維數選擇
- 擴充套件歐幾里得演算法公式快速推導套件演算法公式
- Linux運維人員成長之路學習書籍推薦(未刪減版)Linux運維
- 線性迴歸模型公式推導完整簡潔版模型公式
- 三維旋轉矩陣推導矩陣
- 二維旋轉矩陣推導矩陣
- c++ 一維向量,和二維向量的基本使用C++
- 【Codeforces Round 362 (Div 2)E】【公式推導+快速冪+費馬小定理】PLEASE a[i]=(1-a[i-1])除2下n次項 n為連乘數公式
- 利用MATLAB逆推英雄聯盟護甲值—傷害減少率公式Matlab公式
- python生成器和各種推導式Python
- 戰鬥公式的演化與策略--屬性與公式的關係、減法公式與乘法公式公式
- 總結:生成函式(斐波那契通項公式推導)函式公式
- 統一場理論公式推導和筆記——part5公式筆記
- 統一場理論公式推導和筆記——part6公式筆記
- 一維到N維智慧