http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34870
求n内的素数个数。
1 /* *********************************************** 2 Author : zch 3 Created Time :2015/5/19 8:46:16 4 File Name :a.cpp 5 ************************************************ */ 6 7 #include <cstdio> 8 #include <cstring> 9 #include <iostream> 10 #include <algorithm> 11 #include <vector> 12 #include <queue> 13 #include <set> 14 #include <map> 15 #include <string> 16 #include <cmath> 17 #include <cstdlib> 18 #include <ctime> 19 using namespace std; 20 typedef long long ll; 21 const int maxn = 1000005; 22 int prime[maxn]; 23 bool is_prime[maxn]; 24 25 int solve(int n) { 26 int p=0; 27 for(int i=0;i<=n;i++) is_prime[i]=true; 28 is_prime[0]=is_prime[1]=false; 29 for(int i=2;i<=n;i++) { 30 if(is_prime[i]) 31 { 32 prime[p++]=i; 33 for(int j=2*i;j<=n;j+=i) 34 is_prime[j]=false; 35 } 36 } 37 return p; 38 } 39 40 int main() 41 { 42 //freopen("a.txt","r",stdin); 43 //freopen("b.txt","w",stdout); 44 int n; 45 while(~scanf("%d",&n)) { 46 printf("%d\n",solve(n)); 47 } 48 return 0; 49 }
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=30390
1 /* *********************************************** 2 Author : zch 3 Created Time :2015/5/19 9:53:16 4 File Name :a.cpp 5 ************************************************ */ 6 7 #include <cstdio> 8 #include <cstring> 9 #include <iostream> 10 #include <algorithm> 11 #include <vector> 12 #include <queue> 13 #include <set> 14 #include <map> 15 #include <string> 16 #include <cmath> 17 #include <cstdlib> 18 #include <ctime> 19 using namespace std; 20 typedef long long ll; 21 ll gcd(ll a,ll b) { 22 return b==0?a:gcd(b,a%b); 23 } 24 25 26 int main() 27 { 28 //freopen("a.txt","r",stdin); 29 //freopen("b.txt","w",stdout); 30 ll x,y; 31 while(~scanf("%lld%lld",&x,&y)) { 32 printf("%lld %lld\n",gcd(x,y),x/gcd(x,y)*y); 33 } 34 return 0; 35 }
时间: 2024-11-09 04:06:25