首页 > 其他分享 >Nikita and LCM题解

Nikita and LCM题解

时间:2024-05-30 21:24:31浏览次数:16  
标签:include int 题解 Nikita long 数组 ans LCM define

题目大意

给你一个数组 $ a $,令 $ t $ 为 $ a $ 的子序列且 $ t $ 中所有数的最小公倍数 $ \notin a $ 求 $ t $ 数组中最多有多少个数。

思路

非常明显这是一道数学题,对于数组 $ a $ 我们首先从小到大拍一个序,然后我们可以求出 $ a $ 中所有数的最大公倍数,如果这个公倍数 $ \not= a_n $ 所以答案为 $ n $,否者可以证明 $ a_1,a_2,\dots,a_n $ 都为 $ a_n $ 的因子。

我们可以枚举 $ a_n $ 的每一个因子 $ d $ 如果 $ d $ 没有在 $ a $ 数组中出现过,那么我们就去 Check $ d $,而 Check 的内容为判断 $ a $ 数组里面的数是否能组成 $ d $,思路大致就是这样,小细节就看代码了。

Code:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string.h>
#include <queue>
#include <stdio.h>
#include <map>
#define ll long long
#define prt printf
#define int long long
#define sc(ppt) scanf("%lld" , &ppt)
using namespace std;

int t , n , ans = 0 , a[2005];
map<int , int> G;

int lcm(int a , int b){
	return a * b / __gcd(a , b);
}

void check(int d){
	int cnt = 0 , res = 1;
	for(int i=1 ; i<=n ; i++){
		if(a[i] == a[n]) break;
		if(d % a[i] == 0){
			++ cnt;
			res = lcm(res , a[i]);
		}
	}
	if(res == d) ans = max(ans , cnt); // 打一个擂台 
}

void solve(){
	int res = 1;
	for(int i=1 ; i<=n ; i++){
		res = lcm(res , a[i]);
		if(res > a[n]){
			ans = n ;
			break;
		}
	}
	if(ans != n && !G.count(res)) ans = n;
	for(int i=1 ; i*i<=a[n] ; i++){
		if(a[n] % i != 0) continue;
		if(!G.count(i)){ check(i);}
		if(i * i != a[n] && !G.count(a[n] / i)) check(a[n] / i);
	}
}	

signed main(){
	sc(t) ; while(t --){
		ans = 0 ; G.clear() ;	
		sc(n) ; for(int i=1 ; i<=n ; i++){
			sc(a[i]) ; G[a[i]] =  1;
		}
		sort(a + 1 , a + n + 1) ; solve() ;
		prt("%lld\n" , ans); 
	}
	return 0;
}

标签:include,int,题解,Nikita,long,数组,ans,LCM,define
From: https://www.cnblogs.com/CaoSheng-blog/p/18223238

相关文章

  • MITIT 2024 Spring Invitational Qualification 简要题解
    这个比赛没有找到题解,有点难绷,所以来写篇。(实际上是无聊时写的就是了)题面:https://codeforces.com/gym/105125/。目测难度是绿绿黄紫紫。A有点诈骗。其实策略是只保留\(\le3\)个数,然后就随便维护一下。\(O(n\logn)\)。Code#include<bits/stdc++.h>usingnamespaces......
  • Gym-100520A Andrew Stankevich Contest 45 A 题解
    AnalogousSetsGym-100520ASol1.集合生成函数将可重集合\(M\)映射为生成函数:\[F(M)=\sum_{m\inM}(\#m)\cdotx^m\]如果\(M\)的元素在\(\mathbbN\)上取值,那么,\(F(M)\)是多项式。2.\(\theta\)算子\[\theta(F)=x\cdotF'\]其中\(F'=\frac{dF}{dx}\)......
  • Springboot报class path resource [xxxxx.json] cannot be resolved to URL because i
    当Springboot解析resources文件下的json文件时,在本地环境好用,部署到服务器上找不到文件内容报错classpathresource[xxxxx.json]cannotberesolvedtoURLbecauseitdosenotexist问题排查(1)pom.xml文件配置<build><resources><resource><d......
  • 洛谷 P8725 [蓝桥杯 2020 省 AB3] 画中漂流 的题解
    题目大意传送门思路考虑使用时空复杂度为O(tm)O(tm)......
  • 洛谷 P8614 [蓝桥杯 2014 省 A] 波动数列 的题解
    题目大意求满足和为sss且ti=......
  • 列队春游|概率期望|题解
    题面解析前言,此处所述为\(O(n^2)\)算法,暂时未推出\(O(n)\)的算法,后续可能会更新。题意非常明白,不多赘述。我们去考虑单个位置的概率,维护每个人放在该位置对该位置期望的贡献。以这个思想作为切入点,我们思考,对于一个序列来说,如果它的长度是定的。设总人数为n,当前我们考虑......
  • 【23NOIP提高组】题解
    T1:词典 #include<bits/stdc++.h>usingnamespacestd;inlineintread(){ intx=0,f=1;charch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9�......
  • P10528 [XJTUPC2024] 崩坏:星穹铁道 题解
    头图无语了,猜猜WA哪了不要真头图崩坏:星穹铁道题链这么简单做不对不许玩崩铁!题目大意给你行动的总次数\(n\)和初始战技点数量\(k\),以及编队里四名角色的行动类型,求不同行动方式的方案数。类型如下:思路先考虑dp,分角色类型讨论。设\(f_{i,k}\)表示第\(i......
  • DockerDesktop中启动jenkins容器时提示:Can not write to /var/jenkins_home/copy_ref
    场景Windows10(家庭版)中DockerDesktop(docker)的配置、安装、修改镜像源、使用:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/139264096按照以上教程搭建之后想要运行jenkins容器,所以执行如下指令dockerrun-d--namejenkins-p18088:8080-v/jenkinshome:......
  • CCF-CSP真题《202403-1 词频统计》思路+python满分题解
    哇q(≧▽≦q),第一次写博客,请大家多多关照○| ̄|_ 看到没啥人提供202403的第一题解题思路及python代码,刚好写完,心血来潮想分享解题思路,就写下了这篇博客,有其他的编码版本,欢迎大家一起探讨呀(虽然我是算法菜鸟┗(T﹏T)┛,但有问题,我会尽力回答的!!!)好了废话不多说,上解题思路!大概想了......