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)W=(w1,w2,…,wn). Professor Zhang would like to find a binary vector B=(b1,b2,…,bn)B=(b1,b2,…,bn) (bi∈{+1,−1})(bi∈{+1,−1}) and a scaling factor α≥0α≥0 in such a manner that ∥W−αB∥2‖W−αB‖2 is minimum.
Note that ∥⋅∥‖⋅‖ denotes the Euclidean norm (i.e. ∥X∥=x21+⋯+x2n−−−−−−−−−−−√‖X‖=x12+⋯+xn2, where X=(x1,x2,…,xn)X=(x1,x2,…,xn)).
Input
There are multiple test cases. The first line of input contains an integer TT, indicating the number of test cases. For each test case:
The first line contains an integers nn (1≤n≤100000)(1≤n≤100000) – the length of the vector. The next line contains nn integers: w1,w2,…,wnw1,w2,…,wn (−10000≤wi≤10000)(−10000≤wi≤10000).
Output
For each test case, output the minimum value of ∥W−αB∥2‖W−αB‖2 as an irreducible fraction “pp/qq” where pp, qq are integers, q>0q>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
看懂题目依然是难关。
是要慢慢培养自己琢磨题目的耐心了。
不能老啃别人干粮啊。
因为看懂题目才能引发思考哎。
但是真心觉得,思维题蛮有意思。
人家的求最大公约数的函数:
ll gcd(ll a, ll b) {return b==0? a: gcd(b, a%b);}
我化简步骤:
if(fenzi%fenmu==0)
cout<<fenzi/fenmu<<"/"<<1<<endl;
else{
for(int m=fenmu-1;m>=1;m--){
if(m==1){
cout<<fenzi<<"/"<<fenmu<<endl;
break;
}
else{
if(fenzi%m==0&&fenmu%m==0){
cout<<fenzi/m<<"/"<<fenmu/m<<endl;
break;
}
}
}
}
思路不是自己的。
代码错在求最大公约数也是没谁了。
太菜了太菜了,不甘心不甘心。
#include<iostream>
#include<math.h>
using namespace std;
typedef long long ll;
ll w[100005];
ll gcd(ll a, ll b) {return b==0? a: gcd(b, a%b);}
int main(){
ll k;
cin>>k;
while(k--){
ll n;
cin>>n;
memset(w,0,n);
for(ll i=0;i<n;i++){cin>>w[i];}
ll fenzi=0,fenmu=n;
ll sum1=0,sum2=0;
for(ll i=0;i<n;i++){
sum1+=w[i]*w[i];
if(w[i]>0)
sum2+=w[i];
else
sum2+=-w[i];
}
fenzi=sum1*n-sum2*sum2;
if(fenzi%fenmu==0)
cout<<fenzi/fenmu<<"/"<<1<<endl;
else{
for(int m=fenmu-1;m>=1;m--){
if(m==1){
cout<<fenzi<<"/"<<fenmu<<endl;
break;
}
else{
if(fenzi%m==0&&fenmu%m==0){
cout<<fenzi/m<<"/"<<fenmu/m<<endl;
break;
}
}
}
}
}
return 0;
}
附上大神代码。思路还没有琢磨透
#include <iostream>
#define LL long long
using namespace std;
LL a, b, c, _gcd;
LL gcd(LL a, LL b) {return b==0? a: gcd(b, a%b);}
int main()
{
int t, n, x;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
a = n, b = 0, c = 0;
for(int i = 1; i <= n; ++i)
{
scanf("%d", &x);
b += 1ll*abs(x);
c += 1ll*abs(x)*abs(x);
}
b *= 2;
_gcd = gcd(4*a*c-b*b, 4*a);
printf("%lld/%lld\n", (4*a*c-b*b)/_gcd, 4*a/_gcd);
}
return 0;
}
但是每一道题目都尽量思考。
直到问不出来为什么才算了结。