Codeforces Round #177 (Div. 1) 题解【ABCD】
Codeforces Round #177 (Div. 1)
A. Polo the Penguin and Strings
题意
让你构造一个长度为n的串,且里面恰好包含k个不同字符,让你构造的字符串字典序最小。
题解
先abababab,然后再把k个不同字符输出,那么这样就是最少
代码
#include<bits/stdc++.h>
using namespace std;
string s;
int main()
{
int n,k;
scanf("%d%d",&n,&k);
if(n>1&&k==1){
cout<<"-1"<<endl;
return 0;
}
if(k>n){
cout<<"-1"<<endl;
return 0;
}
if(k==1){
for(int i=1;i<=n;i++)
printf("a");
}else{
for(int i=0;i<n-k+2;i++){
if(i%2==0)printf("a");
else printf("b");
}
for(int i=2;i<k;i++)
printf("%c",'a'+i);
}
printf("\n");
}
B. Polo the Penguin and Houses
题意
给你n个城市,你在x城市,那么下一步会走到a[x]城市。
现在你从前k个城市出发,最终都会走到1号点,从后面n-k个城市出发,不会走到1节点,问你一共有多少种方案。
题解
k很小,所以直接dfs就好了,可以先预处理一下一些没必要dfs的部分。
代码
#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
int n,k,cnt=0;
int a[10],flag,vis[10];
long long ans = 0;
int dfs2(int x){
if(x==1)return 1;
if(vis[x])return 0;
vis[x]=1;
dfs2(a[x]);
}
void check()
{
flag=1;
for(int i=2;i<=k&&flag;i++){
memset(vis,0,sizeof(vis));
flag&=dfs2(i);
}
if(flag)cnt++;
}
void dfs(int x){
if(x==k+1){
check();
return;
}
for(int i=1;i<=k;i++)
a[x]=i,dfs(x+1);
}
int main()
{
scanf("%d%d",&n,&k);
ans=k;
for(int i=0;i<n-k;i++)
ans=(ans*(n-k))%mod;
dfs(2);
cout<<(ans*1ll*cnt)%mod<<endl;
}
C - Polo the Penguin and XOR operation
题意
让你构造一个排列,使得sigma(i^a[i])最大
题解
手动玩一玩可以发现,实际上是可以异或互补的,所以我们把那些互补的都给补上,答案就是最大的。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int vis[maxn],n,a[maxn],ans[maxn];
int main(){
scanf("%d",&n);
for(int i=n;i>=0;i--){
if(vis[i])continue;
int tmp=i,len=0;
while(tmp){
tmp/=2;
len++;
}
int Up=(1<<len)-1;
int x2=Up^i;
ans[i]=x2;
ans[x2]=i;
vis[i]=vis[x2]=1;
}
long long Ans = 0;
for(int i=0;i<=n;i++)
Ans+=i^ans[i];
cout<<Ans<<endl;
for(int i=0;i<=n;i++)
cout<<ans[i]<<" ";
cout<<endl;
}
288D - Polo the Penguin and Trees
题意
给你一棵树,问你有多少对不相交路径
题解
反过来做,然后容斥搞一搞,减去相交的对数。
分为子树外和子树内,都扣一扣就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 8e4+6;
int n;
long long s[maxn];
long long ans = 0;
vector<int> E[maxn];
void dfs(int x,int f){
s[x]=1;
long long tmp = 0;
for(int i=0;i<E[x].size();i++){
int v=E[x][i];
if(v==f)continue;
dfs(v,x);
tmp+=1ll*s[x]*s[v];
s[x]+=s[v];
}
ans-=1ll*tmp*(tmp+2LL*s[x]*(n-s[x]));
}
int main()
{
scanf("%d",&n);
for(int i=1;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
E[a].push_back(b);
E[b].push_back(a);
}
ans=1ll*n*(n-1LL)/2LL*(n*(n-1LL)/2LL);
dfs(1,0);
cout<<ans<<endl;
}
Codeforces Round #177 (Div. 1) 题解【ABCD】的更多相关文章
- Codeforces Round #177 (Div. 2) 题解
[前言]咦?如今怎么流行打CF了?于是当一帮大爷在执着的打div 1的时候,我偷偷的在刷div 2.至于怎么决定场次嘛.一般我报一个数字A,随便再拉一个人选一个数字B.然后開始做第A^B场.假设认为机 ...
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
随机推荐
- Lvs之NAT、DR、TUN三种模式的应用配置案例
LVS 一.LVS简介 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的 ...
- 6.4 APK包限制
Google 2015年 9月28日公告:为了Android开发商可以制作出更加复杂的app和游戏,Google Play游戏包体(APK)大小由原来的50MB提高到100MB.但是针对Android ...
- split(),preg_split()与explode()函数分析与介
split(),preg_split()与explode()函数分析与介 发布时间:2013-06-01 18:32:45 来源:尔玉毕业设计 评论:0 点击:965 split()函数可以实 ...
- nginx做反向代理并防盗链
nginx做反向代理真的非常简单,只需设置location+proxy_pass即可. 防盗链配置有些复杂,需要注意的地方: 在防盗链的location中需要再设置一下proxy_pass(在这里走了 ...
- 解析ASP.NET WebForm和Mvc开发的区别
因为以前主要是做WebFrom开发,对MVC开发并没有太深入的了解.自从来到创新工场的新团队后,用的技术都是自己以前没有接触过的,比如:MVC 和EF还有就是WCF,压力一直很大.在很多问题都是不清楚 ...
- [matlab] 矩阵操作
>_<:矩阵构造 1.简单矩阵构造 最简单的方法是采用矩阵构造符“[]”.构造1´n矩阵(行向量)时,可以将各元素依次放入矩阵构造符[]内,并且以空格或者逗号分隔:构造m´n矩阵时,每行如 ...
- 理解Javascript的异步等待
目前async / await特性并没有被添加到ES2016标准中,但不代表这些特性将来不会被加入到Javascript中.在我写这篇文章时,它已经到达第三版草案,并且正迅速的发展中.这些特性已经被I ...
- 插件~使用ECharts动态在地图上标识点
ECharts可以很方便的在网页上绘制地图,图表,并且可以提供下载图像,放大,缩小,拖动等功能,今天主要说一下它的地图类型(type:'map')是如何实现的. 首先在ECharts地图的坐标需要我们 ...
- 如何启动/停止/重启MySQL
启动.停止.重启 MySQL 是每个拥有独立主机的站长必须要撑握的操作,下面为大家简单介绍一下操作方法: 一.启动方式 1.使用 service 启动:service mysqld start 2.使 ...
- crossplatform---Nodejs in Visual Studio Code 09.企业网与CNPM
1.开始 CNPM : https://npm.taobao.org/ 2.企业网HTTP代理上网 平时办公在一个大企业网(10.*.*.*)中,使用HTTP代理上网,发现npm命令无法执行. 解决方 ...