Sum of Consecutive Integers (Mathematics) of 51nod 1138, 51nod1138
Description:
Http://www.51nod.com/onlineJudge/questionCode.html! ProblemId = 1138
Returns a positive integer N, which is written into several continuous numbers and forms (length> = 2 ). For example, N = 15 can be 1 + 2 + 3 + 4 + 5, 4 + 5 + 6, or 7 + 8. If it cannot be written as the sum of several consecutive integers, No Solution is output.
Input
Enter 1 number N (3 <=n <= 10 ^ 9 ).
OutPut
The number of 1st consecutive integers is output. If multiple integers are arranged in ascending order and cannot be decomposed into the sum of several consecutive integers, No Solution is output.
Input example
15
Output example
147 question analysis: This question has many mathematical skills. I learned the solution in the beauty of short codes. Please read the beauty of short codes. AC code:/** *@xiaoran */#include<iostream>#include<cstdio>#include<map>#include<cstring>#include<string>#include<algorithm>#include<queue>#include<vector>#include<stack>#include<cstdlib>#include<cctype>#include<cmath>#define LL long longusing namespace std;int main(){int n;while(cin>>n){ stack<int> st; int i=1; while(n-i>1){ n-=i++; if(n%i==0){ //cout<<n/i<<endl; st.push(n/i); } } if(st.empty()){ cout<<"No Solution"<<endl; continue; } while(!st.empty()){ cout<<st.top()<<endl; st.pop(); }}return 0;}
Appendix: calculates the number of consecutive sequences that comprise n and the composition of these sequences.
/*** The sum of consecutive numbers, the beauty of short codes * and find these combinations */# include <iostream> using namespace std; void print (int n) {// print these sequence combinations int I = 0, s = n; while (n-I> 0) {n-= I ++; if (n % I = 0) {cout <s <"="; int k, k1; for (k = 0, k1 = n/I; k <I-1; k ++, k1 ++) {cout <k1 <"+";} cout <k1 <endl ;}} int main () {int n; while (cin> n) {int sum = 0, I = 0; print (n); while (n> 0) {n-= ++ I; n % I | sum ++;} cout <sum <endl;} return 0 ;}