Tuesday, 18 August 2015

Rasta and Tavas

Standard
Rasta calls a number like a Tavas if and only if 1 ≤ a ≤ n and the sum of all primes (like p) that p | a is exactly equal to k.
He asks you to find the number of Tavases.

Input format

The first and only line of input contains two integers, n and k (1 ≤ n, k ≤ 106).

Output format

Print a single integer, the number of Tavases.

Sample Input
20 7
Sample Output
3
Explanation
The only Tavases are 7, 10 and 20.

Time Limit: 2 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB 
 
Code:  
 
/* IMPORTANT: class must not be public. */

/*
 * uncomment this if you want to read input.*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.BitSet;
import java.util.HashMap;
import java.util.Map.Entry;


class TestClass {
    public static void main(String args[] ) throws Exception {
        /*
         * Read input from stdin and provide input before running
*/
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        String input[]= new String[2];
        input = line.split(" ");
        printTavas(Integer.valueOf(input[0]),Integer.valueOf(input[1]));

       
    }
   
   
   public static void printTavas(int n, int k){
    int pr[]=new int[n+2];
    for(int i=2;i<=n;i++){
        if(pr[i]==0){
            for(int j=i;j<=n;j+=i){
                pr[j]+=i;
            }
        }
    }
    int count=0;
    for(int i=0;i<=n;i++){
        if(pr[i]==k)
            count++;
    }
    System.out.println(count);
}
}
 

Related Posts:

  • Birthday Party My flatmate, Sayan, once invited all his relatives and friends from all over the city to join him on his birthday party. He is also famous for boast… Read More
  • Minimal Combinatorial Given two integers - n and r, your task is to calculate the combinatorial nCr. nCr = n! / r! (n-r)! The caveat is that you have to writ… Read More
  • Rasta and Tavas Rasta calls a number like a Tavas if and only if 1 ≤ a ≤ n and the sum of all primes (like p) that p | a is exactly equal to k. He asks you to find … Read More
  • Small Factorials Problem You are asked to calculate factorials of some small positive integers. Input An integer T, denoting the number of testcases, followed by T lines, e… Read More
  • Magnificent Fountains I and my flatmate ,Sayan, went to see the magnificient fountains in the Jubilee park on 3rd March.It was the eve of the 184rd Bithday of the late M… Read More

0 comments:

Post a Comment