题目链接

题意 : 给出 n 维向量 W、要你构造一个 n 维向量 B = ( b1、b2、b3 ..... ) ( bi ∈ { +1, -1 } ) 、然后求出对于一个常数 α > 0 使得 || W - αB ||^2 尽量小

分析 :

将 || W - αB || ^ 2 进行化简、如下

未知数是 α

不难看出这是一个一元二次方程 Ax^2 + Bx + C

而根据实际的贪心选择

当 wi > 0 时、有 bi < 0

当 wi < 0 时、有 bi > 0

那么上述方程的 A、B、C 都可以确定并求出且 A > 0

那么根据公式法、此方程有最小值 (4AC - B^2) / (4A)

直接求就行了

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define ULL unsigned long long
  4.  
  5. #define scl(i) scanf("%lld", &i)
  6. #define scll(i, j) scanf("%lld %lld", &i, &j)
  7. #define sclll(i, j, k) scanf("%lld %lld %lld", &i, &j, &k)
  8. #define scllll(i, j, k, l) scanf("%lld %lld %lld %lld", &i, &j, &k, &l)
  9.  
  10. #define scs(i) scanf("%s", i)
  11. #define sci(i) scanf("%d", &i)
  12. #define scd(i) scanf("%lf", &i)
  13. #define scIl(i) scanf("%I64d", &i)
  14. #define scii(i, j) scanf("%d %d", &i, &j)
  15. #define scdd(i, j) scanf("%lf %lf", &i, &j)
  16. #define scIll(i, j) scanf("%I64d %I64d", &i, &j)
  17. #define sciii(i, j, k) scanf("%d %d %d", &i, &j, &k)
  18. #define scddd(i, j, k) scanf("%lf %lf %lf", &i, &j, &k)
  19. #define scIlll(i, j, k) scanf("%I64d %I64d %I64d", &i, &j, &k)
  20. #define sciiii(i, j, k, l) scanf("%d %d %d %d", &i, &j, &k, &l)
  21. #define scdddd(i, j, k, l) scanf("%lf %lf %lf %lf", &i, &j, &k, &l)
  22. #define scIllll(i, j, k, l) scanf("%I64d %I64d %I64d %I64d", &i, &j, &k, &l)
  23.  
  24. #define lson l, m, rt<<1
  25. #define rson m+1, r, rt<<1|1
  26. #define lowbit(i) (i & (-i))
  27. #define mem(i, j) memset(i, j, sizeof(i))
  28.  
  29. #define fir first
  30. #define sec second
  31. #define VI vector<int>
  32. #define ins(i) insert(i)
  33. #define pb(i) push_back(i)
  34. #define pii pair<int, int>
  35. #define VL vector<long long>
  36. #define mk(i, j) make_pair(i, j)
  37. #define all(i) i.begin(), i.end()
  38. #define pll pair<long long, long long>
  39.  
  40. #define _TIME 0
  41. #define _INPUT 0
  42. #define _OUTPUT 0
  43. clock_t START, END;
  44. void __stTIME();
  45. void __enTIME();
  46. void __IOPUT();
  47. using namespace std;
  48.  
  49. ;
  50.  
  51. LL w[maxn], b[maxn];
  52. int n;
  53.  
  54. int main(void){__stTIME();__IOPUT();
  55.  
  56. printf("%d", b);
  57.  
  58. int nCase;
  59. sci(nCase);
  60. while(nCase--){
  61. sci(n);
  62.  
  63. ; i<=n; i++){
  64. scl(w[i]);
  65. ) b[i] = 1LL;
  66. else b[i] = - 1LL;
  67. }
  68.  
  69. LL C = ;
  70. ; i<=n; i++)
  71. C += (w[i] * w[i]);
  72.  
  73. LL B = ;
  74. ; i<=n; i++)
  75. B += (w[i] * b[i]);
  76. B *= (- 2LL);
  77.  
  78. LL A = ;
  79. ; i<=n; i++)
  80. A += (b[i] * b[i]);
  81.  
  82. LL p = ( * A * C - B * B);
  83. LL q = * A;
  84.  
  85. LL GCD = __gcd(p, q);
  86.  
  87. p /= GCD;
  88. q /= GCD;
  89.  
  90. printf("%lld/%lld\n", p, q);
  91. }
  92.  
  93. __enTIME();;}
  94.  
  95. void __stTIME()
  96. {
  97. #if _TIME
  98. START = clock();
  99. #endif
  100. }
  101.  
  102. void __enTIME()
  103. {
  104. #if _TIME
  105. END = clock();
  106. cerr<<"execute time = "<<(double)(END-START)/CLOCKS_PER_SEC<<endl;
  107. #endif
  108. }
  109.  
  110. void __IOPUT()
  111. {
  112. #if _INPUT
  113. freopen("in.txt", "r", stdin);
  114. #endif
  115. #if _OUTPUT
  116. freopen("out.txt", "w", stdout);
  117. #endif
  118. }

