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个折扣的时候,你必须 ...
随机推荐
- linux用户、组管理及权限(一)
一.用户管理 1.为什么需要用户 1)计算机及网络资源的合理分配 2)可以控制用户访问系统的权限.3)身份认证 4) 进程 以某个用户的身份来运行 2.用户分类 用户的角色是通过UID(用户ID)来 ...
- Setting SVN Repository Using TortoiseSVN + Dropbox in 5 Minutes
SVN is a very common version control system in software development. However configuring SVN server ...
- berkeley db replica机制 - 主从同步
repmgr/repmgr_net.c, __repmgr_send(): 做send_broadcast, 然后根据policy 对DB_REP_PERMANENT的处理 __repmgr_send ...
- Dapper试用
以下代码摘自imfunny的<给力分享新的ORM => Dapper> http://www.cnblogs.com/imfunny/archive/2011/09/16/21788 ...
- Dynamic CRM 2013学习笔记(一)插件输入实体参数解析
1. 问题描述 最近新建了一个post事件的插件,传入的参数处理如下: 1: if (context.InputParameters.Contains("Target") &a ...
- C#设计模式(15)——命令模式(Command Pattern)
一.前言 之前一直在忙于工作上的事情,关于设计模式系列一直没更新,最近项目中发现,对于设计模式的了解是必不可少的,当然对于设计模式的应用那更是重要,可以说是否懂得应用设计模式在项目中是衡量一个程序员的 ...
- 大熊君说说JS与设计模式之------代理模式Proxy
一,总体概要 1,笔者浅谈 当我们浏览网页时,网页中的图片有时不会立即展示出来,这就是通过虚拟代理来替代了真实的图片,而代理存储了真实图片的路径和尺寸,这就是代理方式的一种. 代理模式是比较有用途的一 ...
- How Delete File with Readonly Permission?
class Program { static void Main(string[] args) { string file=@"E:\readme.txt"; try { File ...
- Spring 依赖注入控制反转实现,及编码解析(自制容器)
定义: 在运行期,由外部容器动态的将依赖对象动态地注入到组件中. 两种方式: 手工装配 -set方式 -构造器 -注解方式 自动装配(不推荐) 1利用构造器 2set方法注入 dao: package ...
- celery与mangodb搭配应用
写作背景介绍 在celery简单应用中已经介绍了如何去配置一个celery应用,也知道怎么分离任务逻辑代码与客户端代码了.我们现在的任务是怎么把计算结果保存到数据库中,这种数据持久化是非常重要的.你一 ...