首页 > 其他分享 >「杂题乱刷2」CF402D Upgrading Array

「杂题乱刷2」CF402D Upgrading Array

时间:2024-07-05 23:09:00浏览次数:20  
标签:gcd forl ll CF402D Upgrading long cout Array define

题目链接

CF402D Upgrading Array (luogu)

CF402D Upgrading Array

解题思路

首先有一个很显然的结论,就是你一旦在第 \(i\) 个位置上做了一次操作后,那么你之后所有在第 \(j(i \le j)\) 个位置做的操作都无效了,因为此时前缀的公因数为 \(1\) 了。

因此有个很显然的结论就是操作需要从后往前做

然后我们只做对有贡献的操作,有贡献指的是做了这个操作后答案大于等于原本的答案或者说等价于这个操作的区间的最大公因数的价值小于 \(0\)。

于是根据以上方式进行操作即可,时间复杂度 \(O(n \sqrt v )\)。

不过值得注意的是,本题比较卡常,记得在各个地方加上记忆化以加快程序效率。

参考代码

点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define map unordered_map
#define re register
#define ll long long
#define forl(i,a,b) for(re ll i=a;i<=b;i++)
#define forr(i,a,b) for(re ll i=a;i>=b;i--)
#define forll(i,a,b,c) for(re ll i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(re ll i=a;i>=b;i-=c)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define mid ((l+r)>>1)
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) (x&-x)
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define db long double
#define ull unsigned long long
#define lcm(x,y) x/__gcd(x,y)*y
#define Sum(x,y) 1ll*(x+y)*(y-x+1)/2
#define aty cout<<"Yes\n";
#define atn cout<<"No\n";
#define cfy cout<<"YES\n";
#define cfn cout<<"NO\n";
#define xxy cout<<"yes\n";
#define xxn cout<<"no\n";
#define printcf(x) x?cout<<"YES\n":cout<<"NO\n";
#define printat(x) x?cout<<"Yes\n":cout<<"No\n";
#define printxx(x) x?cout<<"yes\n":cout<<"no\n";
#define maxqueue priority_queue<ll>
#define minqueue priority_queue<ll,vector<ll>,greater<ll>>
ll t;
ll n,m,ans=-1e18;
map<ll,ll>mp,mp2;
ll a[100010],b[100010],lst[100010];
ll lst2;
ll gcd[100010];
map<ll,ll>mp3;
bool pdzs(ll a)
{
	if(mp3[a])
		return mp3[a]-1;
    if(a==1)
        return 0;
    if(a==2)
        return 1;
    for(ll i=2;i<=sqrt(a);i++)
        if(a%i==0)
        {
			mp3[a]=1;
		    return 0;
   		}
   	mp3[a]=2;
    return 1;
}
bool pd[1000010];
long long A[2000010],K;
void work(ll n)
{
	for(ll i=2;i<=n;i++)
	{
		if(pd[i]==0)
			A[++K]=i;
		for(ll j=1;j<=K && i*A[j]<=n;j++)
		{
			pd[i*A[j]]=1;
			if(i%A[j]==0)
				break;
		}
	}
}
ll f(ll x)
{
	ll num=x;
	if(mp2[num])
		return mp2[num];
	ll an=0;
	if(pdzs(num))
		return mp[num]==0?1:-1;
	forl(i,1,K)
		if(x%A[i]==0)
		{
			ll pd=mp[A[i]]==0?1:-1;
			while(x%A[i]==0)
				x/=A[i],an+=pd;
			if(x==1)
				return mp2[num]=an;
		}
	if(pdzs(x))
		return mp2[num]=an+(mp[x]==0?1:-1);
	return mp2[num]=an;
}
void solve()
{
	lst2=1;
	cin>>n>>m;
	forl(i,1,n)
		cin>>a[i],lst[i]=a[i];
	gcd[1]=a[1];
	forl(i,2,n)
		gcd[i]=__gcd(gcd[i-1],a[i]);
	forl(i,1,m)
		cin>>b[i],mp[b[i]]=1;
	ll sum=0;
	forl(i,1,n)
		sum+=f(a[i]);
//	cout<<sum<<endl;
	forr(i,n,1)
		if(f(gcd[i]/lst2)<0)
			sum+=f(gcd[i]/lst2)*-1*i,lst2=gcd[i];
	cout<<max(ans,sum)<<endl;
}
int main()
{
	work(40000);
	IOS;
	t=1;
 //	cin>>t;
	while(t--)
		solve();
	QwQ;
}

