P1147 continuous natural number and P1147 continuous Natural Number

Source: Internet
Author: User

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 }

 




Related Article

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.