首页 > 代码库 > UVa 11105 semi-prime H-numbers
UVa 11105 semi-prime H-numbers
方法:素数筛选
素数筛选法的推广。先求出H-primes, 可以证明得到,任意两个H-prime相乘,结果都是semi-prime H-number。求出范围内所有semi-primes,预处理前缀和即可。
(这里用到一种O(n) 素数筛选的方法)
code:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include <cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue> #include <fstream> #include <cassert> #include <unordered_map> #include <cmath> #include <sstream> #include <time.h> #include <complex> #include <iomanip> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a)) #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a)) #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a)) #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a)) #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a)) #define FOREACH(a,b) for (auto &(a) : (b)) #define rep(i,n) FOR(i,0,n) #define repn(i,n) FORN(i,1,n) #define drep(i,n) DFOR(i,n-1,0) #define drepn(i,n) DFOR(i,n,1) #define MAX(a,b) a = Max(a,b) #define MIN(a,b) a = Min(a,b) #define SQR(x) ((LL)(x) * (x)) #define Reset(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define mp make_pair #define pb push_back #define all(v) v.begin(),v.end() #define ALLA(arr,sz) arr,arr+sz #define SIZE(v) (int)v.size() #define SORT(v) sort(all(v)) #define REVERSE(v) reverse(ALL(v)) #define SORTA(arr,sz) sort(ALLA(arr,sz)) #define REVERSEA(arr,sz) reverse(ALLA(arr,sz)) #define PERMUTE next_permutation #define TC(t) while(t--) #define forever for(;;) #define PINF 1000000000000 #define newline ‘\n‘ #define test if(1)if(0)cerr using namespace std; using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> ii; typedef pair<double,double> dd; typedef pair<char,char> cc; typedef vector<ii> vii; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> l4; const double pi = acos(-1.0); set<ll> ans; vector<ll> primes; const int maxn = 25e4; bitset<maxn+1> vis(0); int cnt[4*maxn+1] = {0}; void init() {
repn(i, maxn) { if (!vis[i]) primes.pb(i); for (auto p : primes) { ll nxt = i + p + 4 * p * i; if (nxt > maxn) break; vis[nxt] = true; if ((i-p)%(4*p+1) == 0) break; } } for (auto i : primes) for (auto p : primes) { ll nxt = i + p + 4 * p * i; if (nxt > maxn) break; cnt[nxt*4+1] = true; } repn(i, 4*maxn) cnt[i] += cnt[i-1]; } int main() { init(); int n; while (cin >> n && n) { cout << n << " " << cnt[n] << newline; } }
UVa 11105 semi-prime H-numbers
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。