LightOJ - 1341 Aladdin and the Flying Carpet(唯一分解定理)
2017-07-21 15:05
465 查看
#include<cstdio> using namespace std; typedef long long LL; /* 唯一分解定理的运用 一个数可以唯一分解为n=p1^s1 * p2^s2 * …… pk*sk n的正因数的个数可以表示为: num = (s1+1)*(s2+1)…(sk+1);(+1是因为可以不选这个质因数) 题中是求a=m*b (m,b)的对数且要求m>=b,因此将num/2避免重复,其中小于b的约数也是不合法的 */ const int maxn=1e6+5; int n; LL a,b; int prime[maxn];//素数 bool is_prime[maxn];//i是否是素数 int p;//素数个数 //素数筛 void sieve(int n) { p=0; for(int i=0; i<n; i++) is_prime[i]=1; is_prime[0]=is_prime[1]=0; for(int i=2; i<n; i++) { if(is_prime[i]) { prime[p++]=i; for(int j=2*i; j<n; j+=i) { is_prime[j]=0; } } } } int solve(LL a) { int cnt; int ans=1; LL t=a; if(a/b<b) return 0;//不存在合法约数 //运用唯一分解定理进行分解 for(int i=0; i<p&&prime[i]*prime[i]<=t; i++) { if(t%prime[i]==0) { cnt=0;//计算质因数i的指数 while(t%prime[i]==0) { t/=prime[i]; cnt++; } ans=ans*(cnt+1);//a约数总的个数 } } if(t>1)//如果a是质数 { ans=ans*(1+1); } ans=ans>>1;//避免重复 for(LL i=1; i<b; i++) //去除小于b的约数 { if(a%i==0) { ans--; } } return ans; } int main() { sieve(maxn); int T; scanf("%d",&T); int kase=0; while(T--) { scanf("%lld%lld",&a,&b); printf("Case %d: %d\n",++kase,solve(a)); } return 0; }
相关文章推荐
- 【算数基本定理(唯一分解定理)】Aladdin and the Flying Carpet LightOJ - 1341
- Aladdin and the Flying Carpet LightOJ - 1341(合数)唯一分解定理
- Aladdin and the Flying Carpet LightOJ - 1341 数论/唯一分解定理
- LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)
- LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理)
- LightOJ 1356 Aladdin and the Flying Carpet(唯一分解定理)
- 1341 - Aladdin and the Flying Carpet ---light oj (唯一分解定理+素数筛选)
- Light OJ 1341 Aladdin and the Flying Carpet(唯一分解定理)
- Aladdin and the Flying Carpet LightOJ - 1341 唯一分解/数论/合数分解模板
- Aladdin and the Flying Carpet 唯一分解定理
- Aladdin and the Flying Carpet 唯一分解定理
- 【基本算术定理 && 质因数分解】LightOJ - 1341 Aladdin and the Flying Carpet
- LightOJ 1341 - Aladdin and the Flying Carpet【合数分解】
- LightOJ 1341 Aladdin and the Flying Carpet(算术基本定理)
- LightOJ 1341 Aladdin and the Flying Carpet(算术基本定理)
- LightOJ 1341 Aladdin and the Flying Carpet【整数分解】
- lightoj 1341 - Aladdin and the Flying Carpet / lightoj 1236 - Pairs Forming LCM(算术基本定理)
- LightOJ 1341 Aladdin and the Flying Carpet【整数分解】
- LightOJ 1341 - Aladdin and the Flying Carpet(算术基本定理啊)
- C - Aladdin and the Flying Carpet (唯一分解定理 + 约数定理)