Ultraviolet A 10883-Supermean (combined mathematics + numerical optimization)

Source: Internet
Author: User

Question link: 10883-Supermean meaning: Calculate the super average, that is, calculate an average between two adjacent values until the next number is left, and calculate the value. Idea: drawing can easily infer the formula. Take the last group as an example. 1 2 3 4 5 1.5 2.5 3.5 4.5 2 3 4 2.5 3.5 3 observation can be found from the top to the end, looking at several other routes, there are several times, and then each number is divided by 2 of the corresponding number, which is the combination of C (n-1, [0-n-1. So ans = sum {C (n-1, I) * a [I]/2 ^ (n-1)} Then, because n is very large, it will be a tragedy. Therefore, each item takes the log value first, and then returns it to the power of the power, then each item is log (C (n-1, I) * a [I]/2 ^ (n-1) = log (C (n-1, I) + log (a [I])-(n-1) * log (2); finally, use the exp function to calculate the sum. The Code is as follows:

#include 
 
  #include 
  
   #include 
   
    #define min(a,b) ((a)<(b)?(a):(b))const int N = 50005;int t, n;double a, c;double cal(int i, double a) {return c + log(a) - (n - 1) * log(2);}int main() {int cas = 0;scanf("%d", &t);while (t--) {scanf("%d", &n);double sum = 0;c = 0;for (int i = 0; i < n; i++) {scanf("%lf", &a);if (a < 0) sum -= exp(cal(i, -a));else sum += exp(cal(i, a));c = c + log(n - i - 1) - log(i + 1);}printf("Case #%d: %.3lf\n", ++cas, sum);}return 0;}
   
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.