首页 > 其他分享 >CSSYZ Algorithm Round #2

CSSYZ Algorithm Round #2

时间:2023-06-03 16:01:04浏览次数:34  
标签:return Algorithm int long CSSYZ true Round dp mod

[ABC192F] Potion

分析

设选择的总和为 \(sum\)。
不难发现:
\(x\%k=sum\%k\)。
又因为:
\(ans=(x-sum)/k\)。
不难发现\(sum\)只与\(\%k\)有关,且当\(k\)一定时,\(sum\)越大,\(ans\)越小。
因为\(k\)的值域很小,显然可以对于每一个\(k\),用01背包求解出\(\%k\)意义下的最大\(sum\)。
计算答案即可。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=200;
int dp[N][N][N],n,a[N],x,ans=LLONG_MAX;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>x;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	memset(dp,-63,sizeof(dp));
	for(int i=1;i<=n;i++)
	{
		dp[i][0][0]=0;
		for(int j=1;j<=n;j++)
		{
			for(int k=j;k>=1;k--)
			{
				for(int w=0;w<i;w++)
				{
					if(dp[i][k-1][w]<0)
					continue;
					dp[i][k][(w+a[j])%i]=max(dp[i][k][(w+a[j])%i],dp[i][k-1][w]+a[j]);
				}
			}
		}
		if(dp[i][i][x%i]>=0)
		ans=min(ans,(x-dp[i][i][x%i])/i);
	}
	cout<<ans;
	return 0;
}

[ABC195E] Lucky 7 Battle

分析

博弈论dp模板题。
观察到数据范围较大,加上记忆化。
复杂度线性。

代码

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+100;
int n,f[N][7];
string s1,s2;
bool dfs(int x,int mod)
{
	if(x==n&&mod==0)
		return true;
	else if(x==n)
		return false;
	if(f[x][mod]!=0)
	{
		if(f[x][mod]==1)
		return true;
		else
		return false;
	}
	if(s2[x]=='A')
	{
		if(dfs(x+1,(mod*10)%7)==false||dfs(x+1,(mod*10+s1[x]-'0')%7)==false)
		{
			f[x][mod]=2;
			return false;
		}
		else
		{
			f[x][mod]=1;
			return true;
		}
	}
	else
	{
		if(dfs(x+1,(mod*10)%7)==true||dfs(x+1,(mod*10+s1[x]-'0')%7)==true)
		{
			f[x][mod]=1;
			return true;
		}
		else
		{
			f[x][mod]=2;
			return false;
		}
	}
}
int main()
{
	cin>>n>>s1>>s2;
	if(dfs(0,0)==true)
	cout<<"Takahashi";
	else
	cout<<"Aoki";
	return 0;
}

[ABC199E] Permutation

分析

数据范围很小,考虑状压。
优先考虑全排列的朴素求法。
用当前状态为1表示出现在当前前缀,顺序求解。
又因为本题限制很小,可以直接遍历判断。
因此状压得证。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=200;
int n,m,x[N],y[N],z[N],dp[1<<20];
int get(int x)
{
	int ans=0;
	for(int i=0;i<n;i++)
	{
		if((x&(1<<i))!=0)
		ans++;
	}
	return ans;
}
bool check(int x,int v,int w)
{
	for(int i=0;i<v;i++)
	{
		if((x&(1<<i))!=0)
		w--;
	}
	if(w<0)
	return false;
	else 
	return true;
}
void solve(int x)
{
	for(int i=0;i<n;i++)
	{
		if((x&(1<<i))!=0)
		{
			dp[x]+=dp[x^(1<<i)];
		}
	}
	return;
}
signed main()
{
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>x[i]>>y[i]>>z[i];
	}
	dp[0]=1;
	for(int i=1;i<(1<<n);i++)
	{
		int num=get(i);
		bool fl=0;
		for(int j=1;j<=m;j++)
		{
			if(x[j]==num)
			{
				if(check(i,y[j],z[j])==false)
				{
					fl=1;
					break;
				}
			}
		}
		if(fl==1)
		continue;
		solve(i);
	}
	cout<<dp[(1<<n)-1];
	return 0;
}

[ABC191F] GCD or MIN

分析

