SPOJ - TAP2013H D - Horace and his primes 素数筛+素数分解+打表

来源:互联网 发布:形容男生的网络词语 编辑:程序博客网 时间:2024/04/30 23:00

[The original version of this problem (in Spanish) can be found at https://www.dc.uba.ar/events/icpc/download/problems/tap2013-problems.pdf]

Horace likes to play writing natural numbers in the blackboard in his bedroom. One of his favourite games consists in first writing a number n, then the sum of all the different prime numbers that divide n, and so on until the number written on the board becomes a prime number. For example, if Horace begins writing the number n = 90, because 90 = 2 × 32 × 5 the next number to be written will be 2 + 3 + 5 = 10; then, as 10 = 2 × 5 Horace will write the number 2 + 5 = 7; finally, because 7 is a prime number the game will end here.

Formally, in this game each natural number n >= 2 defines a sequence whose first element is n, and each new element is the sum of all the prime numbers that divide the previous element in the sequence. The order of the game is the position of the first prime number in the sequence, and coincides with the total number of numbers written on the blackboard one the game has ended. In the example from the previous paragraph, with n = 90 the order of the game is K = 3, because the numbers that are written will be 90, 10 and 7.
Now, not all games are equally entertaining to Horace, and in this case he prefers to begin by writing a number n such that the order of the corresponding game is a particular value K. Horace would like to know how many different values of n between A and B inclusive satisfy this condition, but because he does not know how to code he needs someone to do this calculation for him. Can you help him?
INPUT
The first line contains an integer P which indicates the number of questions Horace wants to ask you (1 <= P <= 10^5). Each of the next P lines describes a question using three integer numbers A, B and K, which mean that Horace would like to know how many different values of n satisfy that A <= n <= B and the order of the game beggining with n is K (2 <= A <= B <= 10^6 and 1 <= K <= 10^6).
OUTPUT
You should print P lines, each one containing an integer number with the answer to one of the questions made by Horace, in the order in which they appear in the input.

Formally, in this game each natural number n ≥ 2 defines a sequence whose first element is n, and each new element is the sum of all the prime numbers that divide the previous element in the sequence. The order of the game is the position of the first prime number in the sequence, and coincides with the total amount of numbers written on the blackboard once the game has ended. In the example from the previous paragraph, with n = 90 the order of the game is K = 3, because the numbers that are written will be 9010 and 7.

Now, not all games are equally entertaining to Horace, and in this case he prefers to begin by writing a number n such that the order of the corresponding game is a particular value K. Horace would like to know how many different values of n between A and B inclusive satisfy this condition, but because he does not know how to code he needs someone to do this calculation for him. Can you help him?

 

Input

The first line contains an integer P which indicates the number of questions Horace wants to ask you (1 ≤ P ≤ 105). Each of the next P lines describes a question using three integer numbers AB and K, which mean that Horace would like to know how many different values of n satisfy that A ≤ n ≤ B and the order of the game beginning with n is K (2 ≤ A ≤ B ≤ 106 and 1 ≤ K ≤ 106).

 

Output

You should print P lines, each one containing an integer number with the answer to one of the questions made by Horace, in the order in which they appear in the input.

 

Example 1

Input:190 90 3Output:1

Example 2

Input:52 9 12 9 2800 810 4999999 1000000 2100000 1000000 1000000Output:44520


#include <iostream> #include <cstdio>#include <cstdlib>#include <cmath>#include <fstream>#include <algorithm>#include <climits>#include <cstring>#include <string>#include <set>#include <map>#include <queue>#include <stack>#include <vector>#include <list>const int maxn=5e2+10;using namespace std;typedef  long long ll;typedef  unsigned long long  ull; const int N = 1e6+5;ll T;ll a,b,k;ll yi[1000005];int isprime[1000005];int prime[1000005];int sum[1000005];int re[N][20];int cnt  = 0;void initprime(){for(int i=2;i<N;i++){isprime[i] = true;}for(int i=2;i<N;i++){if(isprime[i]){prime[++cnt]=i;for(int j=i<<1;j<N;j+=i){isprime[j] = false;sum[j] += i;//在筛素数时就找出其和 } }} for(int i=2;i<N;i++){if(isprime[i])yi[i] = 1;else{int tmp = i,ct = 1;//找其k大小 while(!isprime[tmp]){tmp = sum[tmp];ct++;}yi[i] =ct;}} //打表 for(int i=2;i<N;i++){for(int j=1;j<=16;j++){if(yi[i]==j){re[i][j] = re[i-1][j]+1;}else{re[i][j] = re[i-1][j];}}} }int main(){ios::sync_with_stdio(false);  cin >>T;initprime();//for(int i=1;i<=10;i++)//cout <<i<<":"<< sum[i] <<endl; while(T--){ll ans  = 0;cin >> a>>b>>k;if(k>=16){cout <<"0"<<endl;continue;}ans = re[b][k] - re[a-1][k];cout <<ans<<endl;}return 0;} 



0 0