ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1120. Sum of Sequential Numbers

GOOD SOLUTION!
Posted by Gerasim Petrov Velchev 9 Aug 2008 00:58
#include<iostream>
#include<cmath>
using namespace std;
int sum (int a,int b) {return (!((b-a+1)%2))?(b-a+1)/2*(a+b):(b-a)/2*(a+b-1)+b;}
int main () {
int n;
cin>>n;
int a_real,p_real;
int p=(int)(sqrt((double)(n*2)));
do {
int a=(((n*2)/p-(p-1))/2);
if (a&&sum(a,a+p-1)==n) {a_real=a;p_real=p;break;}
p--;
}
while (p);
cout<<a_real<<" "<<p_real<<endl;
return 0;
}
Re: GOOD SOLUTION!
Posted by Egor Stepanov [mikroz] 16 Dec 2008 04:16
strange solution...
there is a simpler way.
Re: GOOD SOLUTION!
Posted by S.77 3 Aug 2011 21:56
//0.015 sec, 108 КiB
#include <stdio.h>
int main(void){
    unsigned n,p,s;
    scanf("%u",&n);
    for(p=44720;n<(s=(p*(p+1))>>1)||(n-s)%p;p--);
    printf("%u %u\n",1+(n-s)/p,p);
    return 0;
}
Re: GOOD SOLUTION!
Posted by Artur Mazgarov 31 Jan 2013 14:59
EXCELLENT! When I got it))