C - Aladdin and the Flying Carpet (质因子分解,因子个数)

C - Aladdin and the Flying Carpet

题目链接:https://vjudge.net/problem/LightOJ-1341#author=2018112767

题目大意:

给一对数字 a,b 。其中,a表示一个矩形的面积,想知道有多少种整数的边的组合可以组成面积为a的矩形,而且要求矩形的最短的边不得小于b。

解题思路:

先算出a的因子数,然后由公式因子个数num=(q1+1)*(q2+1)......*(qn+1).求出num后再除以2,得到的就是所有面积等于a的整数对的个数,因为要小于b,b的范围较小(题中未给。。。),所以遍历1到b,num减去最小的边小于b的矩形个数,得到的就是答案。

代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const ll N=1e6+7;
 5 ll isprime[N],prime[N];
 6 ll m,T;
 7 void primes(){
 8     memset(isprime,0,sizeof(isprime));
 9     m=0;
10     for(ll i=2;i<=N;i++){
11         if(isprime[i]==0){
12             isprime[i]=i;
13             prime[++m]=i;
14         }
15         for(ll j=1;j<=m;j++){
16             if(prime[j]>isprime[i] ||prime[j]*i>N )  break;
17             isprime[i*prime[j]]=prime[j];
18         }
19     }
20 }
21
22 int main(){
23     ll n,test=0;
24     primes();
25     cin>>T;
26     while(T--){
27         ll ans=1,num,a,b;
28         scanf("%lld%lld",&a,&b);
29         if(b*b>=a){
30             printf("Case %lld: 0\n",++test);
31             continue;
32         }
33         n=a;
34         for(ll i=1;i<=m&&prime[i]*prime[i]<=n;i++){
35             num=0;
36             while(n%prime[i]==0){
37                 num++;
38                 n/=prime[i];
39             }
40             ans*=(num+1);
41         }
42         if(n!=1){
43             ans*=2;
44         }
45         ans/=2;
46         for(ll i=1;i<b;i++){
47             if(a%i==0){
48                 ans--;
49             }
50         }
51         printf("Case %lld: %lld\n",++test,ans);
52     }
53     return 0;
54 }

原文地址:https://www.cnblogs.com/meanttobe/p/12396761.html

时间: 2024-11-09 11:24:12

C - Aladdin and the Flying Carpet (质因子分解,因子个数)的相关文章

LightOJ 1341 - Aladdin and the Flying Carpet 基本因子分解

http://www.lightoj.com/volume_showproblem.php?problem=1341 题意:给你长方形的面积a,边最小为b,问有几种情况. 思路:对a进行素因子分解,再乘法原理算一下,最后减去小于b的因子的情况即可. /** @Date : 2016-12-01-19.04 * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : */ #include<b

LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1341 Description It's said that Aladdin had to solve seven

LightOJ Aladdin and the Flying Carpet 1341【算数基本定理+几何】

1341 - Aladdin and the Flying Carpet PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 MB It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned

LightOJ1341 Aladdin and the Flying Carpet 约数相关问题

LightOJ1341 Aladdin and the Flying Carpet 标签 约数相关问题 前言 我的csdn和博客园是同步的,欢迎来访danzh-博客园~ 简明题意 给定n,b,求n的>=b的约数的对数.(n<=1e12) 思路 n的约数对数=\(d(n)/2\),这个应该是很显然的.如果n是完全平方数那么这个式子不对,但是题目说了只用找矩形而不用找正方形,因此不需要考虑n是完全平方数的情况. n的约数对数求出来了,但是题目要求>=b的约数对数,怎么搞呢?这里我也想了半天没

Light OJ 1341 Aladdin and the Flying Carpet

It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised hi

LightOJ 1341 - Aladdin and the Flying Carpet(算术基本定理啊)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter

LightOJ 1341(Aladdin and the Flying Carpet )算术基本定理

It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised hi

E - Aladdin and the Flying Carpet

It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised hi

LOJ 1341 Aladdin and the Flying Carpet(质因子分解)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 题意:给两个数a,b,求满足c * d = a且c>=b且d>=b的c, d二元组对数,(c, d)和(d, c)属于同一种情况. 思路:根据唯一分解定理,先将a唯一分解,则a的所有正约数的个数为num = (1 + a1) * (1 + a2) *...* (1 + ai),这里的ai是素因子的指数.现在我们知道了a的因子个数为num,假设因子从小到大排列为 X1,X2,.