(筛法求素数的灵活运用, 递推).Horace and his primes

H. Horace and his primes

1000ms
1000ms
262144KB
64-bit integer IO format:  %lld      Java class name:  Main
Font Size:   

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, because7 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 nbetween 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 and1 ≤ 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:
1
90 90 3

Output:
1

Example 2

Input:
5
2 9 1
2 9 2
800 810 4
999999 1000000 2
100000 1000000 1000000

Output:
4
4
5
2
0


  
  
题意: 给出N,然后将N的素数因子求和,如果和为不为素数,继续求这个和的素数因子,在把和的素数因子求和……一直到和为素数。
例:n = 90, 90 = 2 * 3^2 * 5, sum = 2 + 3 + 5 = 10; n = 10, 10 = 2 * 5; sum = 7; 停止游戏。游戏结束可以得到一个序列,90、10,、7.故序列数K = 3。问[A,B]中序列数为K个数。
思路:素数筛法打表,预处理后直接输出就行了。
#include <iostream>
#include <algorithm>
#include <functional>

#define NMAX 1000001

int array[NMAX];
bool prime[NMAX];
int ks[NMAX];
int matrix[12][NMAX];

void init()
{
	std::fill(prime, prime + NMAX, true);
	prime[0] = prime[1] = false;
	for (int i = 2; i <= NMAX - 1; i++)//在使用"筛法求素数"时求题意要求的每个数的所有质因子的和
	{
		if (prime[i])
		{
			array[i] = i;
			for (int j = i + i; j <= NMAX - 1; j += i)
			{
				array[j] += i;
				prime[j] = false;
			}
		}
	}
	int acum[12];
	std::fill(acum, acum + 12, 0);
	for (int i = 1; i <= 1000000; i++){
		if (prime[i])
			ks[i] = 1;
		else
			ks[i] = 1 + ks[array[i]];//递推求每个数的K值
		acum[ks[i] - 1]++;
		for (int j = 0; j < 12; j++)//生成矩阵,得到从1到i,K值等于j的数的个数
			matrix[j][i] = acum[j];
	}
}

int main()
{
	init();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       - 
	int P;
	while (std::cin >> P)
	{
		int A, B, K;
		for (int i = 0; i < P; i++)
		{
			std::cin >> A >> B >> K;
			if (K > 12)
				std::cout << "0" << std::endl;
			else
				std::cout << matrix[K - 1][B] - matrix[K - 1][A - 1] << std::endl;

		}
	}
	return 0;
}


 
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值