程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 2576 數論好題 求前n項和的前n項和

hdu 2576 數論好題 求前n項和的前n項和

編輯:C++入門知識

Another Sum Problem
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1110    Accepted Submission(s): 292

 

Problem Description
FunnyAC likes mathematics very much. He thinks mathematics is very funny and beautiful.When he solved a math problem he would be very happy just like getting accepted in ACM.Recently, he find a very strange problem.Everyone know that the sum of sequence from 1 to n is n*(n + 1)/2. But now if we create a sequence which consists of the sum of sequence from 1 to n. The new sequence is 1, 1+ 2, 1+2+3, .... 1+2+...+n. Now the problem is that what is the sum of the sequence from1 to 1+2+...+n .Is it very simple? I think you can solve it. Good luck!
 


Input
The first line contain an integer T .Then T cases followed. Each case contain an integer n (1 <= n <= 10000000).
 


Output
For each case,output the sum of first n items in the new sequence. Because the sum is very larger, so output sum % 20090524.
 


Sample Input
3
1
24
56


Sample Output
1
2600
30856


Source
HDU 2009-5 Programming Contest
 


Recommend
lcy

題意 :
已知        1     2     3     4    5     6      7     8     9 。。。。n
前n項和  1    3      6   10   15   21
求x           1    4      10   20  35  56
輸入n  輸出x    即求前n項和的前n項和

思路:
假設x=sn
則 sn-sn-1=n(n+1)/2; s1=1;
求sn的通項公式  通過累加法  之後化簡 可求得公式為Sn = n(n+1)(n+2)/6;

對sn求余 我們可以分成2部分    即n(n+1)   和  (n+2)
注意 本題中要保證n(n+1)(n+2)能被6整除  因為sn一定是個整數
所以求余的時候要這樣求余
s1 = (n(n+1))% (20090524*6);
余數裡包含6是保證後面的式子可以被6整除。
s2 = (s1*(n+2)/6)%20090524;


另外本題有個坑爹的地方就是輸入只能用I64d 不能用lld  否則就錯
by hnust_xiehonghao

[cpp]
#include <iostream>  
 #include <cstdio>  
  
 using namespace std; 
  
 int main() 
 { 
     int t; 
     scanf("%d",&t); 
     while(t--) 
     { 
         long long n; 
         scanf("%I64d",&n); 
         if(n == 1) 
         { 
             printf("1\n"); 
             continue; 
         } 
         long long ans = (n*(n+1))%(20090524*6); 
         ans = (ans*(n+2)/6)%20090524; 
         printf("%lld\n",ans); 
     } 
     return 0; 
 } 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved