首页 > 其他分享 >CF992 Codeforces Round 489 (Div. 2)

CF992 Codeforces Round 489 (Div. 2)

时间:2023-09-27 22:24:55浏览次数:36  
标签:return int res Codeforces long CF992 Div include MOD

CF992A Nastya and an Array

答案为非零数的个数。


#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
const int N=100005;
int n;
int a[N];
map<int,int>cnt;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
		if(a[i]!=0) cnt[a[i]]++;
	int k=cnt.size();
	printf("%d",k);
	return 0;
}

CF992B Nastya Studies Informatics

\[\operatorname{lcm}(a,b)=\frac{ab}{\gcd(a,b)}\Leftrightarrow y=\frac{ab}{x}\Leftrightarrow xy=ab \]

令 \(a=ix,b=jx\),则 \(xy=ixjx\Leftrightarrow ij=\frac{y}{x}\),枚举 \(k=\frac{y}{x}\) 的因数判断即可。


#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int l,r,x,y;
int main()
{
	scanf("%d%d%d%d",&l,&r,&x,&y);
	if(y%x!=0)
	{
		printf("0");
		return 0;
	}
	int k=y/x;
	int ans=0;
	auto check=[=](int i,int j)
	{
		if(__gcd(i,j)!=1) return false;
		return l<=1LL*x*i&&1LL*x*i<=r&&l<=1LL*x*j&&1LL*x*j<=r&&1LL*x*i*x*j==1LL*x*y;
	};
	for(int i=1;i<=sqrt(k);i++)
		if(k%i==0)
		{
			if(check(i,k/i)) ans++;
			if(i*i!=k&&check(k/i,i)) ans++;
		}
	printf("%d",ans);
	return 0;
}

CF992C Nastya and a Wardrobe

考虑第 \(i\) 个月对第 \(k+1\) 个月的贡献,即为 \(2^{k-i}\)。所有月份的负的贡献为 \(\sum\limits_{i=1}^k2^{k-i}=2^k-1\)。

所有的总和为 \(x\cdot 2^{k+1}-(2^k-1)\)。


#include<iostream>
#include<cstdio>
using namespace std;
const int MOD=1000000007;
long long n,k;
long long ksm(long long a,long long b)
{
	long long res=1;
	while(b)
	{
		if(b&1) res=res*a%MOD;
		a=a*a%MOD,b>>=1;
	}
	return res;
}
int main()
{
	scanf("%lld%lld",&n,&k);
	if(n==0)
	{
		printf("0");
		return 0;
	}
	printf("%lld",(n%MOD*ksm(2,k+1)%MOD-ksm(2,k)+1+MOD)%MOD);
	return 0;
}

CF992D Nastya and a Game

可以发现,最后的串中非 \(1\) 的数的个数不会很多,可以将所有连续的 \(1\) 缩成一段,然后枚举左端点暴力找右端点即可。


#include<iostream>
#include<cstdio>
using namespace std;
const int N=200005;
const long long INF=2e18;
int n,k;
int a[N];
int ad[N],mu[N];
int m;
long long mul(long long a,long long b)
{
	__int128 c=(__int128)a*b;
	if(c>INF) return INF+1;
	else return c;
}
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1,j=1;i<=n;i=j)
	{
		while(j<=n&&a[i]==a[j]) j++;
		if(a[i]==1) m++,ad[m]=j-i,mu[m]=1;
		else
		{
			for(int k=i;k<j;k++)
				m++,ad[m]=mu[m]=a[k];
		}
	}
	long long ans=0;
	for(int i=1;i<=m;i++)
	{
		long long fac=1,sum=0;
		for(int j=i;j<=m;j++)
		{
			fac=mul(fac,mu[j]),sum+=ad[j];
			if(fac>INF) break;
			if(fac%k!=0) continue;
			if(fac/k==sum) ans++;
			else
			{
				long long num=fac/k;
				int t=0,c=0;
				if(mu[i]==1) t+=ad[i]-1,c+=ad[i];
				if(mu[j]==1) t+=ad[j]-1,c+=ad[j];
				if(sum-t<=num&&num<=sum)
				{
					if(mu[i]==1&&mu[j]==1)
					{
						int s=num-(sum-c);
						int l=max(ad[i]-(s-1)+1,1),r=min(ad[i],ad[i]-1-(s-1-ad[j])+1);
						ans+=r-l+1;
					}
					else ans++;
				}
			}
		}
	}
	printf("%lld",ans);
	return 0;
}

CF992E Nastya and King-Shamans

可以发现,如果 \(a_i\) 全为 \(0\),\(a_i> s_{i-1}\) 的位置不会超过 $\log $ 个。线段树维护下每个区间 \(a_i-s_{i-1}\) 的最大值,如果最大值 \(< 0\) 就跳过,这样时间复杂度大概是两只 \(\log\) 的?


