Codeforces 964 等比数列逆元处理 贪心删偶数度节点
A
B
C
注意sum要在mod范围内 且不能用/a*b来推
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll num[];
const ll mod = 1e9 + ;
ll qpow(ll a, ll b)
{
ll ans = , base = a;
while (b)
{
if (b & )
{
ans = (ans * base) % mod;
}
base = (base * base) % mod;
b >>= ;
}
return ans;
}
ll anser = ;
ll sum = ;
ll ok(ll x)
{
return (x % mod + mod) % mod;
}
int main()
{
ll n, a, b, k;
cin >> n >> a >> b >> k;
string str;
cin >> str;
ll now = ;
for (ll i = ; i < k; i++)
{
now = (qpow(a, n - i) * qpow(b, i)) % mod;
if (str[i] == '-')
{
sum = (sum - now + mod) % mod;
}
else
{
sum = (sum + now) % mod;
}
}
ll q = (qpow(b, k) * qpow(qpow(a, mod - ), k)) % mod;
if (q == )
{
cout << (sum * (n + ) / k) % mod << endl;
return ;
}
ll maxn = max(a, b);
ll minn = min(a, b);
ll chu1 = qpow(qpow(a, n + - k), mod - );
ll chu2 = qpow(qpow(maxn, k) - qpow(minn, k), mod - );
chu2 = ok(chu2);
ll beichu = (qpow(maxn, n + ) - qpow(minn, n + )) % mod;
beichu = ok(beichu);
anser = (((((sum * beichu) % mod) * chu1) % mod) * chu2) % mod;
anser = ok(anser);
cout << anser << endl;
}
D
先从叶子节点开始删起 因为如果删除了父亲节点的话 子节点如果是偶数就变成奇数可能会永远删不掉
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = ;
vector<int> tree[];
stack<int> q;
int du[];
int father[];
int visit[];
queue<int> anser;
queue<int> que;
int root;
void dfs(int x, int pre)
{
father[x] = pre;
int len = tree[x].size();
for (int i = ; i < len; i++)
{
int to = tree[x][i];
if (to == pre)
{
continue;
}
dfs(to, x);
}
}
void doit(int x)
{
anser.push(x);
visit[x] = ;
int len = tree[x].size();
for (int i = ; i < len; i++)
{
int to = tree[x][i];
du[to]--;
if (visit[to] || father[x] == to)
{
continue;
}
if (du[to] % == )
{
doit(to);
}
}
}
int main()
{
int n;
cin >> n;
for (int i = ; i <= n; i++)
{
int now;
cin >> now;
if (now == )
{
root = i;
continue;
}
tree[i].push_back(now);
tree[now].push_back(i);
du[i]++, du[now]++;
}
if ((n - ) % )
{
cout << "NO" << endl;
return ;
}
dfs(root, );
que.push(root);
while (!que.empty())
{
int x = que.front();
que.pop();
q.push(x);
int len = tree[x].size();
for (int i = ; i < len; i++)
{
int to = tree[x][i];
if (to == father[x])
{
continue;
}
que.push(to);
}
}
while (!q.empty())
{
if (du[q.top()] % == )
{
doit(q.top());
}
q.pop();
}
if (anser.size() != n)
{
cout << "NO" << endl;
return ;
}
cout << "YES" << endl;
while (!anser.empty())
{
cout << anser.front() << endl;
anser.pop();
}
}
Codeforces 964 等比数列逆元处理 贪心删偶数度节点的更多相关文章
- Codeforces 346C Number Transformation II 贪心(复杂度计算)
题意及思路:https://www.cnblogs.com/liuzhanshan/p/6560499.html 这个做法的复杂度看似是O(n ^ 2),实际上均摊是O(n)的.我们考虑两种极端数据: ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
- Codeforces 963E Alternating Sum 等比数列+逆元
题目大意: 看一下样例就明白了 基本思路: 题目中明确提到k为一个周期,稍作思考,把k项看作一项,然后发现这是个等比数列,q=(b/a)^k, 然后重点就是怎样处理等比数列求和表达式中的除法,这个时候 ...
- Codeforces 798D Mike and distribution - 贪心
Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...
- Sorted Adjacent Differences(CodeForces - 1339B)【思维+贪心】
B - Sorted Adjacent Differences(CodeForces - 1339B) 题目链接 算法 思维+贪心 时间复杂度O(nlogn) 1.这道题的题意主要就是让你对一个数组进 ...
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- Codeforces Gym 100269E Energy Tycoon 贪心
题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 ...
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...
随机推荐
- IDEA项目追踪快捷键
1.查看某个方法在哪里被调用: 在方法上右键选择FindUsages: 快捷键,在方法上Ctrl+G 2.从Ctroller方法直接跳过接口找到实现类方法: 在方法上右键:选择GoTo>Impl ...
- nodejs之express中间件body-parser使用
1.安装express和body-parser npm install express npm install body-parser 2‘.案例如下 var express = require('e ...
- 简单方法让input date支持placeholder(包含ios手机端方法)
<input class="baixin-quan-info-box-time" type="text" onfocus="(this.type ...
- CMake下,某些选项的后调整
编译安卓NDK库时,发现在R15的NDK编译出来的库,总是带了-g选项,导致附带调试,文件过大. 搜索一番后,结论是NDK的文件中有问题: https://github.com/android/ndk ...
- 函数对象的apply()和call()方法
每个函数都包含两个非继承而来的方法:apply()和call().这两个方法的用途都是在特定的作用域中调用函数,特定的作用域为this参数指定的对象. apply()和call()真正强大的地方是能够 ...
- Asp.Net Webform 常用代码
1015.ASP.Net WebForm 数据绑定 < %# %> < %= %> < % %> < %@ %> Eval("Nam ...
- TensorFlow学习笔记0-安装TensorFlow环境
TensorFlow学习笔记0-安装TensorFlow环境 作者: YunYuan 转载请注明来源,谢谢! 写在前面 系统: Windows Enterprise 10 x64 CPU:Intel( ...
- 【OpenCV开发】imread和imwrite的类型以及第三个参数关于图片压缩质量等
本片参考博客:http://blog.csdn.net/poem_qianmo/article/details/20537737 基于OpenCV3.0,与原博客有出入. 在OpenCV1.0时代,基 ...
- Akka系列(八):Akka persistence设计理念之CQRS
前言........ 这一篇文章主要是讲解Akka persistence的核心设计理念,也是CQRS(Command Query Responsibility Segregation)架构设计的典型 ...
- [转帖]56核Xeon Platinum 9200现身 - 英特尔有史以来最大的CPU封装
56核Xeon Platinum 9200现身 - 英特尔有史以来最大的CPU封装 https://www.cnbeta.com/articles/tech/835271.htm 当英特尔宣布上周正式 ...