Codeforces Round #177 (Div. 1)

A. Polo the Penguin and Strings

题意

让你构造一个长度为n的串,且里面恰好包含k个不同字符,让你构造的字符串字典序最小。

题解

先abababab,然后再把k个不同字符输出,那么这样就是最少

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. string s;
  4. int main()
  5. {
  6. int n,k;
  7. scanf("%d%d",&n,&k);
  8. if(n>1&&k==1){
  9. cout<<"-1"<<endl;
  10. return 0;
  11. }
  12. if(k>n){
  13. cout<<"-1"<<endl;
  14. return 0;
  15. }
  16. if(k==1){
  17. for(int i=1;i<=n;i++)
  18. printf("a");
  19. }else{
  20. for(int i=0;i<n-k+2;i++){
  21. if(i%2==0)printf("a");
  22. else printf("b");
  23. }
  24. for(int i=2;i<k;i++)
  25. printf("%c",'a'+i);
  26. }
  27. printf("\n");
  28. }

B. Polo the Penguin and Houses

题意

给你n个城市,你在x城市,那么下一步会走到a[x]城市。

现在你从前k个城市出发,最终都会走到1号点,从后面n-k个城市出发,不会走到1节点,问你一共有多少种方案。

题解

k很小,所以直接dfs就好了,可以先预处理一下一些没必要dfs的部分。

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int mod = 1e9+7;
  4. int n,k,cnt=0;
  5. int a[10],flag,vis[10];
  6. long long ans = 0;
  7. int dfs2(int x){
  8. if(x==1)return 1;
  9. if(vis[x])return 0;
  10. vis[x]=1;
  11. dfs2(a[x]);
  12. }
  13. void check()
  14. {
  15. flag=1;
  16. for(int i=2;i<=k&&flag;i++){
  17. memset(vis,0,sizeof(vis));
  18. flag&=dfs2(i);
  19. }
  20. if(flag)cnt++;
  21. }
  22. void dfs(int x){
  23. if(x==k+1){
  24. check();
  25. return;
  26. }
  27. for(int i=1;i<=k;i++)
  28. a[x]=i,dfs(x+1);
  29. }
  30. int main()
  31. {
  32. scanf("%d%d",&n,&k);
  33. ans=k;
  34. for(int i=0;i<n-k;i++)
  35. ans=(ans*(n-k))%mod;
  36. dfs(2);
  37. cout<<(ans*1ll*cnt)%mod<<endl;
  38. }

C - Polo the Penguin and XOR operation

题意

让你构造一个排列,使得sigma(i^a[i])最大

题解

手动玩一玩可以发现,实际上是可以异或互补的,所以我们把那些互补的都给补上,答案就是最大的。

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1e6+7;
  4. int vis[maxn],n,a[maxn],ans[maxn];
  5. int main(){
  6. scanf("%d",&n);
  7. for(int i=n;i>=0;i--){
  8. if(vis[i])continue;
  9. int tmp=i,len=0;
  10. while(tmp){
  11. tmp/=2;
  12. len++;
  13. }
  14. int Up=(1<<len)-1;
  15. int x2=Up^i;
  16. ans[i]=x2;
  17. ans[x2]=i;
  18. vis[i]=vis[x2]=1;
  19. }
  20. long long Ans = 0;
  21. for(int i=0;i<=n;i++)
  22. Ans+=i^ans[i];
  23. cout<<Ans<<endl;
  24. for(int i=0;i<=n;i++)
  25. cout<<ans[i]<<" ";
  26. cout<<endl;
  27. }

288D - Polo the Penguin and Trees

题意

给你一棵树,问你有多少对不相交路径

题解

反过来做,然后容斥搞一搞,减去相交的对数。

分为子树外和子树内,都扣一扣就好了

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 8e4+6;
  4. int n;
  5. long long s[maxn];
  6. long long ans = 0;
  7. vector<int> E[maxn];
  8. void dfs(int x,int f){
  9. s[x]=1;
  10. long long tmp = 0;
  11. for(int i=0;i<E[x].size();i++){
  12. int v=E[x][i];
  13. if(v==f)continue;
  14. dfs(v,x);
  15. tmp+=1ll*s[x]*s[v];
  16. s[x]+=s[v];
  17. }
  18. ans-=1ll*tmp*(tmp+2LL*s[x]*(n-s[x]));
  19. }
  20. int main()
  21. {
  22. scanf("%d",&n);
  23. for(int i=1;i<n;i++){
  24. int a,b;
  25. scanf("%d%d",&a,&b);
  26. E[a].push_back(b);
  27. E[b].push_back(a);
  28. }
  29. ans=1ll*n*(n-1LL)/2LL*(n*(n-1LL)/2LL);
  30. dfs(1,0);
  31. cout<<ans<<endl;
  32. }

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. Lvs之NAT、DR、TUN三种模式的应用配置案例

    LVS 一.LVS简介     LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的 ...

  2. 6.4 APK包限制

    Google 2015年 9月28日公告:为了Android开发商可以制作出更加复杂的app和游戏,Google Play游戏包体(APK)大小由原来的50MB提高到100MB.但是针对Android ...

  3. split(),preg_split()与explode()函数分析与介

    split(),preg_split()与explode()函数分析与介 发布时间:2013-06-01 18:32:45   来源:尔玉毕业设计   评论:0 点击:965 split()函数可以实 ...

  4. nginx做反向代理并防盗链

    nginx做反向代理真的非常简单,只需设置location+proxy_pass即可. 防盗链配置有些复杂,需要注意的地方: 在防盗链的location中需要再设置一下proxy_pass(在这里走了 ...

  5. 解析ASP.NET WebForm和Mvc开发的区别

    因为以前主要是做WebFrom开发,对MVC开发并没有太深入的了解.自从来到创新工场的新团队后,用的技术都是自己以前没有接触过的,比如:MVC 和EF还有就是WCF,压力一直很大.在很多问题都是不清楚 ...

  6. [matlab] 矩阵操作

    >_<:矩阵构造 1.简单矩阵构造 最简单的方法是采用矩阵构造符“[]”.构造1´n矩阵(行向量)时,可以将各元素依次放入矩阵构造符[]内,并且以空格或者逗号分隔:构造m´n矩阵时,每行如 ...

  7. 理解Javascript的异步等待

    目前async / await特性并没有被添加到ES2016标准中,但不代表这些特性将来不会被加入到Javascript中.在我写这篇文章时,它已经到达第三版草案,并且正迅速的发展中.这些特性已经被I ...

  8. 插件~使用ECharts动态在地图上标识点

    ECharts可以很方便的在网页上绘制地图,图表,并且可以提供下载图像,放大,缩小,拖动等功能,今天主要说一下它的地图类型(type:'map')是如何实现的. 首先在ECharts地图的坐标需要我们 ...

  9. 如何启动/停止/重启MySQL

    启动.停止.重启 MySQL 是每个拥有独立主机的站长必须要撑握的操作,下面为大家简单介绍一下操作方法: 一.启动方式 1.使用 service 启动:service mysqld start 2.使 ...

  10. crossplatform---Nodejs in Visual Studio Code 09.企业网与CNPM

    1.开始 CNPM : https://npm.taobao.org/ 2.企业网HTTP代理上网 平时办公在一个大企业网(10.*.*.*)中,使用HTTP代理上网,发现npm命令无法执行. 解决方 ...