You are on page 1of 18

A.

Dreamoon and Sums


time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Dreamoon loves summing up something for no reason. One day he obtains two
integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is
called nice if and , where k is someinteger number in range [1,a].
By we denote the quotient of integer division of x and y. By we denote
the remainder of integer division of x and y. You can read more about these operations
here: http://goo.gl/AcsXhT.
The answer may be large, so please print its remainder modulo 1000000007 (10
9
+7). Can you compute
it faster than Dreamoon?
Input
The single line of the input contains two integers a, b (1a,b10
7
).
Output
Print a single integer representing the answer modulo 1000000007 (10
9
+7).
Sample test(s)
input
1 1
output
0
input
2 2
output
8
Note
For the first sample, there are no nice integers because is always zero.
For the second sample, the set of nice integers is {3,5}.

Andrewsta petr
a, b = map(int, input().split())
r1 = (b - 1) * b // 2
r2 = (a * b + 1 + b + 1) * a // 2
print((r1 * r2) % 1000000007)

class TaskA {
static final long MODULO = (long) (1e9 +
7);

public void solve(int testNumber,
InputReader in, PrintWriter out) {
long a = in.nextLong();
long b = in.nextLong();
long res = a * ((b * (b - 1)) / 2
% MODULO) % MODULO;
res += (a * (a + 1) / 2) % MODULO
* ((b * (b - 1)) / 2 % MODULO) % MODULO *
b % MODULO;
res %= MODULO;
out.println(res);
}
}



B. Dreamoon and Sets
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Dreamoon likes to play with sets, integers and . is defined as the largest positive integer that
divides both a andb.
Let S be a set of exactly four distinct integers greater than 0. Define S to be of rank k if and only if for all pairs
of distinct elements s
i
, s
j
from S, .
Given k and n, Dreamoon wants to make up n sets of rank k using integers from 1 to m such that no integer
is used in two different sets (of course you can leave some integers without use). Calculate the
minimum m that makes it possible and print one possible solution.
Input
The single line of the input contains two space separated integers n, k (1n10000,1k100).
Output
On the first line print a single integer the minimal possible m.
On each of the next n lines print four space separated integers representing the i-th set.
Neither the order of the sets nor the order of integers within a set is important. If there are multiple possible
solutions with minimal m, print any one of them.
Sample test(s)
input
1 1
output
5
1 2 3 5
input
2 2
output
22
2 4 6 22
14 18 10 16
Note
For the first example it's easy to see that set {1,2,3,4} isn't a valid set of rank 1 since
.

Tankenginer
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int n, k;

int main() {
scanf("%d%d", &n, &k);
printf("%d\n", (6 * n - 1) * k);
int i = 1;
while (n--) {
printf("%d %d %d %d\n", i * k, (i
+ 1) * k, (i + 2) * k, (i + 4) * k);
i += 6;
}
return 0;
}




C. Dreamoon and Strings
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Dreamoon has a string s and a pattern string p. He first removes exactly x characters from s obtaining
string s' as a result. Then he calculates that is defined as the maximal number of non-overlapping
substrings equal to p that can be found in s'. He wants to make this number as big as possible.
More formally, let's define as maximum value of over all s' that can be obtained by
removing exactly xcharacters from s. Dreamoon wants to know for
all x from 0 to |s| where |s| denotes the length of string s.
Input
The first line of the input contains the string s (1|s|2000).
The second line of the input contains the string p (1|p|500).
Both strings will only consist of lower case English letters.
Output
Print |s|+1 space-separated integers in a single line representing the for all x from 0 to |s|.
Sample test(s)
input
aaaaa
aa
output
2 2 1 1 0 0
input
axbaxxb
ab
output
0 1 1 2 1 1 0 0
Note
For the first sample, the corresponding optimal values of s' after removal 0 through |s|=5 characters
from s are {"aaaaa","aaaa", "aaa", "aa", "a", ""}.
For the second sample, possible corresponding optimal values of s' are
{"axbaxxb", "abaxxb", "axbab", "abab","aba", "ab", "a", ""}.

#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 2005;

int n, m;
char s[N], p[N];

int to[N];

int f[N][N];

