1. 程式人生 > >[LightOJ 1341] Aladdin and the Flying Carpet (算數基本定理(唯一分解定理))

[LightOJ 1341] Aladdin and the Flying Carpet (算數基本定理(唯一分解定理))

題目 fly pro sca rime memset def bool tin

題目鏈接: https://vjudge.net/problem/LightOJ-1341

題目描述:

問有幾種邊長為整數的矩形面積等於a,且矩形的短邊不小於b

算數基本定理的知識點:https://baike.baidu.com/item/%E7%AE%97%E6%9C%AF%E5%9F%BA%E6%9C%AC%E5%AE%9A%E7%90%86/10920095?fr=aladdin

 1 #include<cstdio>
 2 #include<vector>
 3 #include<cstring>
 4 #include <cmath>
 5 
 6 using
namespace std; 7 8 typedef long long ll; 9 10 const int maxn = 1e6+10; 11 12 bool vis[maxn]; 13 vector<ll> v; 14 ll a,b; 15 16 //素數打表 17 void init_prime() 18 { 19 memset(vis,1,sizeof(vis)); 20 for(ll i = 2;i < maxn;i++) 21 { 22 for(ll j = i * i;j < maxn;j += i) 23 {
24 vis[j] = 0; 25 } 26 } 27 for(ll i = 2;i < maxn;i++) 28 { 29 if(vis[i] == 1) 30 v.push_back(i); 31 } 32 } 33 34 ll a_count(ll a); 35 36 int main() 37 { 38 int t,flag = 1; 39 init_prime(); 40 scanf("%d",&t); 41 while(t--)
42 { 43 scanf("%lld%lld",&a,&b); 44 if(b >= sqrt(a)) 45 { 46 printf("Case %d: %d\n",flag++,0); 47 continue; 48 } 49 else 50 { 51 ll cnt = 0; 52 for(ll i = 1;i < b;i++) 53 { 54 if(a % i == 0) 55 cnt++; 56 } 57 ll sum = a_count(a)/2; 58 sum -= cnt; 59 printf("Case %d: %lld\n",flag++,sum); 60 } 61 } 62 return 0; 63 } 64 65 66 ll a_count(ll a) 67 { 68 if(a == 0) 69 return 0; 70 ll num,sum = 1,i = 0; 71 while(v[i] < a && i < v.size()) 72 { 73 if(a % v[i] == 0) 74 { 75 num = 0; 76 while(a % v[i] == 0) 77 { 78 a /= v[i]; 79 num++; 80 } 81 sum *= (1+num); 82 } 83 i++; 84 } 85 if(a>1) 86 sum *= 1+1; 87 return sum; 88 }

[LightOJ 1341] Aladdin and the Flying Carpet (算數基本定理(唯一分解定理))