UVa 11408 Count DePrimes

99 篇文章 0 订阅
42 篇文章 0 订阅
A number is called a DePrime if the sum of its prime factors is a prime number.
Given a and b count the number of DePrimes xi such that a ≤ xi ≤ b.
Input
Each line contains a and b in the format ‘a b’. 2 ≤ a ≤ 5000000. a ≤ b ≤ 5000000.
Input is terminated by a line containing ‘0’.
Output
Each line should contain the number of DePrimes for the corresponding test case.
Explanation:
In Test Case 2, take 10. Its Prime Factors are 2 and 5. Sum of Prime Factors is 7, which is a prime.
So, 10 is a DePrime.
Sample Input
2 5
10 21
100 120
0
Sample Output
4
9

9

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

数论+思路~

(被网上的翻译坑得好惨,要求的是区间内质因子之和是素数的数,不是质因子个数……不过话说如果求的是区间内质因子个数之和是素数的数的话能做么?)

神奇的做法,类似于欧拉函数~

在线性筛中加上几句话,顺便求出每个数质因子的和:如果i是a[j]的倍数,那么质因子之和与i相同,再跳出循环;否则就是i的质因子之和再加上a[j],因为a[j]是质数嘛。


#include<cstdio>

int n,m,a[5000001],num[5000001],tot[5000001];
bool b[5000001];

void findd()
{
	for(int i=2;i<=5000000;i++)
	{
		if(!b[i]) a[++a[0]]=i,tot[i]=i;
		for(int j=1;a[j]*i<=5000000 && j<=a[0];j++)
		{
			b[i*a[j]]=1;
			if(!(i%a[j]))
			{
				tot[a[j]*i]=tot[i];break;
			}
			tot[a[j]*i]=tot[i]+a[j];
		}
		num[i]=num[i-1]+!b[tot[i]];
	}
}

int main()
{
	findd();
	while(scanf("%d",&n)==1 && n)
	{
		scanf("%d",&m);
		printf("%d\n",num[m]-num[n-1]);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值