LightOJ 1341 Aladdin and the Flying Carpet(算术基本定理)
来源:互联网 发布:淘宝自动优惠怎么取消 编辑:程序博客网 时间:2024/05/17 02:48
算术基本定理又叫唯一分解定理。
算术基本定理内容:任何一个大于1的自然数 ,都可以唯一分解成有限个质数的乘积 ,这里 均为质数,其中指数 ai是正整数。
算术基本定理的几个应用:
下面的p1,p2…pn都是素数。
同时全体正因数的和也可以写作:
本题就是第一个应用。
有几个注意的地方:获得质数的个数的时候,可以先不筛选素数,但是本题时间卡的紧,必须要先筛素数。还有注意当b*b>=a的时候就特判。
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <math.h>using namespace std;const int N = 1e6+10;int t;int primes[N];bool isprime[N];void getprimes(){ memset(isprime,true,sizeof(isprime)); isprime[1] = false; t = 0; for(int i=2;i<N;i++) { if(isprime[i]) { primes[t++] = i; for(int j=2;i*j<N;j++) isprime[i*j] = false; } } return;}int getfac(long long x){ int ans =1; for(int i=0; i<t && primes[i]*primes[i]<=x ;i++) { if(x==1) break; int tmp=0; while(x%primes[i]==0) { tmp++; x/=primes[i]; } ans*=(tmp+1); } if(x!=1) ans*=2; return ans;}int main(){ getprimes(); int n; scanf("%d",&n); for(int cas =1 ;cas<=n;cas++) { long long a,b; scanf("%lld %lld",&a,&b); if(b*b>=a) { printf("Case %d: 0\n",cas); continue; } int ans = getfac(a); ans /= 2; for(int i=1;i<b;i++)//将不符合情况的种类剔除掉。 { if(a%i==0) ans--; } printf("Case %d: %d\n",cas,ans); } return 0;}
0 0
- LightOJ 1341 Aladdin and the Flying Carpet(算术基本定理)
- LightOJ 1341 - Aladdin and the Flying Carpet(算术基本定理 唯一分解定理)
- LightOJ 1341 Aladdin and the Flying Carpet(算术基本定理)
- lightOj 1341Aladdin and the Flying Carpet 算数基本定理
- Aladdin and the Flying Carpet(算术基本定理)
- Aladdin and the Flying Carpet (算术基本定理)
- Aladdin and the Flying Carpet (算术基本定理)
- lightoj 1341 Aladdin and the Flying Carpet (唯一分解定理)
- LightOJ Aladdin and the Flying Carpet 1341【算数基本定理+几何】
- lightOJ 1341 Aladdin and the Flying Carpet
- LightOJ 1341 Aladdin and the Flying Carpet
- LightOJ 1341Aladdin and the Flying Carpet
- LightOJ 1341 - Aladdin and the Flying Carpet
- LightOJ 1341 Aladdin and the Flying Carpet
- 【算数基本定理(唯一分解定理)】Aladdin and the Flying Carpet LightOJ
- Lightoj1341——Aladdin and the Flying Carpet(算术基本定理)
- Aladdin and the Flying Carpet LightOJ
- Aladdin and the Flying Carpet LightOJ
- android的窗口机制分析------ViewRoot类
- UVA12589_Learning Vector
- vb.net DataGridViewCheckBox 状态
- 第一次在github上提交项目
- 安卓依赖注入
- LightOJ 1341 Aladdin and the Flying Carpet(算术基本定理)
- Android 仿照QQ剪裁头像(完结篇)
- 忘mysql root密码之MYSQLADMIN修改密码方法
- linux 编译安装的时候提示/usr/bin/ld: cannot find -lc
- iOS地图及定位功能基本实现的详尽描述
- Java并发编程:CountDownLatch、CyclicBarrier和Semaphore
- Angular学习
- C++STL之函数对象及谓词
- 用CSP生成RSA密钥对