You received a notebook which is called Death Note. This notebook has infinite number of pages. A rule is written on the last page (huh) of this notebook. It says: "You have to write names in this notebook during nn consecutive days. During the ii-th day you have to write exactly aiai names.". You got scared (of course you got scared, who wouldn't get scared if he just receive a notebook which is named Death Notewith a some strange rule written in it?).
Of course, you decided to follow this rule. When you calmed down, you came up with a strategy how you will write names in the notebook. You have calculated that each page of the notebook can contain exactly mm names. You will start writing names from the first page. You will write names on the current page as long as the limit on the number of names on this page is not exceeded. When the current page is over, you turn the page. Note that you always turn the page when it ends, it doesn't matter if it is the last day or not. If after some day the current page still can hold at least one name, during the next day you will continue writing the names from the current page.
Now you are interested in the following question: how many times will you turn the page during each day? You are interested in the number of pages you will turn each day from 11 to nn.
The first line of the input contains two integers nn, mm (1≤n≤2⋅1051≤n≤2⋅105, 1≤m≤1091≤m≤109) — the number of days you will write names in the notebook and the number of names which can be written on each page of the notebook.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai means the number of names you will write in the notebook during the ii-th day.
Print exactly nn integers t1,t2,…,tnt1,t2,…,tn, where titi is the number of times you will turn the page during the ii-th day.
3 5
3 7 9
0 2 1
4 20
10 9 19 2
0 0 1 1
1 100
99
0
In the first example pages of the Death Note will look like this [1,1,1,2,2],[2,2,2,2,2],[3,3,3,3,3],[3,3,3,3][1,1,1,2,2],[2,2,2,2,2],[3,3,3,3,3],[3,3,3,3]. Each number of the array describes during which day name on the corresponding position will be written. It is easy to see that you should turn the first and the second page during the second day and the third page during the third day.
签到。、
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 1e6+10; 5 int a[N]; 6 int main() { 7 int n, m; 8 cin >> n >> m; 9 for(int i = 1; i <= n; i ++) cin >> a[i]; 10 int ans = 0; 11 for(int i = 1; i <= n; i ++) { 12 ans += a[i]; 13 printf("%d ",ans/m); 14 ans %= m; 15 } 16 return 0; 17 }
B. Segment Occurrences
You are given two strings ss and tt, both consisting only of lowercase Latin letters.
The substring s[l..r]s[l..r] is the string which is obtained by taking characters sl,sl+1,…,srsl,sl+1,…,sr without changing the order.
Each of the occurrences of string aa in a string bb is a position ii (1≤i≤|b|−|a|+11≤i≤|b|−|a|+1) such that b[i..i+|a|−1]=ab[i..i+|a|−1]=a (|a||a| is the length of string aa).
You are asked qq queries: for the ii-th query you are required to calculate the number of occurrences of string tt in a substring s[li..ri]s[li..ri].
The first line contains three integer numbers nn, mm and qq (1≤n,m≤1031≤n,m≤103, 1≤q≤1051≤q≤105) — the length of string ss, the length of string ttand the number of queries, respectively.
The second line is a string ss (|s|=n|s|=n), consisting only of lowercase Latin letters.
The third line is a string tt (|t|=m|t|=m), consisting only of lowercase Latin letters.
Each of the next qq lines contains two integer numbers lili and riri (1≤li≤ri≤n1≤li≤ri≤n) — the arguments for the ii-th query.
Print qq lines — the ii-th line should contain the answer to the ii-th query, that is the number of occurrences of string tt in a substring s[li..ri]s[li..ri].
10 3 4
codeforces
for
1 3
3 10
5 6
5 7
0
1
0
1
15 2 3
abacabadabacaba
ba
1 15
3 4
2 14
4
0
3
3 5 2
aaa
baaab
1 3
1 1
0
0
In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
被坑了
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 1010; 5 int n, m, q, l, r; 6 char s[N], t[N]; 7 int a[N], cnt, sum[N]; 8 int main() { 9 cin >> n >> m >> q; 10 cin >> s >> t; 11 int tmp = 0; 12 for(int i = 0; i < n; i ++) { 13 if(s[i] == t[0]) { 14 int cnt = 0; 15 for(int j = 0; j < m; j ++) { 16 if(s[i+j] == t[j]) cnt++; 17 else break; 18 } 19 if(cnt == m) tmp += 1; 20 } 21 sum[i+1] = tmp; 22 } 23 // for(int i = 1; i <= n;i ++) printf("%d %d ",i,sum[i]); 24 while(q--) { 25 cin >> l >> r; 26 r = r-m+1; 27 if(l > r) printf("0 "); 28 else printf("%d ",sum[r]-sum[l-1]); 29 } 30 return 0; 31 }
C. Vasya And The Mushrooms
Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into n consecutive cells. For each cell Vasya knows how fast the mushrooms grow in this cell (more formally, how many grams of mushrooms grow in this cell each minute). Vasya spends exactly one minute to move to some adjacent cell. Vasya cannot leave the glade. Two cells are considered adjacent if they share a common side. When Vasya enters some cell, he instantly collects all the mushrooms growing there.
Vasya begins his journey in the left upper cell. Every minute Vasya must move to some adjacent cell, he cannot wait for the mushrooms to grow. He wants to visit all the cells exactly once and maximize the total weight of the collected mushrooms. Initially, all mushrooms have a weight of 0. Note that Vasya doesn't need to return to the starting cell.
Help Vasya! Calculate the maximum total weight of mushrooms he can collect.
The first line contains the number n (1 ≤ n ≤ 3·105) — the length of the glade.
The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 106) — the growth rate of mushrooms in the first row of the glade.
The third line contains n numbers b1, b2, ..., bn (1 ≤ bi ≤ 106) is the growth rate of mushrooms in the second row of the glade.
Output one number — the maximum total weight of mushrooms that Vasya can collect by choosing the optimal route. Pay attention that Vasya must visit every cell of the glade exactly once.
3
1 2 3
6 5 4
70
3
1 1000 10000
10 100 100000
543210
In the first test case, the optimal route is as follows:
In the second test case, the optimal route is as follows:
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 3e5+10; 5 ll a[2][N], s[N], sum[2][N]; 6 int main() { 7 int n; 8 cin >> n; 9 for(int i = 1; i <= n; i ++) scanf("%lld", &a[0][i]); 10 for(int i = 1; i <= n; i ++) scanf("%lld", &a[1][i]); 11 for(int i = 1; i <= n; i ++) s[i] = s[i-1] + a[0][i] + a[1][i]; 12 sum[0][n] = a[1][n]; 13 sum[1][n] = a[0][n]; 14 for(int i = n-1; i >= 1; i --) { 15 sum[0][i] = sum[0][i+1] + s[n] - s[i] + ((n-i+1)*2-1)*a[1][i]; 16 sum[1][i] = sum[1][i+1] + s[n] - s[i] + ((n-i+1)*2-1)*a[0][i]; 17 } 18 // for(int i = 0; i < 2; i ++) { 19 // for(int j = 1; j <= n; j ++) printf("%d ", sum[i][j]); 20 // printf(" "); 21 // } 22 ll ans = sum[0][1], cnt = 0; 23 for(int i = 1; i <= n; i ++) { 24 if(i&1) { 25 cnt += (a[0][i]*(2*i-2)+a[1][i]*(2*i-1)); 26 ans = max(ans, cnt+sum[1][i+1]+(s[n]-s[i])*(2*i)); 27 // printf("%d %d %d %d ",i,cnt,sum[1][i] ); 28 } else { 29 cnt += (a[0][i]*(2*i-1)+a[1][i]*(2*i-2)); 30 ans = max(ans, cnt+sum[0][i+1]+(s[n]-s[i])*(2*i)); 31 } 32 } 33 cout << ans << endl; 34 return 0; 35 }
D. Vasya And The Matrix
Now Vasya is taking an exam in mathematics. In order to get a good mark, Vasya needs to guess the matrix that the teacher has constructed!
Vasya knows that the matrix consists of n rows and m columns. For each row, he knows the xor (bitwise excluding or) of the elements in this row. The sequence a1, a2, ..., an denotes the xor of elements in rows with indices 1, 2, ..., n, respectively. Similarly, for each column, he knows the xor of the elements in this column. The sequence b1, b2, ..., bm denotes the xor of elements in columns with indices 1, 2, ..., m, respectively.
Help Vasya! Find a matrix satisfying the given constraints or tell him that there is no suitable matrix.
The first line contains two numbers n and m (2 ≤ n, m ≤ 100) — the dimensions of the matrix.
The second line contains n numbers a1, a2, ..., an (0 ≤ ai ≤ 109), where ai is the xor of all elements in row i.
The third line contains m numbers b1, b2, ..., bm (0 ≤ bi ≤ 109), where bi is the xor of all elements in column i.
If there is no matrix satisfying the given constraints in the first line, output "NO".
Otherwise, on the first line output "YES", and then n rows of m numbers in each ci1, ci2, ... , cim (0 ≤ cij ≤ 2·109) — the description of the matrix.
If there are several suitable matrices, it is allowed to print any of them.
2 3
2 9
5 3 13
YES
3 4 5
6 7 8
3 3
1 7 6
2 15 12
NO
很巧妙。
最改变第一行第一个=列的数字,然后,然后填充第一行和第一列的数字,其余全是0.
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 110; 5 ll n, m; 6 ll x[N], y[N]; 7 ll a[N][N]; 8 int main() { 9 cin >> n >> m; 10 ll ansx = 0; 11 for(int i = 1; i <= n; i ++) { 12 cin >> x[i]; 13 ansx ^= x[i]; 14 } 15 ll ansy = 0; 16 for(int i = 1; i <= m; i ++) cin >> y[i], ansy ^= y[i]; 17 if(ansy == ansx) printf("YES "); 18 else return 0*printf("NO "); 19 ansx ^= x[1]; 20 ansx ^= y[1]; 21 a[1][1] = ansx; 22 for(int i = 2; i <= m; i ++) a[1][i] = y[i]; 23 for(int i = 2; i <= n; i ++) a[i][1] = x[i]; 24 for(int i = 1; i <= n; i ++) { 25 for(int j = 1 ; j <= m; j ++) { 26 printf("%d ",a[i][j]); 27 }printf(" "); 28 } 29 return 0; 30 }