Codeforces Round #447 (Div. 2) ABCDE

A. QAQ
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

"QAQ" is a word to denote an expression of crying. Imagine "Q" as eyes with tears and "A" as a mouth.

Now Diamond has given Bort a string consisting of only uppercase English letters of length n. There is a great number of "QAQ" in the string (Diamond is so cute!).

illustration by 猫屋 https://twitter.com/nekoyaliu

Bort wants to know how many subsequences "QAQ" are in the string Diamond has given. Note that the letters "QAQ" don't have to be consecutive, but the order of letters should be exact.

Input

The only line contains a string of length n (1 ≤ n ≤ 100). It's guaranteed that the string only contains uppercase English letters.

Output

Print a single integer — the number of subsequences "QAQ" in the string.

Examples
input
QAQAQYSYIOIWIN
output
4
input
QAQQQZZYNOIWIN
output
3
Note

In the first example there are 4 subsequences "QAQ": "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN".



A题,水。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=105,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
char s[maxn];

int main() {
	int i,len,j,k;
	scanf("%s",s);
	len=strlen(s);
	int ans=0;
	for (i=0;i<len;i++) {
		for (j=i+1;j<len;j++) {
			for (k=j+1;k<len;k++) {
				if (s[i]=='Q'&&s[k]=='Q'&&s[j]=='A') ans++;
			}
		}
	}
	cout << ans;
	return 0;
}

B. Ralph And His Magic Field
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Ralph has a magic field which is divided into n × m blocks. That is to say, there are n rows and m columns on the field. Ralph can put an integer in each block. However, the magic field doesn't always work properly. It works only if the product of integers in each row and each column equals to k, where k is either 1 or -1.

Now Ralph wants you to figure out the number of ways to put numbers in each block in such a way that the magic field works properly. Two ways are considered different if and only if there exists at least one block where the numbers in the first way and in the second way are different. You are asked to output the answer modulo 1000000007 = 109 + 7.

Note that there is no range of the numbers to put in the blocks, but we can prove that the answer is not infinity.

Input

The only line contains three integers nm and k (1 ≤ n, m ≤ 1018k is either 1 or -1).

Output

Print a single number denoting the answer modulo 1000000007.

Examples
input
1 1 -1
output
1
input
1 3 1
output
1
input
3 3 -1
output
16
Note

In the first example the only way is to put -1 into the only block.

In the second example the only way is to put 1 into every block.




B题,打表推出公式,答案为(2^(n-1))^m-1
当k=-1且n+m为奇数时,答案为0
打表代码:
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=105,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
int a[maxn][maxn];
int ans,N,M;

void dfs(int x,int y) {
	if (x==N+1) {
		int sum;
		for (int i=1;i<=N;i++) {
			sum=1;
			for (int j=1;j<=M;j++) 
				sum*=a[i][j];
			if (sum!=1) return;
		}
		for (int i=1;i<=M;i++) {
			sum=1;
			for (int j=1;j<=N;j++) 
				sum*=a[j][i];
			if (sum!=1) return;
		}
		ans++;
/*		for (int i=1;i<=N;i++) {
			for (int j=1;j<=M;j++) 
				cout << a[i][j] << ' ';
			cout << '\n';
		}
		cout << '\n';*/
	} else {
		if (y==M) {
			a[x][y]=1,dfs(x+1,1);a[x][y]=-1,dfs(x+1,1);
		} else {
			a[x][y]=1,dfs(x,1+y);a[x][y]=-1,dfs(x,1+y);
		}
	}
}

int main() {
	int n,m,k;
	cin >> N >> M;
	ans=0;dfs(1,1);
	cout << ans;
	return 0;
}

AC代码:
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f,mod=1e9+7;   
const ld pi=acos(-1.0L);

ll fastpower(ll base,ll index) {
	ll ans,now;
	if (index<0) return 1;
	ans=1;
	now=base;
	ll k=index;
	while (k) {
		if (k%2) ans=ans*now;
		ans%=mod;
		now=now*now;
		now%=mod;
		k/=2;
	}
	return ans;
}

int main() {
	ll n,m,ans,k;
	scanf("%I64d%I64d%I64d",&n,&m,&k);
	if (n==1&&m==1) {
		printf("1\n");return 0;
	} else if (k==-1&&(n+m)%2==1) {
		printf("0\n");return 0;
	}
	ans=fastpower(2,n-1);
	ans=fastpower(ans,m-1);
	ans%=mod;
	printf("%I64d",ans);
	return 0;
}

C. Marco and GCD Sequence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In a dream Marco met an elderly man with a pair of black glasses. The man told him the key to immortality and then disappeared with the wind of time.

