Save Energy And Lock Puzzle
因为太久没有更新博客了,最近比较忙。。所以就把两周签EL-ROUND-TWO我AC的两道题简单的讲讲,以后很多更新可能都直接贴一个github的账号了quq
pppppkun · GitHub
↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
A – Save Energy
Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after kminutes after turning on.
During cooking, Julia goes to the kitchen every d minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The stove switches on and off instantly.
It is known that the chicken needs t minutes to be cooked on the stove, if it is turned on, and 2t minutes, if it is turned off. You need to find out, how much time will Julia have to cook the chicken, if it is considered that the chicken is cooked evenly, with constant speed when the stove is turned on and at a constant speed when it is turned off.
Input
The single line contains three integers k, d and t (1 ≤ k, d, t ≤ 1018).
Output
Print a single number, the total time of cooking in minutes. The relative or absolute error must not exceed 10 - 9.
Namely, let's assume that your answer is x and the answer of the jury is y. The checker program will consider your answer correct if
image.png.
Examples
Input
3 2 6
Output
6.5
Input
4 2 20
Output
20.0
Note
In the first example, the chicken will be cooked for 3 minutes on the turned on stove, after this it will be cooked for image.png. Then the chicken will be cooked for one minute on a turned off stove, it will be cooked for image.png
. Thus, after four minutes the chicken will be cooked for image.png
. Before the fifth minute Julia will turn on the stove and after 2.5 minutes the chicken will be ready image.png
.
In the second example, when the stove is turned off, Julia will immediately turn it on, so the stove will always be turned on and the chicken will be cooked in 20 minutes.
首先先注意到m和n都超级大,1 ≤ k, d ≤ 1018,所以我们需要用long long 来保存。考虑到m和n之间的关系,有三种情况
n|m
此时一直都是高温加热的状态,所以时间就直接是t了
n<m或者n>m
此时,因为每n次都会去开一次烤炉(如果关上了的话),所以我们可以考虑当烤炉灭了的时间和n之间的关系,如果存在一个最小的数k,使得kn>m,那么我们就称一个周期是kn,这个周期所做出的贡献是(kn-m)*1/2 + m
后面就很简单了,下面看第二题。
C-Lock Puzzle
Welcome to another task about breaking the code lock! Explorers Whitfield and Martin came across an unusual safe, inside of which, according to rumors, there are untold riches, among which one can find the solution of the problem of discrete logarithm!
Of course, there is a code lock is installed on the safe. The lock has a screen that displays a string of n lowercase Latin letters. Initially, the screen displays string s. Whitfield and Martin found out that the safe will open when string t will be displayed on the screen.
The string on the screen can be changed using the operation «shiftx». In order to apply this operation, explorers choose an integer xfrom 0 to n inclusive. After that, the current string p = αβ changes to βRα, where the length of β is x, and the length of α is n - x. In other words, the suffix of the length x of string p is reversed and moved to the beginning of the string. For example, after the operation «shift 4» the string «abcacb» will be changed with string «bcacab », since α = ab, β = cacb, βR = bcac.
Explorers are afraid that if they apply too many operations «shift», the lock will be locked forever. They ask you to find a way to get the string t on the screen, using no more than 6100 operations.
Input
The first line contains an integer n, the length of the strings s and t (1 ≤ n ≤ 2 000).
After that, there are two strings s and t, consisting of n lowercase Latin letters each.
Output
If it is impossible to get string t from string s using no more than 6100 operations «shift», print a single number - 1.
Otherwise, in the first line output the number of operations k (0 ≤ k ≤ 6100). In the next line output k numbers xi corresponding to the operations «shift xi» (0 ≤ xi ≤ n) in the order in which they should be applied.
Examples
Input
6
abacbb
babcba
Output
4
6 3 2 3
Input
3
aba
bba
Output
-1
In the first example, after applying the operations, the string on the screen will change as follows:
-
abacbb→bbcaba
-
bbcaba→ababbc
-
ababbc→cbabab
-
cbabab→babcba
本人认为这题还是有点复杂的,因为我们此时不知道题目例子中给出的变化是不是通用的变换,第二是通用的变换如果存在的话能不能在6100步以内结束呢?注意到1 ≤ n ≤ 2 000,3*n 刚好是在6000以内的数,这可能说明了存在一个通用的变化,使得3步以内归位一个数?
经过不断尝试,我发现可以通过一下操作使得一个数从中间的某一位换到这一串数的最末尾,且不改变已经排好的那段。
A,D是任意的一个子串,B是已经排好的子串,c是B后一位的字符,用A/表示A的逆序,我们从t串的最后一位开始排,假设现在是如下状态 BDcA
BDcA →A/cD/B/
A/cD/B/→BDA/c
BDA/c→cBDA/→BDA/
这样就解决了问题,下面开始code。
首先先判断两个string字母数是不是一样的,如果不一样直接return -1
如果是,我们有一个循环,在循环中要不断地归为原来的子串,考虑到我们最后把操作的步数和怎么操作的都列出来,所以我们需要用一个东西来存每一步的操作,用一个向量就行了。最后只需要打印即可!