观察到GCD和MIN都让答案变小,因此答案应该不大于给定数的最小值。
我们考虑MIN并不会改变现有的数,所以我们只要看GCD够得到哪些数就可以。
我们拆出所有因数,然后选不大于给定数最小值的部分。
对于一个因数,我们判断他能不能被取到:
我们找到所有含有他的数,取他们的GCD,相同就贡献+1。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e3+100;
int n,a[N],l,minn=INT_MAX,ans,maxs=INT_MIN;
vector<int>p;
map<int,vector<int>>mp;
bool check(int x)
{
	int s=mp[x][0];
	for(int i=1;i<mp[x].size();i++)
	{
		s=__gcd(s,mp[x][i]);
	}
	if(s==x)
	return true;
	else
	return false;
}
void pre()
{
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=sqrt(a[i]);j++)
		{
			if(a[i]%j==0)
			{
				if(j<=minn)
				{
					p.push_back(j);
					mp[j].push_back(a[i]);
				}
				if(a[i]/j<=minn)
				{
					p.push_back(a[i]/j);
					mp[a[i]/j].push_back(a[i]); 
				}
			}
		}
	}
	sort(p.begin(),p.end());
	l=unique(p.begin(),p.end())-p.begin()-1;
	return ;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		minn=min(minn,a[i]);
		maxs=max(maxs,a[i]);
	}
	pre();
	for(int i=0;i<=l;i++)
	{
		if(check(p[i])==true)
		ans++;
	}
	cout<<ans;
	return 0;
}

标签:return,Algorithm,int,long,CSSYZ,true,Round,dp,mod
From: https://www.cnblogs.com/iuque/p/17453695.html

相关文章

  • Codeforces 1833E Round Dance
    看到shortestpaths来做的,但是好像没啥关系也没啥难度。首先能知道一个连通块肯定一次就能跳完,所以可以把连通块缩出来。然后有一个性质,记\(cz_i\)为\(i\)连通块内点种通过已知边推出的度数为\(1\)的个数为\(cz_i\),则\(cz_i\bmod2=0\)。记点\(i\)通过已知边推出......
  • android: workaround for slow ‘building workspace’ problem in eclipse
    Whiledevelopingforandroidoneclipse3.6ihadtheproblemthateachtimeisavedafile,eclipseblockedmeseveralsecondswith‘buildingworkspace…’.Similartothese:stackoverflow–android-compilation-is-slow-using-eclipsestackoverflow–android-......
  • pytorch 训练 RuntimeError Unable to find a valid cuDNN algorithm to run convolut
    pytorch训练RuntimeError:UnabletofindavalidcuDNNalgorithmtorunconvolutionpytorch训练RuntimeError:UnabletofindavalidcuDNNalgorithmtorunconvolution#问题描述:python:3.95pytorch:1.10.2pythontrain.py--img640--batch64--epochs600--da......
  • STL algorithm算法
    Functionsin<algorithm>Non-modifyingsequenceoperations:all_ofTestconditiononallelementsinrange(functiontemplate)any_ofTestifanyelementinrangefulfillscondition(functiontemplate)none_ofTestifnoelementsfulfillconditi......
  • SMU Spring 2023 Contest Round 4(第 21 届上海大学程序设计联赛 春季赛)
    A.Antiamunywantstolearnbinarysearch签到题.#include<map>#include<set>#include<cmath>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<climits>#include<......
  • SMU Spring 2023 Trial Contest Round 11
    A.TheTextSplitting题意:给出字符串长度,给出p和q两种切割方式,任选其中一种,把字符串分割输出结果。 题解:先进行判断,p和q是否能整个的分割n,利用p和q的函数关系判断(见代码),再计算有几个p几个q,再进行输出即可voidsolve(){cin>>n>>p>>q;cin>>s;if(p>......
  • surrounded-regions
    Givena2Dboardcontaining'X'and'O',captureallregionssurroundedby'X'.Aregioniscapturedbyflippingall'O'sinto'X'sinthatsurroundedregion.Forexample,XXXXXOOXXXOXXOXXAft......
  • Round-Robin轮询调度法及其实现原理
    轮询调度算法(Round-RobinScheduling)轮询调度算法的原理是每一次把来自用户的请求轮流分配给内部中的服务器,从1开始,直到N(内部服务器个数),然后重新开始循环。算法的优点是其简洁性,它无需记录当前所有连接的状态,所以它是一种无状态调度。轮询调度算法流程假设有......
  • Codeforces Beta Round #2--B题 (DP)
    题目:Theleastroundway 1000*1000的方阵,每个格子有一个非负整数,现在要从左上走到右下,每次只能向下或者向右走。目标是使得所有走的格子里的数的乘积里,末尾0的个数最少,要求输出最有解和走法。不用怎么想也知道基本是个dp了,可以发现其实只有2和5的因子是有用的,但是如果状态同时记......
  • NSSRound#11 Basic
    ez_encABAABBBAABABAABBABABAABBABAAAABBABABABAAABAAABBAABBBBABBABBABBABABABAABBAABBABAAABBAABBBABABABAAAABBAAABABAABABBABBBABBAAABBBAABABAABBAAAABBBAAAABAABBBAABBABABAABABAAAAABBBBABAABBBBAAAABBBBBAB题目提示不是baconic,那就将A转成0,B转成1,然后用工具一把梭,发现......