题目链接:https://cn.vjudge.net/problem/LightOJ-1236

题意

给一整数n,求有多少对a和b(a<=b),使lcm(a, b)=n

注意数据范围n<=10^14

思路

唯一分解定理

要注意的是条件a<=b,这就是说,在不要求大小关系的情况下

ans包括a<b,a>b和a==b的情形,最终答案就是(ans+1)/2

注意数据范围,求因数时使用1e7的素数即可,剩余的未被分解的数一定是大素数

首先求一下素数加速求因数,其次注意prime*prime<=n是另一优化

提交过程

TLE1 没注意数据范围,用了没有优化的getFactors
WA*n 模版有问题,一直在尝试优化
WA 注意ans=factors[i][0]2+1;
TLE2 第二个prime*prime<=n的优化没做
WA 注意long long范围
AC

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e7+20;
int factors[100][2], fsize, primes[maxn/10], psize;
bool isprime[maxn];
void initPrimes(void){
memset(isprime, true, sizeof(isprime));
isprime[0]=isprime[1]=false;
for (int i=2; i<=maxn; i++){
if(isprime[i]) primes[psize++]=i;
for (int j=0; j<psize && i*primes[j]<=maxn; j++){
isprime[primes[j]*i]=false;
if (i%primes[j]==0) break;
}
}
} void getFactors(long long n){
fsize=0;
for (int i=0; i<psize && primes[i]*primes[i]<=n; i++){
if (n%primes[i]==0){
factors[fsize][0]=primes[i];
factors[fsize][1]=0;
while (n%primes[i]==0) factors[fsize][1]++, n/=primes[i];
fsize++;
}
}
if (n>1){
factors[fsize][0]=n;
factors[fsize++][1]=1;
}
} long long solve(long long n){
long long ans=1;
getFactors(n);
for (int i=0; i<fsize; i++)
ans*=factors[i][1]*2+1;
return (ans+1)/2;
} int main(void){
int T, kase=0;
long long n; initPrimes();
scanf("%d", &T);
while (T--){
scanf("%lld", &n);
printf("Case %d: %lld\n", ++kase, solve(n));
} return 0;
}
Time Memory Length Lang Submitted
540ms 14760kB 1096 C++ 2018-07-30 15:45:20

LightOJ-1236 Pairs Forming LCM 唯一分解定理的更多相关文章

  1. LightOJ - 1236 - Pairs Forming LCM(唯一分解定理)

    链接: https://vjudge.net/problem/LightOJ-1236 题意: Find the result of the following code: long long pai ...

  2. LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memor ...

  3. LightOJ 1236 - Pairs Forming LCM(素因子分解)

    B - Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  4. LightOj 1236 Pairs Forming LCM (素数筛选&&唯一分解定理)

    题目大意: 有一个数n,满足lcm(i,j)==n并且i<=j时,(i,j)有多少种情况? 解题思路: n可以表示为:n=p1^x1*p2^x1.....pk^xk. 假设lcm(a,b) == ...

  5. LightOj 1236 - Pairs Forming LCM (分解素因子,LCM )

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意:给你一个数n,求有多少对(i,  j)满足 LCM(i, j) = n, ...

  6. LightOJ 1236 Pairs Forming LCM【整数分解】

    题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1236 题意: 找与n公倍数为n的个数. 分析: ...

  7. LightOJ 1236 Pairs Forming LCM 合数分解

    题意:求所有小于等于n的,x,y&&lcm(x,y)==n的个数 分析:因为n是最小公倍数,所以x,y都是n的因子,而且满足这样的因子必须保证互质,由于n=1e14,所以最多大概在2^ ...

  8. 1236 - Pairs Forming LCM

    1236 - Pairs Forming LCM   Find the result of the following code: long long pairsFormLCM( int n ) {  ...

  9. Light oj 1236 - Pairs Forming LCM (约数的状压思想)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意很好懂,就是让你求lcm(i , j)的i与j的对数. 可以先预处理1e7以 ...

随机推荐

  1. Debian下签名无法验证

    又收集到的新方法 gpg --keyserver  subkeys.pgp.net --recv-keys AED4B06F473041FA  && apt-key add /root ...

  2. Java开发就业形势和面试技巧

    如果从软件编程的就业来讲,如果你现在不懂架构,那么找到一份好工作还是比较难的,但是这里面有两点需要注意: 传统软件公司,这类公司还会使用最为原始的开发技术(SSH),但是这样的传统软件公司的招聘量已经 ...

  3. Java通过UUID随机生成36位、32位唯一识别码(唯一字符串)

    import java.util.UUID; /** * 通过UUID随机生成36位.32位唯一识别码(唯一字符串) * @author [J.H] * */ public class Test { ...

  4. 如何使用阿里云的yum源

    这里需要有网.因为我们需要下载会用到wget [root@localhost ~]# iptables -F[root@localhost ~]# systemctl stop firewalld[r ...

  5. Lost Cows POJ - 2182 二分 + 树状数组

    Code: #include<cstdio> #include<stack> #include<cstring> #include<algorithm> ...

  6. for循环+setTimeout的延迟操作

    例子: for (var i = 0; i < 5; i++) { setTimeout(function () { console.log(i); }, 100) } 上述代码,输出结果显而易 ...

  7. Android开发进度05

    1,今日:目标:完成后台用户的增删改查 2,昨天:完成登录和注册功能 3,收获:熟练了SQLite操作 4,问题:无

  8. webpack实现模块化打包

    webpack打包应用和实现 1)安装webpack $ npm install webpack webpack-cli --save-dev 2)添加配置文件 webpack.config.js 3 ...

  9. 前端通过canvas实现图片压缩

    在一次的项目中,需要用户上传图片,目前市场随便一个手机拍出来的照片都是好几兆,直接上传特别占用带宽,影响用户体验,所以要求对用户上传图片进行压缩后再上传:那么前端怎么实现这个功能呢? 亲测可将4M图片 ...

  10. HDU——T 1498 50 years, 50 colors

    http://acm.hdu.edu.cn/showproblem.php?pid=1498 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...