When he woke up, he only remembered that the key was a sequence of positive integers of some length n, but forgot the exact sequence. Let the elements of the sequence be a1, a2, ..., an. He remembered that he calculated gcd(ai, ai + 1, ..., aj) for every 1 ≤ i ≤ j ≤ n and put it into a set Sgcd here means the greatest common divisor.

Note that even if a number is put into the set S twice or more, it only appears once in the set.

Now Marco gives you the set S and asks you to help him figure out the initial sequence. If there are many solutions, print any of them. It is also possible that there are no sequences that produce the set S, in this case print -1.

Input

The first line contains a single integer m (1 ≤ m ≤ 1000) — the size of the set S.

The second line contains m integers s1, s2, ..., sm (1 ≤ si ≤ 106) — the elements of the set S. It's guaranteed that the elements of the set are given in strictly increasing order, that means s1 < s2 < ... < sm.

Output

If there is no solution, print a single line containing -1.

Otherwise, in the first line print a single integer n denoting the length of the sequence, n should not exceed 4000.

In the second line print n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the sequence.

We can show that if a solution exists, then there is a solution with n not exceeding 4000 and ai not exceeding 106.

If there are multiple solutions, print any of them.

Examples
input
4
2 4 6 12
output
3
4 6 12
input
2
2 3
output
-1
Note

In the first example 2 = gcd(4, 6), the other elements from the set appear in the sequence, and we can show that there are no values different from 246 and 12 among gcd(ai, ai + 1, ..., aj) for every 1 ≤ i ≤ j ≤ n.




C题构造题。
给出一个序列,求出序列每个子区间的gcd并把它们全部放入一个新的集合。现在给出这个集合,让你求出任意一种初始的序列。

如果给出序列的所有数的gcd在集合中没有出现过,则无解。否则,就把这个共同的gcd插入到原集合每个数字的两边。最终序列长度为2*n+1.

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=5005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
int a[maxn];
bool f[1000005];

ll gcd(ll x,ll y) {
	if (x<y) swap(x,y);
	ll z;
	while (y) {
		z=x%y;
		x=y;
		y=z;
	}
	return x;
}

int main() {
	int n,ans,i,j,p;
	scanf("%d",&n);mem0(f);
	for (i=1;i<=n;i++) scanf("%d",&a[i]),f[a[i]]=1;
	if (a[1]==1) {
		printf("%d\n",2*n);
		for (i=1;i<=n;i++) printf("%d %d ",1,a[i]);
	} else {
		p=a[1];
		for (i=2;i<=n;i++) p=gcd(p,a[i]);
		if (!f[p]) {
			printf("-1");return 0;
		} else {
			printf("%d\n%d ",2*n+1,p);
			for (i=1;i<=n;i++) printf("%d %d ",a[i],p);
		}
	}
	return 0;
}

D. Ralph And His Tour in Binary Country
time limit per test
2.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Ralph is in the Binary Country. The Binary Country consists of n cities and (n - 1) bidirectional roads connecting the cities. The roads are numbered from 1 to (n - 1), the i-th road connects the city labeled  (here ⌊ x denotes the x rounded down to the nearest integer) and the city labeled (i + 1), and the length of the i-th road is Li.

Now Ralph gives you m queries. In each query he tells you some city Ai and an integer Hi. He wants to make some tours starting from this city. He can choose any city in the Binary Country (including Ai) as the terminal city for a tour. He gains happiness (Hi - L) during a tour, where L is the distance between the city Ai and the terminal city.

Ralph is interested in tours from Ai in which he can gain positive happiness. For each query, compute the sum of happiness gains for all such tours.

Ralph will never take the same tour twice or more (in one query), he will never pass the same city twice or more in one tour.

Input

The first line contains two integers n and m (1 ≤ n ≤ 1061 ≤ m ≤ 105).

(n - 1) lines follow, each line contains one integer Li (1 ≤ Li ≤ 105), which denotes the length of the i-th road.

m lines follow, each line contains two integers Ai and Hi (1 ≤ Ai ≤ n0 ≤ Hi ≤ 107).

Output

Print m lines, on the i-th line print one integer — the answer for the i-th query.

Examples
input
2 2
5
1 8
2 4
output
11
4
input
6 4
2
1
1
3
2
2 4
1 3
3 2
1 7
output
11
6
3
28
Note

Here is the explanation for the second sample.

Ralph's first query is to start tours from city 2 and Hi equals to 4. Here are the options:

  • He can choose city 5 as his terminal city. Since the distance between city 5 and city 2 is 3, he can gain happiness 4 - 3 = 1.
  • He can choose city 4 as his terminal city and gain happiness 3.
  • He can choose city 1 as his terminal city and gain happiness 2.
  • He can choose city 3 as his terminal city and gain happiness 1.
  • Note that Ralph can choose city 2 as his terminal city and gain happiness 4.
  • Ralph won't choose city 6 as his terminal city because the distance between city 6 and city 2 is 5, which leads to negative happiness for Ralph.

