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的末尾 ...
随机推荐
- CPU处理多任务——中断与轮询方式比较
中断方式与轮询方式比较 中断的基本概念 程序中断通常简称中断,是指CPU在正常运行程序的过程中,由于预选安排或发生了各种随机的内部或外部事件,使CPU中断正在运行的程序,而转到为相应的服务程序去处 ...
- 权重随机算法Java实现
权重随机算法在抽奖,资源调度等系统中应用还是比较广泛的,一个简单的按照权重来随机的实现,权重为几个随机对象(分类)的命中的比例,权重设置越高命中越容易,之和可以不等于100: 简单实现代码如下: ? ...
- 【VS开发】修改MainFrame窗口名称1
在VS2010下新建一个MFC的多文档应用程序,程序默认的标题是"文档名-工程名".图标默认的是写着MFC的三个方块.但在很多软件中都不是使用的默认设置,开发者们都将标题和图标改过 ...
- vue --- vscode 配置 .vue文件生成结构
1.选择“文件 -> 首选项 -> 用户代码片段”,此时,会弹出一个搜索框,输入vue 选择vue后,编辑器会自动打开一个名字为vue.json的文件 2.复制以下内容到这个文件 ...
- Myeclipse下配置SVN报错问题 svn: E175002: java.lang.RuntimeException: Could not generate DH keypair(转)
转:http://blog.csdn.net/yulong_1988/article/details/51459936 在myeclipse下安装svn插件,出现了Could not generate ...
- java.math包简介
java.math包提供了java中的数学类 包括基本的浮点库.复杂运算以及任意精度的数据运算 '可以看得到,主要包括三个类一个枚举 BigDecimal和BigInteger接下来会详细介绍 先 ...
- 死磕并发之CountDownLatch解析
CountDownLatch解析 CountDownLatch是什么 CountDownLatch是基于AQS的阻塞工具,阻塞一个或者多个线程,直到所有的线程都执行完成. CountDownLatch ...
- HDU 1401 Solitaire 双向DFS
HDU 1401 Solitaire 双向DFS 题意 给定一个\(8*8\)的棋盘,棋盘上有4个棋子.每一步操作可以把任意一个棋子移动到它周围四个方向上的空格子上,或者可以跳过它四个方向上的棋子(就 ...
- dhcp协议简介
协议分析 - DHCP协议解码详解 DHCP协议简介 DHCP,全称是 Dynamic Host Configuration Protocol﹐中文名为动态主机配置协议,它的前身是 BOOTP,它工作 ...
- Ubuntu下软件打开时状态图标与原始图标不重合问题解决
问题描述 如下图书所示,pycharm打开时,运行的pycharm图标与原始的在收藏夹中的图标不重合.而其他软件不会这样,运行软件的图标与原始图标会重合, 解决办法 把鼠标悬浮在打开的pycharm上 ...