LightOJ 1341 Aladdin and the Flying Carpet 算数基本定理
题目大意:给出面积n,和最短边m,求能形成的矩形的个数(不能为正方形)。
题目思路:根据算数基本定理有:
1.每个数n都能被分解为:n=p1^a1*p2^a2*^p3^a3……pn^an(p为素数);
2.n的正因数的个数sum为:sum=(1+a1)*(1+a2)*(1+a3)……(1+an);
最短边为m,若m>=sqrt(n),则无解。所以m最多我10^6,可遍历找出1-m中n的因子,并用sum去减去这类因子的个数。
ps:最近一直想去证明算数基本定理,可是感觉能力不够,唉,慢慢来吧。
#include<cstdio>
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
#define MAX 1000005
#define mod 1000000007 using namespace std; long long p[MAX],v[MAX],num_prime; void GetPriem()//获得素数
{
memset(p,,sizeof(p));
memset(v,,sizeof(v));
for(int i=;i<MAX;i++)
{
if(!v[i])
{
p[++num_prime]=i;
for(int j=i;j<MAX;j+=i)
v[j]=;
}
}
} long long Ans(long long n)
{
long long cnt,sum=;
for(int i=;i<=num_prime && p[i]<=n;i++)
{
if(n%p[i]==)
{
cnt=;
while(n%p[i]==)
{
cnt++;
n/=p[i];
}
sum*=(cnt+);
}
}
if(n > )
sum*=;
return sum;
} int main()
{
GetPriem();
int T,cnt=,i,j;
long long n,m;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld",&n,&m);
if(n/m < m)
{
printf("Case %d: %lld\n",cnt++,);
continue;
}
long long ans=Ans(n)/;
for(i=;i<m;i++)
{
if(n%i==)
ans--;
}
printf("Case %d: %lld\n",cnt++,ans);
}
return ;
}
LightOJ 1341 Aladdin and the Flying Carpet 算数基本定理的更多相关文章
- [LightOJ 1341] Aladdin and the Flying Carpet (算数基本定理(唯一分解定理))
题目链接: https://vjudge.net/problem/LightOJ-1341 题目描述: 问有几种边长为整数的矩形面积等于a,且矩形的短边不小于b 算数基本定理的知识点:https:// ...
- LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...
- LightOJ - 1341 Aladdin and the Flying Carpet 唯一分解定理LightOJ 1220Mysterious Bacteria
题意: ttt 组数据,第一个给定飞毯的面积为 sss,第二个是毯子的最短的边的长度大于等于这个数,毯子是矩形但不是正方形. 思路: 求出 sss 的所有因子,因为不可能是矩形,所以可以除以 222, ...
- LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理)
http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. 思路 ...
- LightOJ 1341 - Aladdin and the Flying Carpet 基本因子分解
http://www.lightoj.com/volume_showproblem.php?problem=1341 题意:给你长方形的面积a,边最小为b,问有几种情况. 思路:对a进行素因子分解,再 ...
- LightOJ 1341 - Aladdin and the Flying Carpet
题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你地毯面积和最小可能边的长度,让你求有几种组合的可能. 题解:这题就厉害 ...
- LightOJ 1341 Aladdin and the Flying Carpet【整数分解】
题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1341 题意: 给定一个数,将其拆分成两个数的乘 ...
- LightOJ 1341 Aladdin and the Flying Carpet 数学
题意:给个矩形的面积a,和矩形的最小边长b,问有多少种矩形的方案(不能是正方形) 分析:a可以写成x,y,因为不能是正方形,所以设x<y,那么x<sqrt(a),y>sqrt(a) ...
- LightOJ 1341 Aladdin and the Flying Carpet(整数拆分定理)
分析:题目并不难理解,就是一些细节上的优化需要我们注意,我在没有优化前跑了2000多MS,优化了一些细节后就是400多MS了,之前还TLE了好几次. 方法:将整数拆分为质因子以后,表达为这样的形式,e ...
随机推荐
- 【LeetCode】24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- POJ 2313 Sequence#贪心
(- ̄▽ ̄)-* 找规律 //初始化为B[i]=A[i] //然后由V=|A[1]-B[1]|+|A[2]-B[2|+|A[3]-B[3]| // +|B[1]-B[2]|+|B[2]-B[3]| / ...
- Centos虚拟机安装分区分配
i 安装hadoop虚拟机
- LeetCode OJ 122. Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- chapter 12_2 保存无环的table
保存table有几种方法,选用哪种方法取决于对table的结构作出了哪些限制性的假设 第一个方法: function serialize(o) if type(o) == "number&q ...
- SpringMVC利用Hibernate validator做字段验证
1.添加Hiberbate validator相关的jar包 2.字需要验证的formbean 上添加验证的注解,内置注解有: dBean Validation 中内置的 constraint @Nu ...
- Xib学习之(1)
刚学xib就遇到一个问题 最后还是朋友帮忙解决了 就是在设置约束时,设置的是左右约束为0,可是最后弄出来却是左右边界都空出来了16 解决办法: 具体原因应该是iOS8 之后UIView又多了个属性:l ...
- (1)Two Sum--求数组中相加为指定值的两个数
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- Extjs 树节点操作常用属性
tree: 树 node:节点 1.全部展开 tree.expandAll(); 2.全部收缩 tree.collapseAll(); 3.得到父节点 node.parentNode 4. ...
- redhat 安装hadoop1.2.1伪分布式
完整安装过程参考:http://www.cnblogs.com/shishanyuan/p/4147580.html 一.环境准备 1.安装linux.jdk 2.下载hadoop2. ...