So the answer for the first query is 1 + 3 + 2 + 1 + 4 = 11.




给定一棵完全二叉树,对每个询问i,Hi,求所有距离 i 点在Hi以内的点 j 的sigma(Hi-Dij)


利用完全二叉树的性质:层数最多为logn层。

先预处理每个点的每个子树节点到自己的距离,并排序。

查询的时候,从 i 号点开始一层层向上爬,在每一层统计多出来的节点对答案的贡献。

由于在完全二叉树向上爬的过程中,当前节点总有一个儿子已经被统计过,所以只要统计当前节点和它未统计到的儿子的子树对答案的贡献。而对于未统计到的儿子的子树,我们可以利用预处理的结果,lower_bound求出符合要求的点数有多少个,再利用前缀和统计答案。

时间复杂度O(nlogn+mlogn)


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=1000005,maxm=100005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
ll dist[maxn],ans[maxm];
vector<ll> d[maxn],qd[maxn],id[maxn],sum[maxn];
int num=0;

int main() {
	int n,m,i,j,x;
	ll y;
	scanf("%d%d",&n,&m);
	dist[1]=0;
	for (i=2;i<=n;i++) scanf("%I64d",&dist[i]);
	for (i=1;i<=m;i++) {
		scanf("%d%I64d",&x,&y);
		qd[x].push_back(y);
		id[x].push_back(i);
	}
	d[1].push_back(0);
	for (i=2;i<=n;i++) {
		int now;ll pre;
		now=i;pre=0;
		while (now) {
			d[now].push_back(pre);
			pre+=dist[now];now/=2;
		}
	}
	for (i=1;i<=n;i++) {
		sort(d[i].begin(),d[i].end());
		sum[i].push_back(d[i][0]);
		for (j=1;j<d[i].size();j++) 
			sum[i].push_back(sum[i][j-1]+d[i][j]);
	}
	mem0(ans);
	for (i=1;i<=n;i++) {
		for (j=0;j<qd[i].size();j++) {
			int now=i,last,q;ll rem=qd[i][j];
			ll pos=(ll)(lower_bound(d[now].begin(),d[now].end(),rem)-d[now].begin());
			if (pos!=0)
				ans[id[i][j]]+=rem*pos-sum[now][pos-1];
			last=i;rem-=dist[now];now/=2;
			while (now) {
				if (rem>=0) ans[id[i][j]]+=rem;
				if (last==now*2) q=now*2+1; else q=now*2;
				if (q<=n) {
					ll pos=(ll)(lower_bound(d[q].begin(),d[q].end(),rem-dist[q])-d[q].begin());
					if (pos!=0)
						ans[id[i][j]]+=(rem-dist[q])*pos-sum[q][pos-1];
				}
				rem-=dist[now];last=now;now/=2;
			}
		}
	}
	for (i=1;i<=m;i++) printf("%I64d\n",ans[i]);
	return 0;
}

E. Ralph and Mushrooms
time limit per test
2.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Ralph is going to collect mushrooms in the Mushroom Forest.

There are m directed paths connecting n trees in the Mushroom Forest. On each path grow some mushrooms. When Ralph passes a path, he collects all the mushrooms on the path. The Mushroom Forest has a magical fertile ground where mushrooms grow at a fantastic speed. New mushrooms regrow as soon as Ralph finishes mushroom collection on a path. More specifically, after Ralph passes a path the i-th time, there regrow i mushrooms less than there was before this pass. That is, if there is initially xmushrooms on a path, then Ralph will collect x mushrooms for the first time, x - 1 mushrooms the second time, x - 1 - 2 mushrooms the third time, and so on. However, the number of mushrooms can never be less than 0.

For example, let there be 9 mushrooms on a path initially. The number of mushrooms that can be collected from the path is 986 and 3 when Ralph passes by from first to fourth time. From the fifth time and later Ralph can't collect any mushrooms from the path (but still can pass it).

Ralph decided to start from the tree s. How many mushrooms can he collect using only described paths?

Input

The first line contains two integers n and m (1 ≤ n ≤ 1060 ≤ m ≤ 106), representing the number of trees and the number of directed paths in the Mushroom Forest, respectively.

Each of the following m lines contains three integers xy and w (1 ≤ x, y ≤ n0 ≤ w ≤ 108), denoting a path that leads from tree x to tree y with w mushrooms initially. There can be paths that lead from a tree to itself, and multiple paths between the same pair of trees.

