P1147 continuous natural number and P1147 continuous Natural Number
Description
For a given natural number M, find all consecutive natural number segments. The sum of all numbers in these consecutive natural number segments is M.
Example: 1998 + 1999 + 2000 + 2001 + 2002 = 10000, so a natural number segment from 1998 to 2002 is a solution of M = 10000.
Input/Output Format
Input Format:
A single row containing an integer is given the value of M (10 <= M <= 2,000,000 ).
Output Format:
Each line has two natural numbers. The first and last numbers in a continuous natural number segment that meet the conditions are given. The two numbers are separated by a space, the first of all output rows is listed in ascending order from small to large. For a given input data, there must be at least one solution.
Input and Output sample
Input example #1:
combo.in10000
Output sample #1:
combo.out18 142 297 328 388 412 1998 2002
In fact, this question is completely unnecessary for mathematics.
Make a prefix and
Then, the brute-force enumeration can be performed.
Note that you need to add paper-cutting. Otherwise, the time-out will occur.
Always remember:
A miracle of violence !!!!
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 const int MAXN=10000001; 7 int a[MAXN]; 8 int ans=0; 9 int main()10 {11 int n;12 scanf("%d",&n);13 for(int i=1;i<=n;i++)14 a[i]=a[i-1]+i;15 for(int i=1;i<n;i++)16 {17 for(int j=i;j<n;j++)18 {19 if(a[j]-a[i]==n)20 {21 printf("%d %d\n",i+1,j);22 }23 if(a[j]-a[i]>n)break;24 }25 }26 return 0;27 }