Codeforces 1229B Kamil and Making a Stream

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 1229B Kamil and Making a Stream相关的知识,希望对你有一定的参考价值。

\\(\\gcd\\) 一个性质:对于正整数 \\(x\\), 重复 \\(x\\leftarrow \\gcd(x, i)\\)\\(i\\ge 0\\))直到 \\(x = 1\\)\\(x\\) 出现的值个数上限为 \\(\\log_2(x)+1\\)
证明:考虑到 \\(x\\) 是逐渐变小,则在 \\(x\\) 变小的情况下,对于 \\(x = \\prod_i=1^k p_i^c_i\\)\\(p_i\\ \\operatorname为质数\\))中的 \\(c_i\\) 至少有 \\(1\\)\\(c_i\\leftarrow m\\)\\(m < c_i, p_i^m+1\\not\\mid i\\)),则 \\(1 + \\sum_i+1^k c_i\\)(注意还有 \\(x = 1\\) 的情况)即为取值数量上限,不难证出上限即为 \\(\\log_2(x) + 1\\)

考虑对于节点 \\(u\\)\\(u\\leftarrow fa_u\\)\\(sum\\leftarrow \\gcd(sum, v_fa_u)\\)\\(sum = v_u\\))这个操作,\\(sum\\) 个数上限为 \\(\\log_2(sum) + 1\\)
那就可以直接用一个 map \\(cnt_u\\) 存储 \\(sum\\) 出现的值和出现次数,对于 \\(u\\) 就可以直接从 \\(cnt_fa_u\\) 推出 \\(cnt_u\\)
时间复杂度 \\(O(n\\log^2_2 n)\\)

#include<bits/stdc++.h>
using namespace std;
#define int64 long long
const int64 mod = 1e9 + 7;
const int N = 100000 + 20;
struct Line 
	int v, nxt;
	#define v(i) l[i].v
	#define nxt(i) l[i].nxt
 l[N * 2];
int head[N], lcnt;
void add(int u, int v) 
	l[++lcnt] = v, head[u]; head[u] = lcnt;
	return ;

int64 v[N];
map<int64, int> cnt[N];
int64 ans;
void dfs(int u, int fa) 
	cnt[u][v[u]]++;
	for (map<int64, int>::iterator it = cnt[fa].begin(); it != cnt[fa].end(); it++) 
		cnt[u][__gcd((*it).first, v[u])] += (*it).second;
	
	for (map<int64, int>::iterator it = cnt[u].begin(); it != cnt[u].end(); it++) 
		ans = (ans + (*it).first * (int64)((*it).second) % mod) % mod;
	
	for (int i = head[u]; i; i = nxt(i)) 
		if (v(i) == fa) 
			continue;
		
		dfs(v(i), u);
	
	return ;

int main() 
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) 
		scanf("%lld", &v[i]);
	
	for (int i = 1; i < n; i++) 
		int u, v;
		scanf("%d%d", &u, &v);
		add(u, v); add(v, u);
	
	dfs(1, 0);
	printf("%lld", ans);
	return 0;

Codeforces Round #370 (Div. 2) B

Description

Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion:

  • An ‘L‘ indicates he should move one unit left.
  • An ‘R‘ indicates he should move one unit right.
  • A ‘U‘ indicates he should move one unit up.
  • A ‘D‘ indicates he should move one unit down.

But now Memory wants to end at the origin. To do this, he has a special trident. This trident can replace any character in s with any of ‘L‘, ‘R‘, ‘U‘, or ‘D‘. However, because he doesn‘t want to wear out the trident, he wants to make the minimum number of edits possible. Please tell Memory what is the minimum number of changes he needs to make to produce a string that, when walked, will end at the origin, or if there is no such string.

Input

The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.

Output

If there is a string satisfying the conditions, output a single integer — the minimum number of edits required. In case it‘s not possible to change the sequence in such a way that it will bring Memory to to the origin, output -1.

Examples
input
RRU
output
-1
input
UDUR
output
1
input
RUUR
output
2
Note

In the first sample test, Memory is told to walk right, then right, then up. It is easy to see that it is impossible to edit these instructions to form a valid walk.

In the second sample test, Memory is told to walk up, then down, then up, then right. One possible solution is to change s to "LDUR". This string uses 1 edit, which is the minimum possible. It also ends at the origin.

题意:给你一些方向,可以修改其中的字符(改变方向),让起点和终点相同,当然修改次数最小,不行就是-1

解法:首先奇数是不可能返回的,然后要让起点和终点相同,只要左右上下出现次数相同就好了,那么修改的也就是左右 和 上下 缺少的次数

#include<bits/stdc++.h>
using namespace std;
int MAX=100005;
struct P
{
    int x;int y;
}He[1000005];
map<char,int>q;
int main()
{
    string s;
    cin>>s;
    if(s.length()%2)
    {
        cout<<"-1"<<endl;
    }
    else
    {
        for(int i=0;i<s.length();i++)
        {
            q[s[i]]++;
        }
        cout<<(abs(q[‘L‘]-q[‘R‘])+abs(q[‘U‘]-q[‘D‘]))/2<<endl;
    }
    return 0;
}

  

以上是关于Codeforces 1229B Kamil and Making a Stream的主要内容,如果未能解决你的问题,请参考以下文章

Kamil and Making a Stream

CodeForces 55D

E. Kamil and Making a Stream 区间gcd

Codeforces Round #643 (Div. 2)

Codeforces 854 B Maxim Buys an Apartment 思维 水题

Codeforces 1247C. p-binary