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个折扣的时候,你必须 ...
随机推荐
- Frament 方法
public final boolean isAdded() 如果该Fragment对象被添加到了它的Activity中,那么它返回true,否则返回false. public final boole ...
- JS延时提示框
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 31.0px Consolas; color: #2b7ec3 } p.p2 { margin: 0.0px ...
- 【洛谷P1351】联合权值
我们枚举中间点,当连的点数不小于2时进行处理 最大值好搞 求和:设中间点 i 所连所有点权之和为sum 则对于每个中间点i的联合权值之和为: w[j]*(sum-w[j])之和 #include< ...
- 关于在xml文件中的 error: invalid symbol: 'switch' 错误
在xml布局文件中使用Switch控件时,出现error: invalid symbol: 'switch'报错,代码如下: <Switch android:id="@+id/swit ...
- node应用场景
2.1 Web开发:Express + EJS + Mongoose/MySQL express 是轻量灵活的Nodejs Web应用框架,它可以快速地搭建网站.Express框架建立在Nodejs内 ...
- [译]GLUT教程 - glutPostRedisplay函数
Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> glutPostRedisplay 直 ...
- URAL - 1917 Titan Ruins: Deadly Accuracy(水题)
水题一个,代码挫了一下: 题意不好理解. 你去一个洞窟内探险,洞窟内有许多宝石,但都有魔法守护,你需要用魔法将它们打下来. 每个宝石都有自己的防御等级,当你的魔法超过它的防御等级时它就会被你打下来. ...
- C++混合编程之idlcpp教程Python篇(2)
在上一篇 C++混合编程之idlcpp教程(一) 中介绍了 idlcpp 工具的使用.现在对 idlcpp 所带的示例教程进行讲解,这里针对的 Python 语言的例子.首先看第一个示例程序 Pyth ...
- JSP标准标签库(JSTL)之核心标签(下)
前面记录了核心标签的前几种,现在来接着记录. 七.<c:import>标签 使用<c:import>标签可以包含一个FTP服务器中不同的网页内容.语法格式: <c:i ...
- 手机端使用rem适配
最近一直在做手机端的东西,各种型号的手机适配很是无解.经过同事及百度找到了这么一个方法 html font-size默认100px 将rem进行换算1px==0.01rem; 页面在各个手机适配个别会 ...