int main() {
scanf("%s%s", s, p);
n = strlen(s), m = strlen(p);
for (int i = 0; i < n; ++i) {
to[i] = n + 1;
int k = 0;
for (int j = i; j < n; ++j) {
if (s[j] == p[k]) {
++k;
if (k == m) {
to[i] = j + 1;
break;
}
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= i; ++j) {
f[i + 1][j] = max(f[i + 1][j], f[i][j]);
f[i + 1][j + 1] = max(f[i + 1][j + 1], f[i][j]);
if (to[i] != n + 1) {
int nx = j + to[i] - i - m;
f[to[i]][nx] = max(f[to[i]][nx], f[i][j] + 1);
}
}
}
for (int i = 0; i <= n; ++i) {
printf("%d%c", f[n][i], i == n ? '\n' : ' ');
}
return 0;
}
Petr
class TaskC {
static final int INF = (int) 1e9;

public void solve(int testNumber, InputReader in, PrintWriter out) {
String s = in.next();
String p = in.next();
int plen = p.length();
int[] len = new int[s.length()];
Arrays.fill(len, INF);
for (int i = 0; i + p.length() <= s.length(); ++i) {
int l = 0;
for (int j = 0; j < p.length(); ++j) {
while (i + l < s.length() && s.charAt(i + l) != p.charAt(j)) ++l;
if (i + l >= s.length()) {
l = INF;
break;
}
++l;
}
len[i] = l;
}
int[][] best = new int[s.length() + 1][s.length() + 1];
for (int[] x : best) Arrays.fill(x, INF);
best[s.length()][0] = 0;
for (int i = s.length() - 1; i >= 0; --i) {
for (int j = 0; j <= s.length(); ++j) {
best[i][j] = best[i + 1][j];
if (j > 0 && len[i] < INF)
best[i][j] = Math.min(best[i][j], best[i + len[i]][j - 1] +
len[i] - plen);
}
}
int[] res = new int[s.length() + 1];
for (int take = 1; take <= s.length(); ++take) {
int minDelete = best[0][take];
int maxDelete = s.length() - take * plen;
for (int x = minDelete; x <= maxDelete; ++x)
res[x] = take;
}
for (int i = 0; i < res.length; ++i) {
if (i > 0) out.print(' ');
out.print(res[i]);
}
out.println();
}
}

Hongloid
#include<bits/stdc++.h>
#define REP(i,m) for(int i=0;i<(m);++i)
#define REPN(i,m,in) for(int i=(in);i<(m);++i)
#define ALL(t) (t).begin(),(t).end()
#define CLR(a) memset((a),0,sizeof(a))
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

using namespace std;


#ifndef ONLINE_JUDGE
#define dump(x) cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
#else
#define dump(x) ;
#define prl ;
template<class T> void debug(T a,T b){ ;}
#endif

template<class T> void chmin(T& a,const T& b) { if(a>b) a=b; }
template<class T> void chmax(T& a,const T& b) { if(a<b) a=b; }
typedef long long int lint;
typedef pair<int,int> pi;

namespace std{
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
out<<'('<<a.fr<<','<<a.sc<<')';
return out;
}
}

lint readL(){
lint res;
#ifdef ONLINE_JUDGE
scanf("%I64d",&res);
#else
scanf("%lld",&res);
#endif
return res;
}
void printL(lint res){
#ifdef ONLINE_JUDGE
printf("%I64d",res);
#else
printf("%lld",res);
#endif
}

const int INF=5e8;

char s[2005],p[505];
int n,m;

int nxtpos[2005];

int dp[2005][2005];
int main(){
scanf("%s%s",s,p);
n=strlen(s);
m=strlen(p);

REP(i,n){
int k=0;
nxtpos[i]=INF;
REPN(j,n,i){
if(s[j]==p[k]) ++k;
if(k==m) {
nxtpos[i]=j+1;
break;
}
}
}

memset(dp,-1,sizeof(dp));

dp[0][0]=0;
REP(i,n) REP(j,n+1) if(dp[i][j]>=0){
int nxt=nxtpos[i];
int ers=nxt-i-m;

if(nxt<INF){
chmax(dp[nxt][j+ers],dp[i][j]+1);
}
chmax(dp[i+1][j],dp[i][j]);
chmax(dp[i+1][j+1],dp[i][j]);
}
REP(i,n+1){
printf("%d%c",dp[n][i],i==n?'\n':' ');
}

return 0;
}


