G - Pairs Forming LCM LightOJ - 1236 (质因子分解)
2024-08-24 21:20:08
题解:这道题要从n的角度来考虑i和j。
n可以表示为n=a1^p1*a2^p2*a3^p3......。n=lcm(i,j),那么质因子a1^p1,a1可以在i或者j中,并且p1=max(a1i,a1j)即pi为i中ai和j中ai的最大值。假设a1在i中,对于质因子a1,b中有[0,p1],一共有p1+1中选择。
a1在j中同理,a也有p1+1中选择。所以一共有2(p1+1)-1种情况。为什么要减去1呢?如果i中有p1个a1,b中也有p1个a1,这种情况我们算了两次。所以要减去1。然后累乘。这样算出来我们可以得到(i,j)和(j,i)的总数目。
所以应该除以2,如果i和j相等呢?我们除以2,把(i=n,j=n)这种情况除去了,所以应该再加1.
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const ll N = 1E6 + ;
const ll MAX = 1e7;
ll pre[N];
bool prime[MAX+];
ll pos = ;
void inint(){
prime[] = ;
for (ll i = ; i <= MAX; i++) {
if (!prime[i]) pre[++pos] = i;
for (ll j = ; j <= pos && i * pre[j] <= MAX; j++) {
prime[i * pre[j]] = ;
if (i % pre[j] == ) break;
}
}
}
void solve(ll time){
ll n;
scanf("%lld",&n);
ll n1 = n;
ll ans=;
for (ll i = ; i <= pos; i++) {
if (pre[i] > n1) break;
if (n1 % pre[i] == ){
ll p=;
while (n1 % pre[i] == ) {
n1 /= pre[i];p++;
}
ans*=((ll)+*p);
}
}
if (n1 != ) ans*=(ll);
printf("Case %d: %lld\n",time,ans/+(ll));
}
int main(){
inint();
int t;
scanf("%d",&t);
for(int i=;i<=t;i++) solve(i);
return ;
}
最新文章
- [bzoj2653][middle] (二分 + 主席树)
- MyBatis:统计数量
- 网易云音乐PC端刷曲快捷键
- 不能从const char *转换为LPCWSTR
- 【温故知新】C#委托delegate
- Codevs No.1163 访问艺术馆
- Illegal pattern character &#39;i&#39; 解决问题
- 20160314 Request 和Response
- django开发框架之jumpserver
- linux 开启防火墙操作
- 【Storm】Storm实战之频繁二项集挖掘
- cocos2d 从v1.x升级到v2.x需要注意的几个地方
- MySQL 基础知识梳理学习(一)----系统数据库
- centos下 telnet访问百度
- PLSQL安装、PLSQL汉化、激活
- 记一次oracle数据库复制过程
- select实现简单TCP通信(ubuntu 18.04)
- Slurm任务调度系统部署和测试(源码)(1)
- js实现文字超出部分用省略号代替实例代码
- CSS3 转换
热门文章
- CF1324F Maximum White Subtree 题解
- 报错:Error instantiating class com.liwen.mybatis.bean.Employee with invalid types () or values ().
- Building Applications with Force.com and VisualForce(Dev401)(十四):Implementing Business Processes:Auditing Processes
- ArrayList的传值问题
- prometheus远程写参数优化
- 在Fedora中安装OpenCV-Python | 二
- NeurIPS审稿引发吐槽大会,落选者把荒唐意见怼了个遍:“我谢谢你们了”
- coding++:SpringBoot-事务注解详解
- git本地库中配置多个sshkey
- 1078 Hashing (25分)