Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a problm input n is highest 10^10;for worst case it must give TLE ...in this problem i have got probably everything ok but getting TLE for worst case may be due to using for loop...here time limit is 1 sec.there are two input n maximum 10^5,k maximum 10^9.how to overcome..thanks in advance

What I have tried:

#include <iostream>
using namespace std;

int main()
{

    long long n,k;
    while(cin>>n>>k)
    {
      long long a[n];
      for(int i=0;i<n;i++)>
      {
          cin>>a[i];
      }
        int count=0;

        int i=1;
        //count=i*(i+1)/2;
        while(k>i*(i+1)/2)
        {
            i++;
            //count=i*(i+1)/2;
        }

        int len=i*(i+1)/2;
        int j,x=0;
        for(j=len,x=i-1;j>k;j--)           //may be here is the problem i guess..
        {
            x--;
        }
        cout<<a[x]<<endl;
    }

    //cout << "Hello world!" << endl;
    return 0;
}
Posted
Updated 5-May-16 19:14pm
v2
Comments
Richard MacCutchan 5-May-16 14:04pm    
What?
Sergey Alexandrovich Kryukov 5-May-16 14:25pm    
It does not seem to make any sense. Could you explain the problem clearly?
—SA

1 solution

LOL I agree with Richard & Sergey in WTF but even funnier is reading the code

I am wondering if it ever comes out surely it just deadloops forever if value k is too big that is my guess.

These lines of code are the best ever
int i=1;
     while(k>i*(i+1)/2)

Lets give you a hint:
i) k is a LONG LONG and i is an INTEGER.
- You are going to be a little hard pressed to multiply roll an INT up to a LONG LONG

Then you go and do it all again with integer j.

I must say you got me, I can't work out if you are trying to get upperand lower longs of the long long or get the square root in a strange way. It sort of looks like you may be trying to do this
unsigned long llsqrt (unsigned long long x){
	unsigned long long op, res, one;

	op = x;
	res = 0;

	/* "one" starts at the highest power of four <= than the argument. */
	one = 0x4000000000000000;  /* second-to-top bit set  bit */
	while (one > op) one >>= 2;

	while (one != 0) {
		if (op >= res + one) {
			op -= res + one;
			res += one << 1; 
		}
		res >>= 1;
		one >>= 2;
	}
	return (unsigned long) res;
}
 
Share this answer
 
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900