HDU 5734 Acperience ( 数学公式推导、一元二次方程 )的更多相关文章

  1. HDU 5734 Acperience(数学推导)

    Problem Description Deep neural networks (DNN) have shown significant improvements in several applic ...

  2. HDU 5734 Acperience(返虚入浑)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  3. HDU 5734 Acperience (公式推导) 2016杭电多校联合第二场

    题目:传送门. #include <iostream> #include <algorithm> #include <cstdio> #include <cs ...

  4. HDU 5734 Acperience (推导)

    Acperience 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5734 Description Deep neural networks (DN ...

  5. hdu 5734 Acperience 水题

    Acperience 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5734 Description Deep neural networks (DN ...

  6. HDU 5734 Acperience

    Acperience Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. hdu 5734 Acperience(2016多校第二场)

    Acperience Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  8. HDU 2092 (将表达式变成一元二次方程形式)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2092 整数解 Time Limit: 1000/1000 MS (Java/Others)    Me ...

  9. Python数学运算的一个小算法(求一元二次方程的实根)

    请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程:ax² + bx + c = 0的两个解. #!/usr/bin/env python # -*- coding: ...

随机推荐

  1. vscode+php+xdebug won't stop at breakpoint 断点不起作用

    not stopping on breakpoints breakpoint not working 原因: 1) php.ini xdebug 端口不配置的情况下,默认是 9000,如果vscode ...

  2. php中连接tcp服务的三种方式

    首先需要现有一个 tcp 服务,我们使用 php中的 socket 系列函数实现 <?php //创建socket套接字 $socket = socket_create(AF_INET, SOC ...

  3. Python面向对象中的继承、多态和封装

    Python面向对象中的继承.多态和封装 一.面向对象的三大特性 封装:把很多数据封装到⼀个对象中,把固定功能的代码封装到⼀个代码块, 函数,对象, 打包成模块. 这都属于封装思想. 继承:⼦类可以⾃ ...

  4. 基于DNS(Consul)高可用

    DNS 推荐从Bind-DLZ入手,资料多可控制度更好(查询DNS记录SQL可定制)据说性能差 Bind-DLZhttps://www.cnblogs.com/saneri/p/8178065.htm ...

  5. 【第一季】CH07_FPGA_RunLED创建VIVADO工程实验

    [第一季]CH07_FPGA_RunLED创建VIVADO工程实验 7.1 硬件图片 先来熟悉一下开发板的硬件:LED部分及按钮部分 7.2 硬件原理图 PIN脚定义(讲解以MIZ702讲解,MIZ7 ...

  6. 第十一章 ZYNQ-MIZ702 DDR3 PS读写操作方案

      本编文章的目的主要用简明的方法在纯PS里对DDR3进行读写. 本文所使用的开发板是Miz702 PC 开发环境版本:Vivado 2015.4 Xilinx SDK 2015.4 11.0本章难度 ...

  7. 监控 Kubernetes 集群应用

    Prometheus的数据指标是通过一个公开的 HTTP(S) 数据接口获取到的,我们不需要单独安装监控的 agent,只需要暴露一个 metrics 接口,Prometheus 就会定期去拉取数据: ...

  8. GAN——ModeCollapse

    GAN——ModeCollapse 2017年05月21日 13:54:31 LiuSpark 阅读数 6821更多 分类专栏: 机器学习   版权声明:本文为博主原创文章,遵循CC 4.0 BY-S ...

  9. poj 1753高斯

    和前面的开关问题差不多,就是要理解一下我们方程等号的右端代表的含义是什么.我们建立的方程是想让对位的位置变或者不变,然后生成增广矩阵的时候要多注意一点. ac代码: #include #include ...

  10. java读取串口-mfz-rxtx-2.2-20081207-win-x86

    1.下载jar包 RXTXcomm.jar 2.实现代码 package main; import java.awt.*; import java.awt.event.*; import java.i ...