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】的更多相关文章

  1. Codeforces Round #177 (Div. 2) 题解

    [前言]咦?如今怎么流行打CF了?于是当一帮大爷在执着的打div 1的时候,我偷偷的在刷div 2.至于怎么决定场次嘛.一般我报一个数字A,随便再拉一个人选一个数字B.然后開始做第A^B场.假设认为机 ...

  2. Codeforces Round #182 (Div. 1)题解【ABCD】

    Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...

  3. Codeforces Round #608 (Div. 2) 题解

    目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...

  4. Codeforces Round #525 (Div. 2)题解

    Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...

  5. Codeforces Round #528 (Div. 2)题解

    Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...

  6. Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F

    Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...

  7. Codeforces Round #677 (Div. 3) 题解

    Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...

  8. Codeforces Round #665 (Div. 2) 题解

    Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...

  9. Codeforces Round #160 (Div. 1) 题解【ABCD】

    Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...

随机推荐

  1. canvas,制作炫酷的时钟和倒计时

    html部分 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 30.0px Consolas; color: #2b7ec3 } p.p2 { margin ...

  2. 使用Notepad++编码编译时报错(已解决?)

    使用Notepad++编码编译时报错(已解决?) 使用Notepad++编码,编译的时候经常会报错,说什么GBK编码啥啥啥~~~但同样的编码用ECLIPSE就没有问题.再有,用记事本把他保存成ANSI ...

  3. 6.7 Binder机制

    Binder在Android系统中江湖地位非常之高.在Zygote孵化出system_server进程后,在system_server进程中出初始化支持整个Android framework的各种各样 ...

  4. OpenGL(一)——入门学习

    概要 1. 为什么使用OpenGL 2. 在VS2008上搭建环境 3. 一个简单的例程 OpenGL相较于DirectX的优越性 1. 与C语言紧密结合 OpenGL命令最初就是用C语言函数来进行描 ...

  5. 注册表(regedit)

    注册表(Registry,繁体中文版Windows称之为登录档)是Microsoft Windows中的一个重要的数据库,用于存储系统和应用程序的设置信息. 打开方式:1.开始>>运行.中 ...

  6. QT中QProcess调用命令行的痛苦经历

    在QT程序中需要将某些目录和文件压缩为一个rar的压缩包,于是想到了在QT中通过QProcess类调用命令行的rar.exe来达到效果,但是没想到QProcess类用起来很麻烦,而且达不到效果,折腾了 ...

  7. 用c#开发微信 (12) 微统计 - 阅读分享统计系统 2 业务逻辑实现

    微信平台自带的统计功能太简单,有时我们需要统计有哪些微信个人用户阅读.分享了微信公众号的手机网页,以及微信个人用户访问手机网页的来源:朋友圈分享访问.好友分享消息访问等.本系统实现了手机网页阅读.分享 ...

  8. 微软BI 之SSIS 系列 - 在 SSIS 中导入 ACCESS 数据库中的数据

    开篇介绍 来自 天善学院 一个学员的问题,如何在 SSIS 中导入 ACCESS 数据表中的数据. 在 SSIS 中导入 ACCESS 数据库数据 ACCESS 实际上是一个轻量级的桌面数据库,直接使 ...

  9. 说不尽的MVVM(1) – Why MVVM

    最近学的一篇课文<说不尽的狗>竟让我有了写<说不尽的MVVM>这一想法,事非亵渎,实出无奈.我在刚学WPF不久时听说有MVVM这种东西,做了下尝试,发现他能给程序的设计带来很大 ...

  10. JS 日期格式化和解析工具

    本来想模仿Java里面的SimpleDateFormat()对象的,但是感觉这样用起来不方便,所以还是直接写成单独的方法算了. 原文链接 日期格式化 使用说明 formatDate(date, fmt ...