标签:gcd,forl,ll,CF402D,Upgrading,long,cout,Array,define
From: https://www.cnblogs.com/wangmarui/p/18286751

相关文章

  • Javascript中Object、Array、String
    Object在JavaScript中,Object 类型是一种复杂的数据类型,用于存储键值对集合。它提供了多种方法来操作这些键值对,以及执行其他常见的操作。这里,我列出了一些 Object 类型的常见方法或特性,它们在日常编程中非常有用:属性访问点符号(.):如果属性名是一个有效的标识符(例如,没有空格......
  • 「杂题乱刷2」CF1454F Array Partition
    题目链接CF1454FArrayPartition解题思路我们发现显然第一个和第三个区间的值区间随着长度的增大而增大。于是我们就可以枚举第一个区间的右端点位置,然后现在问题就转化成了找到一个断点来确定第二,三个区间的长度,由于前文提到的第三个区间的值区间随着长度的增大而增大,于是我......
  • ArrayList底层结构和源码分析
    //无参构造器创建ArrayList对象//ArrayListlist=newArrayList();//断点1ArrayListlist=newArrayList(8);//断点2//添加1-10数据for(inti=0;i<=10;i++){list.add(i);}//添......
  • ArrayList和LinkedList的比较
    基本比较 底层结构增删效率改查效率ArrayList可变数组较低;数组扩容较高LinkedList双向链表较高,通过链表追加较低选择使用若改查操作多选择ArrayList增删操作多选择LinkedList通常程序中大部分操作为查询,因此通常使用ArrayList根据需求,效率优先的原则......
  • torch.tensor、numpy.array、list三者之间互相转换
    torch.tensor、numpy.array、list三者之间互相转换1.1list转numpyndarray=np.array(list)1.2numpy转listlist=ndarray.tolist()2.1list转torch.Tensortensor=torch.Tensor(list)2.2torch.Tensor转list先转numpy,后转listlist=tensor.numpy().tolist(......
  • 优化代码以避免 ESLint 的 array-callback-return 错误,需要确保 map 函数中的每个回调
    要优化这段代码以避免ESLint的array-callback-return错误,你需要确保map函数中的每个回调都返回一个值或者通过早期返回来处理。在你的例子中,你只在满足特定条件时返回元素。ESLint的规则要求每个数组迭代都应有返回值,否则它会抛出警告。一种解决方案是在不满足任何条件......
  • centos系统构建安装john导致的编译问题error: size of array element is not a multip
    blake2.h:112:5:error:sizeofarrayelementisnotamultipleofitsalignment112|blake2b_stateS[4][1];|^~~~~~~~~~~~~blake2.h:113:5:error:sizeofarrayelementisnotamultipleofitsalignment113|blake2b_stateR[1];......
  • Beautiful Array(Round 954)
    #include<bits/stdc++.h>#defineendl'\n'usingll=longlong;typedefunsignedlonglongull;usingnamespacestd;voidGordenGhost();signedmain(){#ifdefGordenfreopen("in.txt","rt",stdin);freopen......
  • LeetCode 1013. Partition Array Into Three Parts With Equal Sum
    原题链接在这里:https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/description/题目:Givenanarrayofintegers arr,return true ifwecanpartitionthearrayintothree non-empty partswithequalsums.Formally,wecanpartition......
  • The this Pointer (this 指针) and An Array of Objects (对象数组)
    ThethisPointer[this指针]andAnArrayofObjects[对象数组]1.The`this`Pointer(`this`指针)2.AnArrayofObjects(对象数组)References1.ThethisPointer(this指针)classStock{private: doubletotal_val_; ...public: double......