D. Dreamoon and Binary
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
Dreamoon saw a large integer x written on the ground and wants to print its binary form out. Dreamoon has
accomplished the part of turning x into its binary format. Now he is going to print it in the following manner.
He has an integer n=0 and can only perform the following two operations in any order for unlimited times
each:
1. Print n in binary form without leading zeros, each print will append to the right of previous prints.
2. Increase n by 1.
Let's define an ideal sequence as a sequence of operations that can successfully print binary representation
of x without leading zeros and ends with a print operation (i.e. operation 1). Dreamoon wants to know how
many different ideal sequences are there and the length (in operations) of the shortest ideal sequence.
The answers might be large so please print them modulo 1000000007 (10
9
+7).
Let's define the string representation of an ideal sequence as a string of '1' and '2' where the i-th
character in the string matches the i-th operation performed. Two ideal sequences are called different if their
string representations are different.
Input
The single line of the input contains a binary integer representing x (1x<2
5000
) without leading zeros.
Output
The first line of the output should contain an integer representing the number of different ideal sequences
modulo 1000000007 (10
9
+7).
The second line of the output contains an integer representing the minimal length of an ideal sequence
modulo 1000000007 (10
9
+7).
Sample test(s)
input
101
output
1
6
input
11010
output
3
5
Note
For the first sample, the shortest and the only ideal sequence is 222221 of length 6.
For the second sample, there are three ideal sequences 21211, 212222222221,
222222222222222222222222221. Among them the shortest one has length 5.
Petr
class TaskD {
static final int INF = (int) 1e9;
static final int MODULO = (int) (1e9 + 7);

int[][] numEqual;
int n;
String x;

int compare(int p1, int p2, int len) {
if (numEqual[p1][p2] >= len) return 0;
int diff = numEqual[p1][p2];
return x.charAt(p1 + diff) - x.charAt(p2 + diff);
}

public void solve(int testNumber, InputReader in, PrintWriter out) {
x = in.next();
n = x.length();
numEqual = new int[n + 1][n + 1];
for (int p1 = n - 1; p1 >= 0; --p1) {
for (int p2 = p1; p2 >= 0; --p2) {
if (x.charAt(p1) == x.charAt(p2)) {
numEqual[p1][p2] = 1 + numEqual[p1 + 1][p2 + 1];
}
numEqual[p2][p1] = numEqual[p1][p2];
}
}
int[][] ways = new int[n + 1][n + 1];
int[][] minOutputs = new int[n + 1][n + 1];
Arrays.fill(ways[0], 1);
Arrays.fill(minOutputs[0], 0);
int[] minAnsOutputs = new int[n + 1];
Arrays.fill(minAnsOutputs, INF);
for (int len = 1; len <= n; ++len) {
Arrays.fill(minOutputs[len], INF);
for (int lastAtMost = 1; lastAtMost <= len; ++lastAtMost) {
ways[len][lastAtMost] = ways[len][lastAtMost - 1];
minOutputs[len][lastAtMost] = minOutputs[len][lastAtMost - 1];
if (x.charAt(len - lastAtMost) == '1') {
int prevAtMost = lastAtMost;
prevAtMost = Math.min(prevAtMost, len - lastAtMost);
if (prevAtMost == lastAtMost && compare(len - 2 * lastAtMost, len
- lastAtMost, lastAtMost) > 0) {
--prevAtMost;
}
int pways = ways[len - lastAtMost][prevAtMost];
if (pways != 0) {
int nways = ways[len][lastAtMost] + pways;
if (nways >= MODULO) nways -= MODULO;
ways[len][lastAtMost] = nways;
}
if (len == n) {
minAnsOutputs[lastAtMost] = 1 + minOutputs[len -
lastAtMost][prevAtMost];
} else {
minOutputs[len][lastAtMost] =
Math.min(minOutputs[len][lastAtMost], 1 + minOutputs[len - lastAtMost][prevAtMost]);
}
}
}
}
out.println(ways[n][n]);
BigInteger res = null;
for (int lastlen = 1; lastlen <= n; ++lastlen) {
if (res != null && res.bitLength() < lastlen - 1) {
break;
}
if (minAnsOutputs[lastlen] >= INF) continue;
BigInteger cur = new BigInteger(x.substring(n - lastlen),
2).add(BigInteger.valueOf(minAnsOutputs[lastlen]));
if (res == null || cur.compareTo(res) < 0) {
res = cur;
}
}
out.println(res.mod(BigInteger.valueOf(MODULO)));
}
}


