首页 > 代码库 > UVA - 10539 Almost Prime Numbers (几乎是素数)
UVA - 10539 Almost Prime Numbers (几乎是素数)
题意:输入两个正整数L、U(L<=U<1012),统计区间[L,U]的整数中有多少个数满足:它本身不是素数,但只有一个素因子。
分析:
1、满足条件的数是素数的倍数。
2、枚举所有的素数,以及其倍数,将满足条件且小于等于n的个数计算出来,solve(u) - solve(l - 1)即可。
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) const double eps = 1e-8; inline int dcmp(double a, double b) { if(fabs(a - b) < eps) return 0; return a < b ? -1 : 1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const int MAXN = 1e6 + 10; const int MAXT = 10000 + 10; using namespace std; int vis[MAXN]; vector<LL> prime; void init(){ for(int i = 2; i < MAXN; ++i){ if(!vis[i]){ prime.push_back(i); for(int j = 2 * i; j < MAXN; j += i){ vis[j] = 1; } } } } LL solve(LL n){ LL ans = 0; int len = prime.size(); for(int i = 0; i < len; ++i){ LL tmp = prime[i] * prime[i]; if(tmp > n) break; while(tmp <= n){ ++ans; tmp *= prime[i]; } } return ans; } int main(){ init(); int T; scanf("%d", &T); while(T--){ LL l, u; scanf("%lld%lld", &l, &u); printf("%lld\n", solve(u) - solve(l - 1)); } return 0; }
UVA - 10539 Almost Prime Numbers (几乎是素数)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。