【CF1823C】Strongly Composite【数论】

在这里插入图片描述

题意

构造b数组,元素乘积等于a数组元素乘积,且所有元素都要是“强合数”,求b数组长度k的最大值。

分析

看看样例,想一个数怎样才能成为强合数。

发现三个不同质数的乘积或者一个质数的平方一定是一个强合数。为了让数组更长,优先选择质数的平方。

于是就可以把a数组的乘积进行质因数分解,然后去除每个质数的平方,最后三个三个组合就行。

但是,直接乘起来会爆掉,所以碰到一个 a i a_i ai 就分解。

但是我还是没过。。。超时了。。

有一个重要处理:在线性筛的时候记录每个数的最小质因子,分解质因数的时候就从最小质因子开始,直接可以跳到每个数的质因子,不用一个个枚举。

上代码

![#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;

int t,n;
int p[1000100],tot,kk[1000100],f[10001000];
bool v[10000010];
ll cnt,ans;

void pre()
{
	for(int i=2;i<=10000000;i++)
	{
		if(!v[i]) p[++tot]=i,f[i]=tot;
		for(int j=1;j<=tot&&i*p[j]<=10000000;j++)
		{
			v[i*p[j]]=1;
			f[i*p[j]]=j;//记录
			if(i%p[j]==0) break;
		}
	}
}

int main()
{
	pre();
	cin>>t;
	while(t--)
	{
		scanf("%d",&n);
		for(register int i=1;i<=n;i++)
		{
			int x;
			scanf("%d",&x);
			int j=f[x];
			while(x>1)
			{
				j=f[x];
				while(x%p[j]==0)
				{
					x/=p[j];
					kk[j]++;
					cnt++;
				}
			}
		}
		for(register int i=1;i<=tot;i++)
		{
			ans+=(kk[i]/2);
			cnt-=2*(kk[i]/2);
		}
		ans+=cnt/3;
		printf("%lld\n",ans);
		ans=0;
		cnt=0;
		memset(kk,0,sizeof(kk));
	}
	return 0;
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The strongly implicit procedure (SIP) is a numerical method used for solving nonlinear equations iteratively. In MATLAB, the SIP algorithm can be implemented using the fsolve function, which is a built-in solver for nonlinear equations. The basic idea behind the SIP algorithm is to solve a system of nonlinear equations by forming a linear system and solving it iteratively. At each iteration, the linear system is solved using a Newton-Raphson method, which involves computing the Jacobian matrix and updating the solution estimate. The fsolve function in MATLAB takes as input the nonlinear function to be solved, the initial guess for the solution, and optional parameters such as the tolerance and maximum number of iterations. It returns the solution estimate and the convergence information. Here is an example MATLAB code for solving a system of nonlinear equations using the SIP algorithm: ```matlab % Define the nonlinear function f = @(x) [x(1)^2 + x(2)^2 - 1; x(1) - x(2)^2]; % Set the initial guess x0 = [1; 1]; % Set the options for fsolve options = optimoptions('fsolve', 'Display', 'iter', 'FunctionTolerance', 1e-6); % Solve the system of nonlinear equations [x,~,exitflag] = fsolve(f, x0, options); % Print the solution and convergence information disp(['Solution: x = ', num2str(x')]); disp(['Exit flag: ', num2str(exitflag)]); ``` In this example, the nonlinear function is defined as a function handle using anonymous function notation. The initial guess is set to [1; 1], and the options for fsolve are set to display the iteration information and a function tolerance of 1e-6. The fsolve function is called with the function handle, initial guess, and options as input, and it returns the solution estimate, convergence information, and exit flag. The solution estimate is printed to the console along with the exit flag.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值