11526 - H(n)

All about problems in Volume CXV. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Re: 11526 - H(n)

Postby alamgir kabir » Sat May 23, 2009 3:24 pm

I have tested more than 10000 test case not so big but the code given in the problem set gives the same result that this code give.
I also check the test case that MRH gave me and this also ok now
But I am getting WA again and again.
I will be mad.............
What's wrong with this code.
Can any one explain why i am getting WA again and again though all the test case passed.
I will be ever greatful to him.

Code: Select all
#include<stdio.h>
#include<queue>
#include<math.h>
using namespace std;

int main()
{
    /*freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);*/

    long long test, num, i, sq, a, b, sum;
    queue< long long > q1, q2;

    scanf("%lld", &test);

    while( test -- )
    {
        scanf("%lld", &num);
        if( num == 0 )
        {
            printf("0\n");
            continue;
        }

        sq = sqrt( num );

        q1.push( num );
        q2.push( 1 );

        for( i = 2; i <= sq; i ++ )
        {
            if( i != num / i )
            {
                q2.push( i );
            }
            q1.push( num / i );
        }

        sum = q1.front();
        a = q1.front();

        /*printf("%ld\n", q1.front());*/

        q1.pop();

        while( !q1.empty() )
        {
            /*printf("%ld\n", q1.front());*/

            sum += q1.front();
            /*printf("sum %ld\n", sum);*/
            b = q1.front();

            q1.pop();

            if( !q2.empty() )
            {
                /*printf("%ld\n", q2.front());*/
                sum += q2.front() * ( a - b );
                q2.pop();
            }
            a = b;
        }
        while( !q2.empty() )
        {
            sum += q2.front() * ( a - q2.front() );
            q2.pop();
        }
        printf("%lld\n", sum);
    }
    return 0;
}



I have read all the post here but not getting new.
please help me.
Thnx everybody.
alamgir
alamgir kabir
New poster
 
Posts: 37
Joined: Wed Oct 03, 2007 10:42 am

Re: 11526 - H(n)

Postby liauys » Wed Jul 29, 2009 3:55 pm

To kabir, don't forget there might be negative numbers given in the input set. :)
liauys
New poster
 
Posts: 7
Joined: Thu Jul 02, 2009 6:37 pm

Re: 11526 - H(n)

Postby arifcsecu » Wed Sep 30, 2009 2:14 am

Code remove after AC
Last edited by arifcsecu on Wed Sep 30, 2009 11:51 pm, edited 1 time in total.
Try to catch fish rather than asking for some fishes.
arifcsecu
Learning poster
 
Posts: 64
Joined: Fri Sep 25, 2009 11:29 am
Location: Chittagong,University of chittagong

Re: 11526 - H(n)

Postby arifcsecu » Wed Sep 30, 2009 11:59 am

oh i got accepted now
i forget for negative numbers

when n<=0
printf("\n");

bye
Try to catch fish rather than asking for some fishes.
arifcsecu
Learning poster
 
Posts: 64
Joined: Fri Sep 25, 2009 11:29 am
Location: Chittagong,University of chittagong

Re: 11526 - H(n)

Postby saiful_sust » Wed Sep 30, 2009 6:45 pm

Hi arifcsecu
if u got acc then plz remove ur code
:D
saiful_sust
Learning poster
 
Posts: 97
Joined: Fri Aug 22, 2008 10:18 pm
Location: CSE.SUST.SYLHET

Re: 11526 - H(n)

Postby arifcsecu » Wed Sep 30, 2009 11:53 pm

i am new
thanks for ur ...........
Try to catch fish rather than asking for some fishes.
arifcsecu
Learning poster
 
Posts: 64
Joined: Fri Sep 25, 2009 11:29 am
Location: Chittagong,University of chittagong

Re: 11526 - H(n)

Postby Aunik_04 » Thu Jan 12, 2012 10:45 pm

