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的末尾 ...
随机推荐
- docker-compose部署zk和kafka
version: '3.4' services: zk1: image: zookeeper restart: always hostname: zk1 container_name: zk1 por ...
- 阶段3 2.Spring_07.银行转账案例_2 案例中添加转账方法并演示事务问题
使用xmlioc这个项目进行完善. 创建一个新的工程把之前的代码都复制过来. 复制pom.xml内的依赖项 java下的com包复制过来. 配置文件复制过来 测试类固执过来 内容进行删减 测试类的方法 ...
- 【HANA系列】SAP HANA SLT在表中隐藏字段并传入HANA的方法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SLT在表中隐 ...
- 二叉平衡树AVL的插入与删除(java实现)
二叉平衡树 全图基础解释参考链接:http://btechsmartclass.com/data_structures/avl-trees.html 二叉平衡树:https://www.cnblogs ...
- Centos7搭建Open-ldap
OpenLDAP是轻型目录访问协议(Lightweight Directory Access Protocol,LDAP)的自由和开源的实现,可用于实现统一认证 一.安装环境 安装方式:yum 系统: ...
- poj1220(短除法实现任意进制转换)
题目链接:https://vjudge.net/problem/POJ-1220 题意:给定a进制的大数s,将其转换为b进制.其中2<=a,b<=62. 题意:一般进制转换是以10进制为中 ...
- [转帖]站点部署,IIS配置优化指南
站点部署,IIS配置优化指南 https://www.cnblogs.com/heyuquan/p/deploy-iis-set-performance-guide.html 挺值得学习的 毕竟之前很 ...
- linux操作系统安装运行Redis
Redis是c语言开发的. 安装redis需要c语言的编译环境.如果没有gcc需要在线安装.yum install gcc-c++ 安装步骤: 1.安装gcc yum install gcc ...
- 【一个蒟蒻的挣扎】单源最短路(Dijkstra)
赛前没啥时间好好解释了,还有三天2019CSP,大家加油啊!!! ヾ(◍°∇°◍)ノ゙ 背掉它就好啦!!! 我觉得我这一版打得还行就放上来了 #include<cstdio> #inclu ...
- gRPC编译教程
windows平台的编译 一.编译openssl ① 安装perl(可以使用ActivePerl),执行perl Configure VC-WIN64A no-asm .在这里解释一下参数含义,VC- ...