题目1 : Ctrl-C Ctrl-V
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Yor are bored. So you open a notepad and keep pressing letter 'A' to type a lot of 'A's into the text area.
Suddenly an idea come out. If you can do the following 4 kinds of operations N times, how many 'A's you can type into the text area?
- A: Type a letter A into the text area
- Ctrl-A: Select all the letters in the text area
- Ctrl-C: Copy the selected letters into clipboard
- Ctrl-V: Paste the letters from clipboard into the text area
Assume N=7 you can get 9 'A's by doing following operations: A, A, A, Ctrl-A, Ctrl-C, Ctrl-V, Ctrl-V.
输入
An integer N. (1 <= N <= 1000000000)
输出
The maximum number of 'A's you can type into the text area. Note the answer may be very large so output the answer modulo 1000000007.
样例输入
7
样例输出
9
首先,对于最优方案,Ctrl-A + Ctrl-C + 若干个Ctrl-V一定是连在一起的。
例如 Ctrl-A + A + Ctrl-C + Ctrl-V,一定不如把A放在Ctrl-A之前,即: A + Ctrl-A + Ctrl-C + Ctrl-V
Ctrl-A + Ctrl-C + A + Ctrl-V 一定不如 A + Ctrl-A + Ctrl-C + Ctrl-V。
我们用f[i]表示i个操作最多能输出多少个A。对于最优解,最后一个操作一定是A或者Ctrl-V。
如果最后一个操作是A,则f[i] = f[i-1] + 1;如果最后一个操作是Ctrl-V,我们假设最后一共有连续K个Ctrl-V,则f[i] = f[i-2-K] * (K+1)。
于是我们可以得到一个O(N^2)的DP算法。
当然本题数据范围很大,O(N^2)的DP没办法通过所有的数据。不过我们可以用这个算法得到前若干项的值。例如N=1..25的答案如下:
1 1
2 2
3 3
4 4
5 5
6 6
7 9
8 12
9 16
10 20
11 27
12 36
13 48
14 64
15 81
16 108
17 144
18 192
19 256
20 324
21 432
22 576
23 768
24 1024
25 1296
可以看出,对于N >= 16,f[N] = f[N - 5] * 4。换句话说,当N足够大的时候,最优的策略就是用Ctrl-A + Ctrl-C + Ctrl-V + Ctrl-V + Ctrl-V 5个操作把长度变成4倍。
import java.util.Scanner;
public class Main {
class Node {
int use;
int count;
int paste;
Node(int use, int count, int paste) {
this.use = use;
this.count = count;
this.paste = paste;
}
}
long mod = 1000000007;
long[] a = new long[100];
int[] last = new int[a.length];
long pow(long x, int y) {
if (y == 0) return 1;
if (y == 1) return x;
long z = pow(x, y / 2);
if (y % 2 == 0) {
return z * z % mod;
} else {
return z * z * x % mod;
}
}
long solve(int x) {
if (x <= 16) return a[x];
int power = (x - 11) / 5;
int which = (x - 11) % 5;
long ans = a[11 + which] * pow(4, power) % mod;
return ans;
}
Main() {
Scanner cin = new Scanner(System.in);
int x = cin.nextInt();
for (int i = 0; i < 6; i++) {
a[i] = i;
}
for (int i = 6; i <= 16; i++) {
for (int j = 1; j < i - 2; j++) {
long now = a[j] * (i - 1 - j);
if (now > a[i]) {
a[i] = now;
last[i] = j;
}
}
}
System.out.println(solve(x));
}
public static void main(String[] args) {
new Main();
}
}