current position:Home>Fragment sum of 1049 sequence (20 points) (c + +)
Fragment sum of 1049 sequence (20 points) (c + +)
2022-01-27 04:30:36 【BatmannLv】
Notice:
1. Start by writing nested loops directly , Found timeout . So we need to use mathematical methods to solve .
Then refer to other people's blogs , Discover its regularity :
First observe a number , for example , about N=4 The serial number in the sequence is 3 The fragment of the number of is as follows :
( Starting from 1)[(1,2,3),(1,2,3,4)];
( Starting from 2)[(2,3),(2,3,4)];
( Starting from 3)[(3),(3,4)];
You can see it , Including i In the number of clips , The beginning of the fragment can be 1,2,3..i, altogether i A choice ,
( If included 3 The clips are 123 Three options ) The end of the clip can be i,i+1,...n, altogether n-i+1 A choice .
( If included 3 The clips are 34 Two options ).
So we can promote , For length is N The serial number in the sequence is i Number of numbers , The clip can be divided into i Parts of , The first i Part of the starting point is i,
The end points are i,i+1,i+2,...,N, common (N-i+1) individual , So sequence i The frequency of occurrence of is i*(N-i+1).
http://www.bubuko.com/infodetail-3010293.html
2. About long long sum:
“N The larger the ,double Precision error caused by multiple accumulation of values of type , Because the input is decimal , Store in double In the middle of the day ,
Binary representation is used inside the computer , And the word length of the computer is limited , Some decimal floating point numbers cannot be accurately represented by binary
It's only infinitely close , Under the limitation of word length, rounding error is inevitable , These subtle errors are N When it is large, it is accumulated many times
There will be large errors , So it is recommended not to use double Type for accurate calculation of multiple accumulation , Instead, it can accurately store
The integer of . Try to input double The value of type is expanded 1000 Double and turn to long long Integer accumulation , Use at the same time
long long Type to save sum Value , Output divided by 1000.0 Convert to floating point type and then output ( It's equivalent to moving the decimal point back 3 Behind you
Calculate again , avoid double The decimal part of the type is stored imprecisely , After multiple accumulation, it will affect the results ) But I think multiplied by 1000
Not necessarily rigorous , Maybe the test sample has at least three decimal places , If the test sample becomes four decimal places 、 Five place 、 Six ,
multiply 1000 It is equivalent to truncating directly at three decimal places , The original fourth, fifth and sixth digits may still cause
Precision problem , But if you multiply by 10000 It will go beyond long long Value , I think the most accurate is to intercept the largest of all decimals
The one with the number of digits .. Maybe there's something missing in my idea , After testing , The test sample does not exceed three decimal places ,
Although it is modified to multiply by 1000 The post code has AC, But if the test sample is slightly modified , It may lead to non AC 了 …
So put a question mark on this question first , I guess the sample title may be revised in the future …”
my ( The code timed out )
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int n;
cin >> n;
float a[n];
for(int i = 0;i < n;i++){
cin >> a[i];
}
float sum1 = 0,sum2 = 0;
for(int i = 0;i < n;i++){
sum1 = 0;
for(int j = i;j < n;j++){
sum1 += a[j];
sum2 += sum1;
}
}
printf("%.2f",sum2);
return 0;
}
Liunuo's code
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
double temp;
long long sum = 0;
for(int i = 1;i <= n;i++){
cin >> temp;
sum += (long long)(temp * 1000) * i * (n - i + 1);
}
printf("%.2f",sum / 1000.0);
return 0;
}
copyright notice
author[BatmannLv],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270430306591.html
The sidebar is recommended
- Spring IOC container loading process
- [thinking] the difference between singleton mode and static method - object-oriented programming
- Hadoop environment setup (MySQL environment configuration)
- 10 minutes, using node JS creates a real-time early warning system for bad weather!
- Git tool
- Force deduction algorithm - 92 Reverse linked list II
- What is the sub problem of dynamic programming?
- C / C + +: static keyword summary
- Idea does not have the artifacts option when configuring Tomcat
- Anaconda can't open it
guess what you like
-
I don't know how to start this
-
Matlab simulation of transportation optimization algorithm based on PSO
-
MySQL slow log optimization
-
[Vue] as the window is stretched (larger, smaller, wider and higher), the text will not be displayed
-
Popular Linux distributions for embedded computing
-
Suzhou computer research
-
After installing SSL Certificate in Windows + tomcat, the domain name request is not successful. Please answer!!
-
Implementation time output and greetings of jQuery instance
-
The 72 year old uncle became popular. Wu Jing and Guo fan made his story into a film, which made countless dreamers blush
-
How to save computer research
Random recommended
- Springboot implements excel import and export, which is easy to use, and poi can be thrown away
- The final examination subjects of a class are mathematical programming, and the scores are sorted and output from high to low
- Two pronged approach, Tsinghua Professor Pro code JDK and hotspot source code notes, one-time learning to understand
- C + + recursive knapsack problem
- The use of GIT and GitHub and the latest git tutorial are easy to understand -- Video notes of crazy God speaking
- PostgreSQL statement query
- Ignition database test
- Context didn't understand why he got a high salary?, Nginxfair principle
- Bootstrap switch switch control user's guide, springcloud actual combat video
- A list that contains only strings. What other search methods can be used except sequential search
- [matlab path planning] multi ant colony algorithm grid map path planning [including GUI source code 650]
- [matlab path planning] improved genetic algorithm grid map path planning [including source code phase 525]
- Iinternet network path management system
- Appium settings app is not running after 5000ms
- Reactnative foundation - 07 (background image, status bar, statusbar)
- Reactnative foundation - 04 (custom rpx)
- If you want an embedded database (H2, hsql or Derby), please put it on the classpath
- When using stm32g070 Hal library, if you want to write to flash, you must perform an erase. If you don't let it, you can't write continuously.
- Linux checks where the software is installed and what files are installed
- SQL statement fuzzy query and time interval filtering
- 69. Sqrt (x) (c + + problem solving version with vs runnable source program)
- Fresh students are about to graduate. Do you choose Java development or big data?
- Java project: OA management system (java + SSM + bootstrap + MySQL + JSP)
- Titanic passenger survival prediction
- Vectorization of deep learning formula
- Configuration and use of private image warehouse of microservice architect docker
- Relearn JavaScript events
- For someone, delete return 1 and return 0
- How does Java dynamically obtain what type of data is passed? It is used to judge whether the data is the same, dynamic data type
- How does the database cow optimize SQL?
- [data structure] chain structure of binary tree (pre order traversal) (middle order traversal) (post order traversal) (sequence traversal)
- Webpack packaging optimization solution
- 5. Operation element
- Detailed explanation of red and black trees
- redhat7. 9 install database 19C
- Blue Bridge Cup notes: (the given elements are not repeated) complete arrangement (arrangement cannot be repeated, arrangement can be repeated)
- Detailed explanation of springboot default package scanning mechanism and @ componentscan specified scanning path
- How to solve the run-time exception of test times
- Detailed explanation of k8s management tool kubectl
- Android system view memory command