[/code]#include<stdio.h>
#include<math.h>
int main()
{
long int dem,n,i,kase,j;
scanf("%ld",&kase);
for(j=0;j<kase;j++)
{
scanf("%ld",&n);
dem=n/2;
long long int res=0;
for(i=1;i<=dem;i++)
res+=n/i;
res=res+(n-dem);
printf("%lld\n",res);
}
return 0;
}


i got time limit error.. whats the problem??
Aunik_04
New poster
 
Posts: 1
Joined: Thu Jan 12, 2012 10:39 pm

Re: 11526 - H(n)

Postby brianfry713 » Sat Jan 14, 2012 12:20 am

You get TLE because you have a loop of n/2 including a divide. For n=2147483647, how long does your code take to run? Read this thread for ideas on a faster algorithm.
brianfry713
Guru
 
Posts: 1742
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11526 - H(n)

Postby Rashik_kuet_dour » Fri Aug 03, 2012 7:52 pm

my program is giving right output for all 9 digit numbers. but when i input 2147463638.my program gives 46475329054 as output where the actual output should be 46475375394.can't figure out what the problem is...






#include<iostream>
#include<math.h>
using namespace std;
int main ()
{
int test, j;
long long sum,i,n,b4, res;
double root_n;
cin>>test;
for(j=1; j<=test; j++)
{
cin>>n;
if (n<1)
{
cout <<0<<'\n';
continue;
}
if(n==2)
{
sum=3;
cout<<sum<<'\n';
continue;
}
if(n==3)
{
sum=5;
cout<<sum<<'\n';
continue;
}
sum=n;
b4=n;
root_n=sqrt(n);
for(i=2; i<=root_n; i++)
{
res=n/i;
sum=sum+res+(b4-res)*(i-1);
b4=res;
}
cout<<sum<<'\n';
}
return 0;
}
Rashik_kuet_dour
New poster
 
Posts: 1
Joined: Fri Aug 03, 2012 5:00 pm

Re: 11526 - H(n)

Postby tanveerahmedivan » Thu Feb 07, 2013 1:20 am

Code: Select all
#include<stdio.h>
#include<math.h>
int main()
{
   
   long long int t,k,n,i,j,l,m,a,b,c;
   while(scanf("%lld",&t)!=EOF)
   {
      for(k=1;k<=t;k++)
      {
         scanf("%lld",&n);
         if(n<=0)printf("\n");
         else
         {
            j=sqrt(n);
            l=n;
            b=n;
            for(i=2;i<=j;i++)
            {
               a=(n/i);
               c=b-a;
               l=l+(c*(i-1));
               l=l+(n/i);
               b=a;
            }
            
            for(i=b;i>j;i--)
               l=l+(n/i);
            
            printf("%lld\n",l);
         }
      }
   }
   return 0;
}

Wrong ans why...... :( :cry:
tanveerahmedivan
New poster
 
Posts: 1
Joined: Tue Jul 10, 2012 7:46 am

Re: 11526 - H(n)

Postby brianfry713 » Fri Feb 08, 2013 8:48 pm

Input:
Code: Select all
2
0
1
AC output:
Code: Select all
0
1
brianfry713
Guru
 
Posts: 1742
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11526 - H(n)

Postby sdipu » Sun May 19, 2013 2:04 am

I tried this problem at least 30 times. But every time I get WA for this code :
Code: Select all
#include <stdio.h>
#include <math.h>

long long H(int n)
{
    if (n < 0) return 0;
    if (n == 1) return 1;

    int i,p;
    long long r;

    p = sqrt(n);
    for(i=1,r=0; i<=p; ++i)
        r+= n/i;
    r <<= 1;

    if(p == n/p) r -= (long long) p*p;
    else r -= (long long) p*(n/(p+1));

    return r;
}
int main()
{

    int t,n;
    scanf("%d",&t);

    while (t--)
    {
        scanf("%d", &n);
        printf("%lld\n", H(n));
    }
    return 0;
}


I checked several input-outputs. but i couldn't find my mistake. Please help
sdipu
New poster
 
Posts: 1
Joined: Sun May 19, 2013 1:50 am

Previous

Return to Volume CXV

Who is online

Users browsing this forum: sdipu and 1 guest