The last line contains a single integer s (1 ≤ s ≤ n) — the starting position of Ralph.

Output

Print an integer denoting the maximum number of the mushrooms Ralph can collect during his route.

Examples
input
2 2
1 2 4
2 1 4
1
output
16
input
3 3
1 2 4
2 3 3
1 3 8
1
output
8
Note

In the first sample Ralph can pass three times on the circle and collect 4 + 4 + 3 + 3 + 1 + 1 = 16mushrooms. After that there will be no mushrooms for Ralph to collect.

In the second sample, Ralph can go to tree 3 and collect 8 mushrooms on the path from tree 1 to tree 3.



给一个有向图,每条边有d个蘑菇。走某条路时,第一次可以得到d个,第二次d-1个,第三次d-1-2个。。。问从点s出发,最多得到多少蘑菇。


首先用tarjan把图缩点成一个新的DAG,再在DAG上把边反向,拓扑排序进行DP。

每个强连通分量之内的边,走多少次都没有关系。那么,计算出每条这类边最多走多少次,然后由此计算走这么多次能获得多少个蘑菇,把这个值加到新建的DAG上的点权上。这些可以预处理前缀和完成。而连接两个不同连通分量之间的边,最多只能走一次,最大收益就为这条边初始的d。

对于新图,我们按拓扑序DP就好了。因为我们已经把边反向,所以有以下DP方程:

dp[to]=max(dp[to],dp[from]+edge.dist)

注:DAG=有向无环图

这题并不难,只是写起来比较费时间


#include <cstdio>
#include <iostream>
#include <string.h>
#include <queue>
#include <algorithm>
#include <stack>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=1000005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
int head[maxn],col[maxn],dfn[maxn],low[maxn],x[maxn],y[maxn];
ll d[maxn],sum[maxn],su[maxn],dp[maxn],val[maxn];
bool inst[maxn];
stack<int> st;
int num=0,cnum=0;

struct Edge {
	int from,to,pre;
	ll dist;
};
Edge edge[maxn*2];

void addedge(int from,int to,ll dist) {
	edge[num]=(Edge){from,to,head[from],dist};
	head[from]=num++;
}

void tarjan(int now) {
	num++;
	dfn[now]=low[now]=num;
	inst[now]=1;
	st.push(now);
	for (int i=head[now];i!=-1;i=edge[i].pre) {
		int to=edge[i].to;
		if (!dfn[to]) {
			tarjan(to);
			low[now]=min(low[now],low[to]);
		}
		else if (inst[to]) 
		    low[now]=min(low[now],dfn[to]); 
	}
	if (dfn[now]==low[now]) {
		inst[now]=0;
		col[now]=++cnum;
		while (st.top()!=now) {
			col[st.top()]=cnum;
			inst[st.top()]=0;
			st.pop();
		}
		st.pop();
	}
}

int main() {
	int n,m,i,j,s,cnt=0;
	memset(head,-1,sizeof(head));
	scanf("%d%d",&n,&m);
	su[0]=sum[0]=0;
	for (i=1;su[i-1]<=1e8;i++) {
		cnt++;
		su[i]=su[i-1]+i;
		sum[i]=sum[i-1]+su[i];
	}
	for (i=1;i<=m;i++) {
		scanf("%d%d%I64d",&x[i],&y[i],&d[i]);
		addedge(x[i],y[i],d[i]);
	}
	scanf("%d",&s);
	num=0;
	for (i=1;i<=n;i++) {
		if (!dfn[i]) tarjan(i);
	}
	memset(head,-1,sizeof(head));
	num=0;mem0(dfn);
	ll hk;
	for (i=1;i<=m;i++) {
		if (col[x[i]]==col[y[i]]) {
			ll pos=(ll)(lower_bound(su,su+cnt+1,d[i])-su);
			if (pos<=0) hk=0; else hk=pos*d[i]-sum[pos-1];
			val[col[x[i]]]+=hk;
		} else {
			addedge(col[y[i]],col[x[i]],d[i]);
			dfn[col[x[i]]]++;
		}
	}
	queue<int> q;
	mem0(inst);
	for (i=1;i<=cnum;i++) {
		if (dfn[i]==0) q.push(i),inst[i]=1;
	}
	while (!q.empty()) {
		int now=q.front();
		q.pop();
		dp[now]+=val[now];
		for (i=head[now];i!=-1;i=edge[i].pre) {
			int to=edge[i].to;
			dfn[to]--;
			if (dfn[to]==0) {
				q.push(to);inst[to]=1;
			}
			dp[to]=max(dp[to],dp[now]+edge[i].dist);
		}
	}
	printf("%I64d\n",dp[col[s]]);
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值