HDU -- 1868 -- math problems <I have done it all TM>

Source: Internet
Author: User

Mathematical scum hiding =-=

Fortunately, this question is about high school knowledge... the sum of series ..

At the beginning, I thought of a 2-layer method with the largest chest and no brain. Even if I saw this data, I thought it would be <32 bits very big ..

I think about it again. I don't think I can find it in deep search...

Just to simplify the formula.

X1 + X2 + X3 +... + Xn Special tolerances d = 1 so X2 = X1 + 1x3 = X1 + 2 xn = X1 + n-1

Then it can be reduced to X1 * n + N * (n-1) /2 = sum here n is the index Column Length Sum is the question for us to write n letters is not good, forgive me =-= N * (n-1)/2 is 0, 1, 2, 3, 4 .... the sum of N

OK. I thought it was AC.

But I am still tle... I am about it ..

 1 #include <iostream> 2 using namespace std; 3  4 int main() 5 { 6     cin.sync_with_stdio(false); 7     int n , ans , temp; 8     while( cin >> n ) 9     {10         ans = 0;11         for( int i = 2 ; i<=n ; i++ )12         {13             temp = i*(i-1)/2;14             if(n>temp)15             {16                 if( (n-temp)%i == 0 )17                     ans ++;18             }                    19         }20         cout << ans << endl;21     }22     return 0;23 }

In fact, you can also see that I have another judgment on if (n> temp) in for, because some of them will be larger than N, and then subtract is negative, but the same is true (n-Temp) % I = 0 so I need to cut it

However, if I cut it like this, the for side will continue to run.

In fact, it is very simple. I just need to add an else break;

 1 #include <iostream> 2 using namespace std; 3  4 int main() 5 { 6     cin.sync_with_stdio(false); 7     int n , ans , temp; 8     while( cin >> n ) 9     {10         ans = 0;11         for( int i = 2 ; i<=n ; i++ )12         {13             temp = i*(i-1)/2;14             if(n>temp)15             {16                 if( (n-temp)%i == 0 )17                     ans ++;18             }    19             else20                 break;                21         }22         cout << ans << endl;23     }24     return 0;25 }
View code

 

Today:

Thousands of love types in the world

But there is never one that can be reused.

 

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.