CodeChef November Challenge 2013 » Yet Another Cute Girl
2013-11-04 21:04
651 查看
题目:Yet Another Cute Girl
Chef doesn't love math anymore. He loves Sasha. Sashen'ka is cute.
Chef goes on a date with her. Flowers are boring, while numbers are not. He knows that most of all this girl loves numbers, that's why he is going to bring ribbon with numbers L, L+1, L+2, ..., R written on it.
Sasha thinks that numbers with prime number of divisors are special. That's why she is going to kiss boy for each such number he will bring.
Now Chef wonder how many times he will be kissed by Sashen'ka ?
The first line of the input contains an integer T denoting the number of test cases. The description of Ttest cases follows.
The first line of each test case contains two number L, R.
For each test case, output a single line containing answer for corresponding test case.
1 ≤ T ≤ 5
1 ≤ L ≤ R ≤ 1012
0 ≤ R-L ≤ 106
Example case 1. Numbers 2,3,4,5,7,9 are special.
思路:就是求区间内素数的个数,再加上一个prime[i]^(prime[j]-1)的形式的个数
Chef doesn't love math anymore. He loves Sasha. Sashen'ka is cute.
Chef goes on a date with her. Flowers are boring, while numbers are not. He knows that most of all this girl loves numbers, that's why he is going to bring ribbon with numbers L, L+1, L+2, ..., R written on it.
Sasha thinks that numbers with prime number of divisors are special. That's why she is going to kiss boy for each such number he will bring.
Now Chef wonder how many times he will be kissed by Sashen'ka ?
Input
The first line of the input contains an integer T denoting the number of test cases. The description of Ttest cases follows.The first line of each test case contains two number L, R.
Output
For each test case, output a single line containing answer for corresponding test case.
Constraints
1 ≤ T ≤ 51 ≤ L ≤ R ≤ 1012
0 ≤ R-L ≤ 106
Example
Input: 1 1 10 Output: 6
Explanation
Example case 1. Numbers 2,3,4,5,7,9 are special.思路:就是求区间内素数的个数,再加上一个prime[i]^(prime[j]-1)的形式的个数
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <algorithm> #include <map> #include <vector> #include <set> using namespace std; #define maxn 1000100 long long num[79000]; bool prime[maxn]; int n_prime=0; bool cnt[1000001]; map<long long,int>m; vector<long long>v; long long l,r; void Prime() { memset(prime,true,sizeof(prime)); prime[0]=prime[1]=0; for(int i=2;i<maxn;i++) if(prime[i]) { num[++n_prime]=i; for(int j=2*i;j<maxn;j+=i) prime[j]=0; } //cout<<n_prime<<":"<<num[n_prime]<<endl; } void make_prime() { m.clear(); long long tmp; memset(cnt,true,sizeof(cnt)); for(int i=1;i<=n_prime;i++) { tmp=l/num[i]; while(tmp*num[i]<l||tmp<=1) tmp++; for(long long j=tmp*num[i];j<=r;j+=num[i]) { if(j>=l&&j<=r) cnt[j-l]=0; } } if(l==1) cnt[0]=0; for(long long i=0;i+l<=r;i++) if(cnt[i]&&i+l<=r&&i>=0) m[i+l]++; } long long Pow(long long a,long long b) { long long ans=1; while(b) { if(b&1) { b--; ans*=a; } else { b/=2; a*=a; } } return ans; } int main() { Prime(); long long tmp; v.clear(); for(int i=1;i<n_prime && num[i]*num[i]<1e12;i++) { for(int j=2;j<n_prime;j++) { tmp=Pow(num[i],num[j]-1); if(tmp>1e12) break; v.push_back(tmp); } } sort(v.begin(),v.end()); int T; scanf("%d",&T); while(T--) { scanf("%lld%lld",&l,&r); make_prime(); printf("%d\n",m.size()+(upper_bound(v.begin(),v.end(),r)-lower_bound(v.begin(),v.end(),l))); } return 0; }
相关文章推荐
- CodeChef November Challenge 2013 题解
- codechef_Home » Compete » January Challenge 2013 » The Minimum Number Of Moves
- CodeChef December Challenge 2013 解题报告
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And his Cake
- CF&&CC百套计划2 CodeChef December Challenge 2017 Penalty Shoot-out
- Codechef November Challenge 2015 Simple Sum
- CF&&CC百套计划2 CodeChef December Challenge 2017 Total Diamonds
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays
- codechef November Challenge 2014 .Chef and Churu
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries
- 【分块+树状数组】codechef November Challenge 2014 .Chef and Churu
- CodeChef SEGSUMQ Yet Another SubSegment Sum Problem(思路来自他人)
- CodeChef November Lunchtime 2013 Lucy and the Number Game(简单题)
- codechef November Challenge 2017解题报告
- [分治最短路 && 树链剖分]Codechef September Challenge 2017 QGRID. Querying on a Grid
- SDOI DAY1 T1 & CodeChef Divisible Subset 前缀和抽屉原理
- 刷漆(Codechef October Challenge 2014:Remy paints the fence)
- [杜教筛] Codechef January Challenge 2018 #SQRGOOD Simplify the Square Root
- BZOJ 3426 && CodeChef/CHANGE
- [题解]CodeChef APRIL Challenge 17