Codeforces Round #563 Div. 2

  A:顯然排序便可。html

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1000000010
#define N 1010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
int n,a[N<<1];
signed main()
{
	n=read();
	for (int i=1;i<=2*n;i++ )a[i]=read();
	sort(a+1,a+2*n+1);int s=0;
	for (int i=1;i<=n;i++) s+=a[i];
	int s2=0;for (int i=n+1;i<=2*n;i++) s2+=a[i];
	if (s!=s2) {for (int i=1;i<=2*n;i++) cout<<a[i]<<' ';}
	else cout<<-1;
	return 0;
	//NOTICE LONG LONG!!!!!
}

  B:若奇偶數均存在就能夠任意交換,sort便可。不然沒法改變。c++

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1000000010
#define N 100010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
int n,a[N];
signed main()
{
	n=read();
	for (int i=1;i<=n;i++) a[i]=read();
	int s=0,s2=0;
	for (int i=1;i<=n;i++) if (a[i]&1) s++;else s2++;
	if (s&&s2) sort(a+1,a+n+1);
	for (int i=1;i<=n;i++) printf("%d ",a[i]);
	return 0;
	//NOTICE LONG LONG!!!!!
}

  C:質數下標之間須要兩兩不一樣,因而最大值就是<=n的質數個數。對於合數,將其設爲其最小質因子對應的數便可。spa

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1000000010
#define N 100010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
int n,a[N];
bool isprime(int x)
{
	for (int i=2;i*i<=x;i++) if (x%i==0) return 0;
	return 1;
}
signed main()
{
	n=read();
	int mx=0;
	for (int i=2;i<=n;i++)
	if (isprime(i)) a[i]=++mx;
	else
	{
		for (int j=2;j*j<=i;j++) if (i%j==0) {a[i]=a[j];break;}
	}
	for (int i=2;i<=n;i++) printf("%d ",a[i]);
	return 0;
	//NOTICE LONG LONG!!!!!
}

  D:若是m>=2n即沒有限制,使序列前綴和依次爲0,1,2……便可。不然這些數中有一半不能選擇,作法相似。htm

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1000000010
#define N 18
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
int n,m,a[1<<N],cnt;
signed main()
{
	n=read(),m=read();
	if (m>=(1<<n))
	{
		cout<<(1<<n)-1<<endl;
		for (int i=1;i<(1<<n);i++) printf("%d ",i^(i-1));
	}
	else
	{
		for (int i=1;i<(1<<n);i++) if (i<(i^m)) a[++cnt]=i;
		cout<<(1<<n-1)-1<<endl;
		for (int i=1;i<=(1<<n-1)-1;i++) printf("%d ",a[i]^a[i-1]);
	}
	return 0;
	//NOTICE LONG LONG!!!!!
}

  E:容易發現前綴gcd最大變化次數就是<=n的數所擁有的最大質因子個數。顯然形如2k的數知足條件。同時3*2k-1也可能知足條件。對於前者, 設c[i]=[n/2i]-[n/2i+1],也即恰有i個2的數的個數,那麼合法排列方案數的計算相似於CTS2019隨機立方體https://www.cnblogs.com/Gloid/p/10936699.html中的一個子問題,再也不贅述。後者的話考慮一下3這個因子在還剩多少個2時被刪去,相似地計算。blog

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1000000010
#define N 1000010
#define P 1000000007
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
	int x=0,f=1;char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
int n,ans,a[30],fac[N],inv[N],b[2][30];
int C(int n,int m){if (m>n) return 0;return 1ll*fac[n]*inv[m]%P*inv[n-m]%P;}
void inc(int &x,int y){x+=y;if (x>=P) x-=P;}
signed main()
{
	n=read();
	fac[0]=1;for (int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%P;
	inv[0]=inv[1]=1;for (int i=2;i<=n;i++)  inv[i]=P-1ll*(P/i)*inv[P%i]%P;
	for (int i=2;i<=n;i++) inv[i]=1ll*inv[i]*inv[i-1]%P;
	int m=0,tmp=n;while (tmp) m++,tmp>>=1;
	m--;
	if ((3<<m-1)<=n)
	{
		for (int i=1;i<=n;i++)
		{
			int x=0,y=i;
			while (y%2==0) y>>=1,x++;
			if (i%3==0) b[1][min(x,m-1)]++;
			else b[0][min(x,m-1)]++;
		}
		for (int i=m-1;i>=0;i--)
		{
			int s=1,u=n;
			for (int j=m-1;j>=i;j--)
			{
				s=1ll*s*C(u-1,b[1][j]-1)%P*fac[b[1][j]]%P;
				u-=b[1][j];
			}
			int tot=0;for (int j=m-1;j>=i;j--) tot+=b[0][j];
			s=1ll*s*C(u-1,tot-1)%P*fac[tot]%P;
			u-=tot;
			for (int j=i-1;j>=0;j--)
			{
				s=1ll*s*C(u-1,b[0][j]+b[1][j]-1)%P*fac[b[0][j]+b[1][j]]%P;
				u-=b[0][j]+b[1][j];
			}
			inc(ans,s);
		}
	}
	memset(a,0,sizeof(a));
	for (int i=1;i<=n;i++)
	{
		int x=0,y=i;
		while (y%2==0) y>>=1,x++;
		a[x]++;
	}
	int s=1,u=n;
	for (int i=m;i>=0;i--)
	{
		s=1ll*s*C(u-1,a[i]-1)%P*fac[a[i]]%P;
		u-=a[i];
	}
	inc(ans,s);
	cout<<ans;
	return 0;
	//NOTICE LONG LONG!!!!!
}

  F:點分治,問出當前點深度就能夠知道其是否是隱藏點的祖先。若是不是就在該點父親所在子樹中繼續尋找,不然問出到隱藏點路徑上的第二個點到對應子樹中尋找。發現離隱藏點距離爲0時就找到了。排序

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 200010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
int n,p[N],t,d,fa[N],deep[N],size[N];
bool flag[N];
struct data{int to,nxt;
}edge[N<<1];
void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;}
void dfs(int k)
{
    for (int i=p[k];i;i=edge[i].nxt)
    if (edge[i].to!=fa[k])
    {
        deep[edge[i].to]=deep[k]+1;
        fa[edge[i].to]=k;
        dfs(edge[i].to);
    }
}
void make(int k,int from)
{
    size[k]=1;
    for (int i=p[k];i;i=edge[i].nxt)
    if (edge[i].to!=from&&!flag[edge[i].to])
    {
        make(edge[i].to,k);
        size[k]+=size[edge[i].to];
    }
}
int findroot(int k,int from,int s)
{
    int mx=0;
    for (int i=p[k];i;i=edge[i].nxt)
    if (edge[i].to!=from&&!flag[edge[i].to]&&size[edge[i].to]>size[mx]) mx=edge[i].to;
    if ((size[mx]<<1)>s) return findroot(mx,k,s);
    else return k;
}
int getd(int x){cout<<"d "<<x<<endl;return read();}
int gets(int x){cout<<"s "<<x<<endl;return read();}
int solve(int k)
{
    make(k,k);flag[k=findroot(k,k,size[k])]=1;
    int u=getd(k);if (u==0) return k;
    if (deep[k]+u==d) return solve(gets(k));
    else return solve(fa[k]);
}
int main()
{
    n=read();
    for (int i=1;i<n;i++)
    {
        int x=read(),y=read();
        addedge(x,y),addedge(y,x);
    }
    dfs(1);
    d=getd(1);
    cout<<"! "<<solve(1);
    return 0;
}

  小小小號。result:rank 18 rating +163get

相關文章
相關標籤/搜索