hdu--1868--数学题<卧槽,我都TM做到数学题了>

数学渣掩面走过=-=

还好 这题 是高中知识吧... 数列求和的..

一开始 我想到了2层for最胸大无脑的方法.. 看到这数据就算了 <32位 很大啊..

又去想了会dp 没想出来 深搜感觉也不行...

就去化简式子了

x1 + x2 + x3 + ..... + xn   这边很特殊 公差d=1 所以 x2 = x1+1   x3 = x1+2  xn = x1 + n-1

那么就可以化简为  x1 * n + n*(n-1) / 2 = sum     这里的n是指数列长度  sum才是题目给我们的n 额 字母写的不好 见谅=-=  n*(n-1)/2 就是 0 , 1 ,2 ,3,4....n的和的公式

OK 到此 我也以为AC了

可是我还是TLE了...我就艾特了..

 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 }

其实你也看到了 我这边在for中多了一个判断   if( n>temp )因为有些和会比n大 然后相减是负数 但同样满足(n-temp)%i==0所以我需要剪了它

但我这样剪了 for这边还是会继续往下运行啊

其实 很简单 我只要在加一句话 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             else
20                 break;                
21         }
22         cout << ans << endl;
23     }
24     return 0;
25 }
View Code

today:

  世上成千上万种爱

  但从来没有一种可以重来

just follow your heart
原文地址:https://www.cnblogs.com/radical/p/3916373.html