A. Even Substrings
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a string s=s1s2…sns=s1s2…sn of length nn, which only contains digits 11, 22, ..., 99.
A substring s[l…r]s[l…r] of ss is a string slsl+1sl+2…srslsl+1sl+2…sr. A substring s[l…r]s[l…r] of ss is called even if the number represented by it is even.
Find the number of even substrings of ss. Note, that even if some substrings are equal as strings, but have different ll and rr, they are counted as different substrings.
Input
The first line contains an integer nn (1≤n≤650001≤n≤65000) — the length of the string ss.
The second line contains a string ss of length nn. The string ss consists only of digits 11, 22, ..., 99.
Output
Print the number of even substrings of ss.
Examples
input
Copy
4 1234
output
Copy
6
input
Copy
4 2244
output
Copy
10
Note
In the first example, the [l,r][l,r] pairs corresponding to even substrings are:
In the second example, all 1010 substrings of ss are even substrings. Note, that while substrings s[1…1]s[1…1] and s[2…2]s[2…2] both define the substring "2", they are still counted as different substrings.
题意看懂不好说,直接说写法吧。遍历,经过一个数就odd++统计,遇到偶数,当前偶数可以之前的数匹配,所以ans+=odd
#include
using namespace std;
const int N=65000+10;
char s[N];
int n,a[N];
int main()
{
cin>>n>>s+1;
for(int i=1;i<=n;i++) a[i]=s[i]-'0';
int odd=0,ans=0;
for(int i=1;i<=n;i++)
{
odd++;
if(a[i]%2==0) ans+=odd;
}
cout<
B. Chocolates
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You went to the store, selling nn types of chocolates. There are aiai chocolates of type ii in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy xixi chocolates of type ii (clearly, 0≤xi≤ai0≤xi≤ai), then for all 1≤j For example, the array x=[0,0,1,2,10]x=[0,0,1,2,10] satisfies the requirement above (assuming that all ai≥xiai≥xi), while arrays x=[0,1,0]x=[0,1,0], x=[5,5]x=[5,5] and x=[3,2]x=[3,2] don't. Calculate the maximum number of chocolates you can buy. Input The first line contains an integer nn (1≤n≤2⋅1051≤n≤2⋅105), denoting the number of types of chocolate. The next line contains nn integers aiai (1≤ai≤1091≤ai≤109), denoting the number of chocolates of each type. Output Print the maximum number of chocolates you can buy. Examples input Copy output Copy input Copy output Copy input Copy output Copy Note In the first example, it is optimal to buy: 0+0+1+3+60+0+1+3+6 chocolates. In the second example, it is optimal to buy: 1+2+3+4+101+2+3+4+10 chocolates. In the third example, it is optimal to buy: 0+0+0+10+0+0+1 chocolates. 就是构造 从后依次递减的最大数即可 C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a tree (a connected undirected graph without cycles) of nn vertices. Each of the n−1n−1 edges of the tree is colored in either black or red. You are also given an integer kk. Consider sequences of kk vertices. Let's call a sequence [a1,a2,…,ak][a1,a2,…,ak] good if it satisfies the following criterion: Consider the tree on the picture. If k=3k=3 then the following sequences are good: [1,4,7][1,4,7], [5,5,3][5,5,3] and [2,3,7][2,3,7]. The following sequences are not good: [1,4,6][1,4,6], [5,5,5][5,5,5], [3,7,3][3,7,3]. There are nknk sequences of vertices, count how many of them are good. Since this number can be quite large, print it modulo 109+7109+7. Input The first line contains two integers nn and kk (2≤n≤1052≤n≤105, 2≤k≤1002≤k≤100), the size of the tree and the length of the vertex sequence. Each of the next n−1n−1 lines contains three integers uiui, vivi and xixi (1≤ui,vi≤n1≤ui,vi≤n, xi∈{0,1}xi∈{0,1}), where uiui and vivi denote the endpoints of the corresponding edge and xixi is the color of this edge (00 denotes red edge and 11 denotes black edge). Output Print the number of good sequences modulo 109+7109+7. Examples input Copy output Copy input Copy output Copy input Copy output Copy Note In the first example, all sequences (4444) of length 44 except the following are good: In the second example, all edges are red, hence there aren't any good sequences. 这个题真的有意思。不管给出多少点,能构成路的所有情况就是n^k个。刚开始想叉了,想着把黑边的某个联通块点数m求出来,然后答案是m^k,但是该联通块还可以与红联通块组成路径,然后就一直卡着不知道怎么写了,耗费了近一个小时发现,可以反过来做,把红边的某个联通块点数m求出来,然后答案不就是n^k-m^k-剩下的红点
5
1 2 1 3 6
10
5
3 2 5 4 10
20
4
1 1 1 1
1
#include
4 4
1 2 1
2 3 1
3 4 1
252
4 6
1 2 0
1 3 0
1 4 0
0
3 5
1 2 1
2 3 0
210
#include