2016 ACM/ICPC Asia Regional Shenyang Online && hdoj5901 Count primes Lehmer

Count primes

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Problem Description

Easy question! Calculate how many primes between [1...n]!

Input

Each line contain one integer n(1 <= n <= 1e11).Process to end of file.

Output

For each case, output the number of primes in interval [1...n]

Sample Input

2
3
10

Sample Output

1
2
4

Source

2016 ACM/ICPC Asia Regional Shenyang Online

粘来的板子。。留着用

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 #define MAXN 110
 5 #define MAXM 10010
 6 #define MAXP 666666
 7 #define MAX 1000010
 8 #define LL long long int
 9 #define clr(arr) memset(arr,0,sizeof(arr))
10 #define setbit(ar, i) (((ar[(i) >> 6]) |= (1 << (((i) >> 1) & 31))))
11 #define chkbit(ar, i) (((ar[(i) >> 6]) & (1 << (((i) >> 1) & 31))))
12 #define isprime(x) (( (x) && ((x)&1) && (!chkbit(arr, (x)))) || ((x) == 2))
13
14 LL dp[MAXN][MAXM];
15 unsigned int arr[(MAX>>6)+5]={0};
16 int len=0,primes[MAXP],counter[MAX];
17
18 void SS(){
19     setbit(arr,0);
20     setbit(arr,1);
21     for(int i=3;(i*i)<MAX;i++,i++)
22     if (!chkbit(arr,i)){
23         int k=i<<1;
24         for(int j=i*i;j<MAX;j+=k)
25             setbit(arr,j);
26     }
27     for(int i=1;i<MAX;i++){
28         counter[i]=counter[i-1];
29         if (isprime(i)) primes[len++]=i,counter[i]++;
30     }
31 }
32
33 void init(){
34     SS();
35     for(int n=0;n<MAXN;n++)
36     for(int m=0;m<MAXM;m++){
37         if (!n) dp[n][m]=m;
38         else dp[n][m]=dp[n-1][m]-dp[n-1][m/primes[n-1]];
39     }
40 }
41
42 LL phi(LL m,int n){
43     if (!n) return m;
44     if (primes[n-1]>=m) return 1;
45     if (m<MAXM&&n<MAXN) return dp[n][m];
46     return phi(m,n-1)-phi(m/primes[n-1],n-1);
47 }
48
49 LL Lehmer(LL m){
50     if (m<MAX) return counter[m];
51     int s=sqrt(0.9+m);
52     int y=cbrt(0.9+m);
53     int a=counter[y];
54     LL res=phi(m,a)+a-1;
55     for(int i=a;primes[i]<=s;i++)
56         res=res-Lehmer(m/primes[i])+Lehmer(primes[i])-1;
57     return res;
58 }
59
60 int main(){
61     init();
62     LL n;
63     while(scanf("%I64d",&n)!=EOF) printf("%I64d\n",Lehmer(n));
64     return 0;
65 }
时间: 2024-09-30 23:57:36

2016 ACM/ICPC Asia Regional Shenyang Online && hdoj5901 Count primes Lehmer的相关文章

2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 388    Accepted Submission(s): 212 Problem Description For a number,if the length of continuous odd digits is even and the length

2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元

hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 802    Accepted Submission(s): 269 Problem Description hannnnah_j is a teacher in WL High school who teaches biolog

2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp

QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 859    Accepted Submission(s): 325 Problem Description Every school has some legends, Northeastern University is the same. Enter

2016 ACM/ICPC Asia Regional Shenyang Online

I:QSC and Master 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5900 题意: 给出n对数keyi,vali表示当前这对数的键值和权值,可以操作将连续的两个数合并,如果满足gcd(a[i],a[i+1])>1,得到的价值是两个数的权值和,每次合并两个数之后,这两个数就会消失,然后旁边的数会接上比如1 2 3 4 合并了 2 3 则 剩下1 4也可以合并 思路:区间dp 1:处理出任意区间内的所有数是否可以合并 对于当前的[l,r]

hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)

Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 208    Accepted Submission(s): 101 Problem Description You may not know this but it's a fact that Xinghai Square is

HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 997    Accepted Submission(s): 306 Problem Description The empire is under attack again. The general of empire is planning to defend his

2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869

Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 681    Accepted Submission(s): 240 Problem Description This is a simple problem. The teacher gives Bob a list of prob

2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873

Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 439    Accepted Submission(s): 157 Problem Description A mysterious country will hold a football world championships---Abnormal Cup

2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分

I Count Two Three Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 782    Accepted Submission(s): 406 Problem Description I will show you the most popular board game in the Shanghai Ingress Resis