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 1209. 1, 10, 100, 1000...

Simple solution
Posted by Marcin Mika 24 Jan 2003 00:24
We need to check whether the given number is of the form:
k(k+1)/2+k+2
just consider the quadratic in k and check if
9-4*(4-2*N) is a perfect square.

#include <stdio.h>
#include <math.h>
#include <string>

using namespace std;

char solve( int n ){
if ( n==1 ) return '1';

double w;
double N=n;

w=int( sqrt( double( 9.0-4.0*(4.0-2.0*N) ) )+0.0000001 );
if ( w*w==9.0-4.0*(4.0-2.0*N) ) return '1';
return '0';
}

int main( void ){
int n,k;

scanf( "%d", &n );
while ( n-- ){
 scanf( "%d", &k );
 putc( solve(k), stdout );
 putc( ' ', stdout );
}

}
Re: Simple solution
Posted by Accepted 29 Jan 2003 20:34
why my program dont work ?

  var
    a               : array [1..65536] of longint;
    n,i,k           : longint;
  begin
    read (n);
    for i := 1 to n do
      read (a[i]);
    for i := 1 to n do
      begin
        k := round(sqrt((a[i]-1)*2));
        if (k*(k+1)) div 2 = a[i] - 1 then write ('1 ') else write
('0 ');
      end;
  end.
Re: Why do you add 0.0000001?
Posted by Igor Dex 2 Feb 2003 02:25
> We need to check whether the given number is of the form:
> k(k+1)/2+k+2
> just consider the quadratic in k and check if
> 9-4*(4-2*N) is a perfect square.
>
> #include <stdio.h>
> #include <math.h>
> #include <string>
>
> using namespace std;
>
> char solve( int n ){
> if ( n==1 ) return '1';
>
> double w;
> double N=n;
>
> w=int( sqrt( double( 9.0-4.0*(4.0-2.0*N) ) )+0.0000001 );
> if ( w*w==9.0-4.0*(4.0-2.0*N) ) return '1';
> return '0';
> }
>
> int main( void ){
> int n,k;
>
> scanf( "%d", &n );
> while ( n-- ){
>  scanf( "%d", &k );
>  putc( solve(k), stdout );
>  putc( ' ', stdout );
> }
>
> }
>
No subject
Posted by Joao Batista de Souza Leao Neto 13 Feb 2003 08:47
> #include <stdio.h>
> #include <math.h>
> #include <string>
>
> using namespace std;
>
> char solve( int n ){
> if ( n==1 ) return '1';
>
> double w;
> double N=n;
>
> w=int( sqrt( double( 9.0-4.0*(4.0-2.0*N) ) )+0.0000001 );
> if ( w*w==9.0-4.0*(4.0-2.0*N) ) return '1';
> return '0';
> }
>
> int main( void ){
> int n,k;
>
> scanf( "%d", &n );
> while ( n-- ){
>  scanf( "%d", &k );
>  putc( solve(k), stdout );
>  putc( ' ', stdout );
> }
>
> }
>
Re: Simple solution
Posted by 王平 3 Nov 2004 22:20
/*My problem don't work too.WHY!!             */
/*SOS!!!*/
#include<iostream>
#include<vector>
using namespace std;
int main(){
    long i,k,ar[500],t;
    vector<int> num;
    vector<int>::iterator it;
    for(i=0;i<500;i++) ar[i]=i*(i+1)/2+1;
    cin>>k;
    while(k--) {cin>>t;num.insert(num.end(),t); }
    it=num.begin();
    while(it!=num.end()){
        t=*it++;i=500;
        while(t<ar[--i]);
        if(t==ar[i]) cout<<'1'<<' ';
        else cout<<'0'<<' ';
    }
    return 0;
}