Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
lets define F(n) as follows ,
F(n)= count of numbers from 1 to max(1,n-1) which are coprime with n .
You will be given T queries , in each query you will be given two numbers x,y .
Your task is to find F(x)+F(x+1)+.....+F(y-1)+F(y) and print it in a new line .

Input Format

The first line contains T indicating the number of queries , Each of the next T lines contain two number x,y .

Constraints

1<= T <= 100000
1 <= X <= Y <= 100000

Output Format

For each query print the answer in a new line .

Sample Input 0

2
2 5
6 10
Sample Output 0

9
22
Explanation 0

for the 1st query
F(2) = 1 ( only 1 is coprime with 2)
F(3) = 2 ( 1,2 is coprime with 2 )
F(4) = 2 ( 1,3 is coprime with 4 )
F(5) = 4 ( 1,2,3,4 is coprime with 5 )
1+2+2+4 = 9

This is the question. The sample test cases has passed. I tried a few custom test cases. Again passed, however can't understand why all other test cases are not passing. I'm getting terminated due to timeout error. I can't view the test cases too. So,I really have to idea why it's happening. The logic is correct and it works. But not all the test cases are not passing.

What I have tried:

C++
#include<iostream>
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int X,Y;
        float total= 0;
        cin>>X;
        cin>>Y;
        for(int i=X;i<=Y;i++)
        {
            int temp = i,mark =0;;
            float m =1,count=0;
            while(temp%2 == 0){
                mark = 1;
                temp=temp/2;
            }
            float FF =2;
            if(mark)
                    m *= 1-(1/FF);
            for(int j=3;j<=sqrt(temp);j=j+2)
            {
                 mark =0;
                while(temp%j==0){
                    mark = 1;
                    temp=temp/j;}
                float FF;
                FF = (float)j;           
                if(mark){
                    m *= 1-(1/FF); 
                }
            }
            if(temp>2)
            {
                float FF;
                FF = (float)temp;           
                m *= 1-(1/FF);
            }
            count = i*m;
            total += count;
        }
         long int kira = ( long int)total;
        cout<<kira<<endl;      
    }
}
Posted
Updated 1-Sep-19 20:28pm
v2

Quote:
I'm getting terminated due to timeout error. I can't view the test cases too. So,I really have to idea why it's happening.

The error message means that the program takes too much time to complete the tests.
You need to make your program faster.
Memoization can do the trick: Memoization - Wikipedia[^]
 
Share this answer
 
Comments
Kohila G 12-Sep-19 10:23am    
Thanks.
Quote:
I'm getting terminated due to timeout error

What this means, is that the platform interrupted the test run because your code took too long to calculate the results. In other words, it's too slow. And if there are such time constraints, then the correct logic is not the only thing that matters.

I don't know how much time your code is allowed to spend on it, but you do know what the worst-case input values are (because they're given under "Constraints").

So, try to run your code on the worst-case inputs, see how long it takes, and change your algorithm so that it handles these inputs faster. How you do that, is up to you - staying under the time constraints is part of the task, after all!
 
Share this answer
 
Comments
Kohila G 2-Sep-19 5:27am    
I tried the worst case in the custom test case too

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