#include<iostream>
#include<cstdio>
using namespace std;
const int N=200005;
int n,Q;
long long a[N];
long long s[N];
struct Node
{
	int l,r;
	long long Max,tag;
}tree[N<<2];
void push_up(int i)
{
	tree[i].Max=max(tree[i*2].Max,tree[i*2+1].Max);
	return;
}
void add(int i,long long v)
{
	tree[i].Max+=v;
	tree[i].tag+=v;
	return;
}
void push_down(int i)
{
	if(!tree[i].tag) return;
	add(i*2,tree[i].tag);
	add(i*2+1,tree[i].tag);
	tree[i].tag=0;
	return;
}
void build(int i,int l,int r)
{
	tree[i].l=l,tree[i].r=r;
	if(l==r)
	{
		tree[i].Max=a[l]-s[l-1];
		return;
	}
	int mid=(l+r)/2;
	build(i*2,l,mid);
	build(i*2+1,mid+1,r);
	push_up(i);
	return;
}
void modify(int i,int l,int r,long long v)
{
	if(l>r) return;
	if(l<=tree[i].l&&tree[i].r<=r) return add(i,v);
	push_down(i);
	if(l<=tree[i*2].r) modify(i*2,l,r,v);
	if(r>=tree[i*2+1].l) modify(i*2+1,l,r,v);
	push_up(i);
	return;
}
void query(int i,int &res)
{
	if(res!=-1) return;
	if(tree[i].Max<0) return;
	if(tree[i].l==tree[i].r)
	{
		if(tree[i].Max==0) res=tree[i].l;
		return;
	}
	push_down(i);
	query(i*2,res);
	query(i*2+1,res);
	return;
}
int main()
{
	scanf("%d%d",&n,&Q);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)
		s[i]=s[i-1]+a[i];
	build(1,1,n);
	while(Q--)
	{
		int p,x;
		scanf("%d%d",&p,&x);
		modify(1,p+1,n,a[p]-x);
		modify(1,p,p,x-a[p]);
		a[p]=x;
		int ans=-1;
		query(1,ans);
		printf("%d\n",ans);
	}
	return 0;
}

标签:return,int,res,Codeforces,long,CF992,Div,include,MOD
From: https://www.cnblogs.com/zhou-jk/p/17734492.html

相关文章

  • CF1008 Codeforces Round 497 (Div. 2)
    CF1008ARomaji直接模拟。#include<iostream>#include<cstdio>#include<cstring>usingnamespacestd;constintN=105;intn;chars[N];intmain(){ scanf("%s",s+1); n=strlen(s+1); for(inti=1;i<=n;i++) if(s[i]!='a......
  • CF996 Codeforces Round 492 (Div. 2) [Thanks, uDebug!]
    CF996AHittheLottery直接贪心尽可能的分配到\(k_5\),剩下的依次分配给\(k_4,k_3,k_2,k_1\)。#include<iostream>#include<cstdio>usingnamespacestd;intn;intk[6];intmain(){ scanf("%d",&n); k[5]=n/100,n%=100; k[4]=n/20,n%=20; k[3]=n/1......
  • CF1011 Codeforces Round 499 (Div. 2)
    CF1011AStages每次记下上一个选的位置,贪心能填就填。#include<iostream>#include<cstdio>usingnamespacestd;constintN=55;intn,k;chars[N];intcnt[27];intmain(){ scanf("%d%d",&n,&k); scanf("%s",s+1); for(inti=1;i<=n......
  • CF1020 Codeforces Round 503 (by SIS, Div. 2)
    CF1020ANewBuildingforSIS分类讨论\(a,b\)两个端点的几种情况就好了,特判\(t_a=t_b\)的情况。#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>usingnamespacestd;intn,h,a,b,k;voidsolve(){ intta,fa,tb,fb; scanf(&qu......
  • CF1036 Educational Codeforces Round 50 (Rated for Div. 2)
    CF1036AFunctionHeight答案为\(\lceil\frac{k}{n}\rceil\)。#include<iostream>#include<cstdio>usingnamespacestd;longlongn,k;intmain(){ scanf("%lld%lld",&n,&k); printf("%lld",(k+n-1)/n); return0;}......
  • CF1079 Codeforces Round 522 (Div. 2, based on Technocup 2019 Elimination Round 3
    CF1079AKitchenUtensils令\(c_i\)表示餐具\(i\)出现的数量,最小的餐具套数为\(t=\lceil\frac{\max\{c_i\}}{k}\rceil\),按照这个计算就好了。#include<iostream>#include<cstdio>#include<algorithm>usingnamespacestd;constintN=105;intn,k;inta[N]......
  • CF1072 Codeforces Round 517 (Div. 2, based on Technocup 2019 Elimination Round 2
    CF1072AGoldenPlate第\(i\)个矩形的周长为\(2(w-4(i-1))+2(h-4(i-1))-4\),枚举\(i\)求和。#include<iostream>#include<cstdio>usingnamespacestd;intn,m,k;intmain(){ scanf("%d%d%d",&n,&m,&k); intans=0; for(i......
  • CF1162 Codeforces Round 557 (Div. 2) [based on Forethought Future Cup - Final Ro
    CF1162AZoningRestrictionsAgain每个位置越高越好,暴力模拟即可。#include<iostream>#include<cstdio>usingnamespacestd;constintN=55;intn,h,m;inta[N];intmain(){ scanf("%d%d%d",&n,&h,&m); for(inti=1;i<=n;i++) a[i]=h;......
  • Codeforces Round 900 (Div. 3) - A B C D E
    目录A.HowMuchDoesDaytonaCost?B.AleksaandStackA.HowMuchDoesDaytonaCost?判断数k包不包含在数组里面即可B.AleksaandStack选定初始数为2,3,后面的遍历法二:全奇数即可,因为奇数+奇数=偶数,奇数x3=奇数,......
  • [题解] Codeforces Round 900(Div.3) E~F
    CodeforcesRound900(Div.3)E~FE.Iva&Pav因为按位与的结果不会随着越多数字的增加而增加,因此我们可以利用这个性质二分出右端点,只需要一个可以查询区间的数据结构即可。或者是按位考虑第\(i\)个数字的第\(k\)位,后缀最近的\(0\)的位置,按位考虑也可以。但是这题使用二分......