BZOJ 3181|COCI 2012|Broj|数学|容斥|二分|筛法
2016-04-23 12:33
381 查看
咦 Rank 7?
根号算法?也不能叫根号算法。。
令H(n)为我们划分的标准。(H(n)的表示好像在2015还是2014的国家集训队论文里这么写的)
若p≥H(n),筛法找出[1,109p]中的比p小的质数,然后选择第N个数字。
p≤H(n),先找出[1,p)的质数,二分容斥。
至于H(n)的取值,并不会理论推导。。这个值按照经验设吧。。
取65好像是比较优的。
小的正整数)。p一定是质数。若答案超过10^9则输出0。
of Eratosthenes. Size of our sieve will be 109/P. Integers in the sieve
represent multiples of P. During the execution of this algorithm we can
find smallest prime factors or mark only multiples of prime numbers
smaller than P as in the official solution.
For smaller values of P we can binary search through [1,109/P],
again looking at these numbers as the corresponding multiples of P.
For some number we must find the number of integers not greater and
relatively prime with that number. We can do this by using inclusionexclusion
principle with prime numbers less than P.
With careful implementation this solution can work for much larger
values of P than requested for this subtask.
We can also solve this task for smaller value of P by making use of
periodic behaviour of smallest prime factors. Let A(n) be the smallest
prime factor of n, B(k) the k-th prime number, and T(k) the product
of first k primes. For A(n)≤B(k), A(n+T(k))=A(n) holds. So it’s
enough to know A(n) for n≤T(k) in order to find the N-th prime
who’s smallest prime factor is B(k)
Necessary skills: sieve of Eratosthenes, inclusion-exclusion principle
Category: number theory, combinatorics
根号算法?也不能叫根号算法。。
令H(n)为我们划分的标准。(H(n)的表示好像在2015还是2014的国家集训队论文里这么写的)
若p≥H(n),筛法找出[1,109p]中的比p小的质数,然后选择第N个数字。
p≤H(n),先找出[1,p)的质数,二分容斥。
至于H(n)的取值,并不会理论推导。。这个值按照经验设吧。。
取65好像是比较优的。
#include <cstdio> #include <cstring> #define rep(i,j,k) for(i=j;i<k;++i) typedef long long ll; const int C = 65, N = 1000000000; bool v[20000000]; int pri[100], tot, n, p; ll f(int mid, int p) { int i, j, k; ll ret = mid, d, f; rep(i,2,p) v[i] = 0; tot = 0; rep(i,2,p) { if (!v[i]) pri[tot++] = i; for (j = 0; j < tot && i * pri[j] < p; ++j) { v[i * pri[j]] = 1; if (i % pri[j] == 0) break; } } for (k = 1; k < (1 << tot); ++k) { d = 1, f = -1; rep(i,0,tot) if (k & (1 << i)) { d *= pri[i]; if (d > mid) break; f *= -1; } ret -= f * mid / d; } return ret; } int work1() { int ans = 0, l = 1, r = N / p; while (l < r) { int mid = l + r >> 1; if (f(mid, p) < n) l = mid + 1; else r = mid; } if (f(l, p) == n) ans = l * p; return ans; } int work2() { int ans = 0, len = N / p + 1, i, k = 1; if (n == 1) ans = p; rep(i,2,len) v[i] = 0; rep(i,2,len) if (!v[i]) if (i < p) for (ll j = (ll) i * i; j < len; j += i) v[j] = 1; else if (++k == n) ans = i * p; return ans; } int main() { while (scanf("%d%d", &n, &p) != EOF) printf("%d\n", p < C ? work1() : work2()); return 0; }
3181: [Coci2012]BROJ
Description
求最小质因子等于p的第n小的正整数(恰好有n-1个最小质因子等于p且比它小的正整数)。p一定是质数。若答案超过10^9则输出0。
Sample Input
2 3Sample Output
9HINT
1 <= n, p <= 10^9Solution
To solve this for large values of P we will use modification of the sieveof Eratosthenes. Size of our sieve will be 109/P. Integers in the sieve
represent multiples of P. During the execution of this algorithm we can
find smallest prime factors or mark only multiples of prime numbers
smaller than P as in the official solution.
For smaller values of P we can binary search through [1,109/P],
again looking at these numbers as the corresponding multiples of P.
For some number we must find the number of integers not greater and
relatively prime with that number. We can do this by using inclusionexclusion
principle with prime numbers less than P.
With careful implementation this solution can work for much larger
values of P than requested for this subtask.
We can also solve this task for smaller value of P by making use of
periodic behaviour of smallest prime factors. Let A(n) be the smallest
prime factor of n, B(k) the k-th prime number, and T(k) the product
of first k primes. For A(n)≤B(k), A(n+T(k))=A(n) holds. So it’s
enough to know A(n) for n≤T(k) in order to find the N-th prime
who’s smallest prime factor is B(k)
Necessary skills: sieve of Eratosthenes, inclusion-exclusion principle
Category: number theory, combinatorics
相关文章推荐
- eclipse中 将java项目转换为web项目
- JavaScript类型总览(图)
- 前端要给力之:代码可以有多烂?
- 链表中倒数第k个结点
- 基础DOM和CSS操作(三)
- Android进阶笔记16:onInterceptTouchEvent、onTouchEvent与onTouch
- shell基础
- colorAccent,colorPrimary,colorPrimaryDark
- Sketch
- Linux界面切换
- RHEL-6忘记root密码的解决办法
- 1685: Route Planning
- C++输入输出流
- 使用ViewPager和Fragment实现底部导航滑动重构版
- referrer地重要性
- html5_websocket_tomcat8
- Java web:SSH项目拷贝产生问题以及解决(getContextPath() xml hibernate等报错)
- HDU 5672 String(尺取法)
- Accessing Members of an Enclosing Class
- javascript 传参练手