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全局配置
进入全局设置界面: 取消每次启动IDEA就默认打开上一次最后关闭的项目 编译器代码字体设置: 控制台字体大小和颜色设置 同一个文件代码里面的各个不同方法之间显示分割线 代码自动提示不区分大小写 格式化 ...
- vim命令行模式常见快捷方式
普通模式下的快捷键 快捷键 说明 i insert, 在光标所在处输入 I 在当前光标所在行的行首输入 a append, 在光标所在处后面输入 A 在当前光标所在行的行尾输入 o 在当前光标所在行的 ...
- windows触摸板速度调整
Windows 触摸板滚动速度调整: 在注册表中: The magic key is: Computer\HKEY_CURRENT_USER\Software\Microsoft\Wisp\Tou ...
- PS 之图片中抠出大树
工具:Photoshop CC2017 原图:(目的是将大树从图片中抠出) 操作: 1.打开要抠图的图片,然后执行:[图层]---->[新建调整图层]---->[反相]---->[在 ...
- MySQL 树形结构 根据指定节点 获取其所在全路径节点序列
背景说明 需求:MySQL树形结构, 根据指定的节点,获取其所在全路径节点序列. 问题分析 1.可以使用类似Java这种面向对象的语言,对节点集合进行逻辑处理,获取全路径节点序列. 2.直接自定义My ...
- C++统计程序运行时间代码片段
因为经常需要统计代码的运行时间,所以计时功能就显得很重要, 记录一下现在喜欢用的计时方式,供日后查阅. 1.下面是计时主函数, bool TimeStaticMine(int id,const cha ...
- python学习之内置函数(一)
4.7 内置函数 4.7.1 内置函数(1) eval 执行字符串类型的代码,并返回最终结果. eval('2 + 2') # 4 n=81 eval("n + 4") # 85 ...
- 主机加固之windows2003
这篇与上一篇的win7主机加固内容大体类似,部分有些不同.这篇也可以用来尝试加固windows XP. 1. 配置管理 1.1用户策略 注意:在对Windows系统加固之前先新建一个临时的系统管理员账 ...
- Mac022-brew安装tool
1.Mac上查看目录的树结构 Step1:安装tree命令 brew install tree Step2:某一目录下执行tree,会将该目录下的所有目录以树形结构显示 $ tree $ tree - ...
- hdoj4507(数位dp)
题目链接:https://vjudge.net/problem/HDU-4507 题意:定义如果一个整数符合下面3个条件之一,那么我们就说这个整数和7有关—— 1.整数中某一位是7: 2.整数的每一位 ...