E. Dreamoon and Notepad
time limit per test
3.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Dreamoon has just created a document of hard problems using notepad.exe. The document consists
of n lines of text, a
i
denotes the length of the i-th line. He now wants to know what is the fastest way to move
the cursor around because the document is really long.
Let (r,c) be a current cursor position, where r is row number and c is position of cursor in the row. We
have 1rn and 0ca
r
.
We can use following six operations in notepad.exe to move our cursor assuming the current cursor position
is at (r,c):
1. up key: the new cursor position (nr,nc)=(max(r-1,1),min(a
nr
,c))
2. down key: the new cursor position (nr,nc)=(min(r+1,n),min(a
nr
,c))
3. left key: the new cursor position (nr,nc)=(r,max(0,c-1))
4. right key: the new cursor position (nr,nc)=(r,min(a
nr
,c+1))
5. HOME key: the new cursor position (nr,nc)=(r,0)
6. END key: the new cursor position (nr,nc)=(r,a
r
)
You're given the document description (n and sequence a
i
) and q queries from Dreamoon. Each query asks
what minimal number of key presses is needed to move the cursor from (r
1
,c
1
) to (r
2
,c
2
).
Input
The first line contains an integer n(1n400,000) the number of lines of text.
The second line contains n integers a
1
,a
2
,...,a
n
(1a
i
10
8
).
The third line contains an integer q(1q400,000).
Each of the next q lines contains four integers r
1
,c
1
,r
2
,c
2
representing a query
(1r
1
,r
2
n,0c
1
a
r1
,0c
2
a
r2
).
Output
For each query print the result of the query.
Sample test(s)
input
9
1 3 5 3 1 3 5 3 1
4
3 5 3 1
3 3 7 3
1 0 3 3
6 0 7 3
output
2
5
3
2
input
2
10 5
1
1 0 1 5
output
3
Note
In the first sample, the first query can be solved with keys: HOME, right.
The second query can be solved with keys: down, down, down, END, down.
The third query can be solved with keys: down, END, down.
The fourth query can be solved with keys: END, down.

y drazil, 46 minutes ago, ,
Definitions used in this editorial:
stands for ceiling function.
stands for floor function.
stands for real division.
For positive integer a, b, div(a,b) stands for integral division, .
For positive integer a, b, mod(a,b) stands for module operation, a=div(a,b)*b+mod(a,b).
length(string) is the length of string.
For non-negative integer ab, A[a..b] stands for the set of A[a],A[a+1],A[a+2]... A[b-1] when A is
an array and the substring of A consists of a
th
to (b-1)
th
character(inclusive) of A when A is a string. For
such substring we havelength(A[a..b])=b-a.
C(a,b) stands for the combination function, the ways of selecting a elements from a group of b elements.
476A - Dreamoon and Stairs
We can show that the maximum number of moves possible is n and minimal moves needed is , so the
problem equals to determine the minimal integer that is a multiple of m in the range .
One way to find the minimal number which is a multiple of m and greater than or equal to a number x
is , we can compare this number to the upper bound n to determine if there is a valid solution.
Although best practice is O(1), O(n) enumeration of each possible number of moves would also work.
time complexity: O(1)
sample code: 8212169
476B - Dreamoon and WiFi
The order of moves won't change the final position, so we can move all '?'s to the end of the string.
We have the following information:
1. the correct final position
2. the position that Dreamoon will be before all '?'s
3. the number of '?'s
We can infer that the distance and direction dreamoon still needs to move in the '?' part from 1. and 2., and
furthur translate that to how many +1s and -1s dreamoon will need to move.
What's left is a combinatorial problem, the probability would be .
So we can compute that formula within O(n) time assuming n is the length of commands, but since N is small
so we can brute force every possible choice of '?' with some recursive or dfs like search in O(2
n
) time
complexity.
Note that the problem asks for a precision of 10
-9
, so one should output to 11 decimal places or more.
time complexity: O(n)
sample code: 8212470
476C - Dreamoon and Sums / 477A - Dreamoon and Sums
If we fix the value of k, and let d=div(x,b), m=mod(x,b), we have :
d=mk
x=db+m
So we have x=mkb+m=(kb+1)*m.
And we know m would be in range [0,b-1] because it's a remainder, so the sum of x of that fixed k would
be .
Next we should notice that if an integer x is nice it can only be nice for a single particular
Unable to parse markup [type=CF_TEX]
uniquely defines div(x,b) and mod(x,b).
Thus the final answer would be sum up for all individual k which can be calculated
in O(a) and will pass the time limit of 1.5 seconds.
Also the formula above can be expanded to . Dreamoon says he's too lazy to do this
part, so if you useO(1) solution you just computed the answer faster than Dreamoon!!!
Note that no matter which approach one should be very careful of overflowing of the integer data type of the
used language.
time complexity: O(1)
sample code:

You might also like