AtCoder Grand Contest 014 题解
A - Cookie Exchanges 模拟
Problem Statement
Takahashi, Aoki and Snuke love cookies. They have A, B and C cookies, respectively. Now, they will exchange those cookies by repeating the action below:
Each person simultaneously divides his cookies in half and gives one half to each of the other two persons.
This action will be repeated until there is a person with odd number of cookies in hand.
How many times will they repeat this action? Note that the answer may not be finite.
这道题就是模拟一个过程:
每次都进行下面的操作.
如果所有人的饼干都是偶数,就折半分给其他两个人,否则结束循环.
然后问循环次数,-1表示无限次.
Constraints
- \(1\leq A,B,C \leq 10^9\)
Solution
直接模拟就好了.
貌似这东西可以证明其复杂度是有保障的.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(ll &x){
x=0;static char ch;static bool flag;flag = false;
while(ch=getchar(),ch<'!');if(ch=='-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
#define rg register int
#define rep(i,a,b) for(rg i=(a);i<=(b);++i)
#define per(i,a,b) for(rg i=(a);i>=(b);--i)
const int lim = 100000000;
int main(){
ll a,b,c;read(a);read(b);read(c);
int ans = 0;
ll tmpa,tmpb,tmpc;
while(ans <= lim){
tmpa = tmpb = tmpc = 0;
bool flag = false;
if(!(a&1)) tmpb += a >> 1,tmpc += a >> 1;
else break;
if(!(b&1)) tmpa += b >> 1,tmpc += b >> 1;
else break;
if(!(c&1)) tmpa += c >> 1,tmpb += c >> 1;
else break;
if(a == tmpa && b == tmpb && c == tmpc){
puts("-1");
return 0;
}a = tmpa;b = tmpb;c = tmpc;++ ans;
}
if(ans <= lim) printf("%d\n",ans);
else puts("-1");
return 0;
}
B - Unplanned Queries
Problem Statement
Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.
First, Takahashi created a tree with N vertices numbered 1 through N, and wrote 0 at each edge.
Then, Aoki gave him M queries. The i-th of them is as follows:
Increment the number written at each edge along the path connecting vertices ai and bi, by one.
After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.
Determine whether there exists a tree that has the property mentioned by Takahashi.
定义在树上从u走到v时会将所经过的所有的边上的权都+1,初始权为0.
给出若干次的起点和终点.现在知道最终树上的所有边权均为偶数,问是否存在这么一课树。
Constraints
- \(2 \leq N \leq 105\)
- \(1 \leq M \leq 105\)
- \(1 \leq ai,bi \leq N\)
- \(ai \neq bi\)
Solution
这道题直接构造出以1为根,剩下的所有的点的父亲都是1的树即可.
你要问为什么我只能说我也不知道,考场上抱着写写试试的心态没想到就过了.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;static char ch;static bool flag;flag = false;
while(ch=getchar(),ch<'!');if(ch=='-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
#define rg register int
#define rep(i,a,b) for(rg i=(a);i<=(b);++i)
#define per(i,a,b) for(rg i=(a);i>=(b);--i)
const int maxn = 100010;
int a[maxn];
int main(){
int n,m;read(n);read(m);
int u,v;
rep(i,1,m){
read(u);read(v);
if(u != 1) ++ a[u];
if(v != 1) ++ a[v];
}
bool flag = false;
rep(i,2,n) if(a[i] & 1) {flag = true;break;}
puts(flag ? "NO" : "YES");
return 0;
}
AtCoder Grand Contest 014 题解的更多相关文章
- AtCoder Grand Contest 014题解
传送门 \(A\) 首先大力猜测一下答案不会很大,所以次数大于\(10^6\)输出\(-1\)就行了 不过我并不会证上界,据说是因为如果\(a=b=c\)且都是偶数肯定\(-1\),否则设\(a\le ...
- AtCoder Grand Contest 014
AtCoder Grand Contest 014 A - Cookie Exchanges 有三个人,分别有\(A,B,C\)块饼干,每次每个人都会把自己的饼干分成相等的两份然后给其他两个人.当其中 ...
- AtCoder Grand Contest 014 D:Black and White Tree
题目传送门:https://agc014.contest.atcoder.jp/tasks/agc014_d 题目翻译 给你一棵树,每次任选一个点染色,先手染白色,后手染黑色.如果最后存在一个白色的点 ...
- AtCoder Grand Contest 014 E:Blue and Red Tree
题目传送门:https://agc014.contest.atcoder.jp/tasks/agc014_e 题目翻译 有一棵有\(N\)个点的树,初始时每条边都是蓝色的,每次你可以选择一条由蓝色边构 ...
- AtCoder Grand Contest 017 题解
A - Biscuits 题目: 给出 \(n\) 个物品,每个物品有一个权值. 问有多少种选取方式使得物品权值之和 \(\bmod\space 2\) 为 \(p\). \(n \leq 50\) ...
- Atcoder Grand Contest 054 题解
那天晚上由于毕业晚会与同学吃饭喝酒没打 AGC,第二天稍微补了下题,目前补到了 E,显然 AGC 的 F 对于我来说都是不可做题就没补了(bushi A 简单题,不难发现如果我们通过三次及以上的操作将 ...
- AtCoder Grand Contest 030题解
第一次套刷AtCoder 体验良好 传送门 Poisonous Cookies cout<<b+min(c,a+b+); Tree Burning 难度跨度有点大啊 可以证明当第一次转向之 ...
- AtCoder Grand Contest 031题解
题面 传送门 题解 比赛的之后做完\(AB\)就开始发呆了--简直菜的一笔啊-- \(A - Colorful\ Subsequence\) 如果第\(i\)个字母选,那么它前面任意一个别的字母的选择 ...
- AtCoder Grand Contest 039 题解
传送门 \(A\) 首先只有一串的情况下,遇到相同的肯定是改后面那一个最优,然后两串的话可能要分奇偶讨论一下 //quming #include<bits/stdc++.h> #defin ...
随机推荐
- 2016/05/17 thinkphp3.2.2 分页的使用:①在Home下设置Publics文件夹或在thinkPHP下library的vender 把page.class.php 考贝进入 ②通过new 实例化方式调用 $page=new \Home\Publics\Page($total,3);
注意分页的方法有两种:一种是thinkphp3.2 自带的 另一种是之前新闻页用过的 显示效果稍有差别 显示效果: 细节问题: ①搜索页面 要加session判断 和 分页 ②修改 ...
- 求最小正整数x,A^x=1(mod M)求阶模板
整数的阶:设a和n是互素的正整数,使得a^x=1(mod n)成立的最小的正整数x称为a模n的阶 //求阶模板:A^x=1(mod M),调用GetJie(A,M) //输入:10^10>A,M ...
- Sitemesh3的使用心得
项目中用到了sitemesh3,就把使用心得记下来,至于配置之类的,官方网站都有,这里只是写下自己对它的理解,方便再次理解, sitemesh是基于过滤器的原理,拦截到符合配置文件中配置的路径,然后会 ...
- Linux 服务器配置JDK
1. 查看java版本 [root@plttestap5 ~]# java -versionjava version "1.8.0_121"Java(TM) SE Runtime ...
- E - What Is Your Grade?
E - What Is Your Grade? Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- Wrapper配置详解及高级应用
将一个简单的程度如HelloWorld 的应用包装秤Wrapper 服务并不复杂,甚至可以认为非常简单.但是实际项目应用过程中我们的程序一般较庞大,运行环境也较复杂. 通过Wrapper 配置文件 ...
- [DBNETLIB][ConnectionOpen(Connect()).]SQL Server 不存在或拒绝访问 数据库错误 解决办法总结
连接数据库报错:“数据库异常:[DBNETLIB] [ConnectionOpen(Connenct()).] Sqlserver 不存在或拒绝访问” 原因: 1.查看是不是没有在数据库中添加数据库服 ...
- hdu2563——统计问题
Problem Description 在一无限大的二维平面中,我们做例如以下如果: 1. 每次仅仅能移动一格. 2. 不能向后走(如果你的目的地是"向上",那么你能够向左走, ...
- Java并发之BlockingQueue
一.Queue Queue是队列接口是 Collection的子接口.除了基本的 Collection操作外,队列还提供其他的插入.提取和检查操作.每个方法都存在两种形式:一种抛出异常(操作失败时 ...
- 读:Instance-aware Image and Sentence Matching with Selective Multimodal LSTM
摘要:有效图像和句子匹配取决于如何很好地度量其全局视觉 - 语义相似度.基于观察到这样的全局相似性是由图像(对象)和句子(词)的成对实例之间的多个局部相似性的复合聚集,我们提出了一个实例感知图像和句子 ...