LightOJ 1341 Aladdin and the Flying Carpet(算术基本定理)
2015-09-09 17:20
543 查看
算术基本定理又叫唯一分解定理。
算术基本定理内容:任何一个大于1的自然数 ,都可以唯一分解成有限个质数的乘积
,这里
均为质数,其中指数 ai是正整数。
算术基本定理的几个应用:
下面的p1,p2…pn都是素数。
同时全体正因数的和也可以写作:
本题就是第一个应用。
有几个注意的地方:获得质数的个数的时候,可以先不筛选素数,但是本题时间卡的紧,必须要先筛素数。还有注意当b*b>=a的时候就特判。
算术基本定理内容:任何一个大于1的自然数 ,都可以唯一分解成有限个质数的乘积
,这里
均为质数,其中指数 ai是正整数。
算术基本定理的几个应用:
下面的p1,p2…pn都是素数。
同时全体正因数的和也可以写作:
本题就是第一个应用。
有几个注意的地方:获得质数的个数的时候,可以先不筛选素数,但是本题时间卡的紧,必须要先筛素数。还有注意当b*b>=a的时候就特判。
[code]#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <math.h> using namespace std; const int N = 1e6+10; int t; int primes ; bool isprime ; 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; }
相关文章推荐
- UVA12589_Learning Vector
- SpringMVC之注解和参数传递
- java序列化
- vb.net DataGridViewCheckBox 状态
- Android 仿照QQ剪裁头像(完结篇)
- [Bootstrap]7天深入Bootstrap(3)CSS布局
- c++ 成员变量初始化
- Mysql 分页语句Limit用法
- iOS ZBar扫码简单实现
- redmine配置邮箱
- Linux kernel 的 sendfile 是如何提高性能的
- Oracle ORA-02020 : 过多的数据库链接在使用中解决步骤
- spring security 基础入门(配置详解)
- android的窗口机制分析------ViewRoot类
- 已知字母序列【d, g, e, c, f, b, o, a】,请实现一个函数针对输入的一组字符串 input[] = {"bed", "dog", "dear", "eye"},按照字母顺序排序并打印
- 内核同步介绍
- 关于TableView select常识性问题
- 0909 编译原理的理解
- mysql远程连接 Host * is not allowed to connect to this MySQL server问题解决方法
- Netbeans 8.0.2 with Maven 3.3.3不能正常编译项目