Codeforces Round #475 (Div. 2) ABC

A. Splits

Let's define a split of n as a nonincreasing sequence of positive integers, the sum of which is n.

For example, the following sequences are splits of 8: [4, 4], [3, 3, 2], [2, 2, 1, 1, 1, 1], [5, 2, 1].

The following sequences aren't splits of 8: [1, 7], [5, 4], [11,  - 3], [1, 1, 4, 1, 1].

The weight of a split is the number of elements in the split that are equal to the first element. For example, the weight of the split [1, 1, 1, 1, 1] is 5, the weight of the split [5, 5, 3, 3, 3] is 2 and the weight of the split [9] equals 1.

For a given n, find out the number of different weights of its splits.

Input

The first line contains one integer n (1 ≤ n ≤ 109).

Output

Output one integer — the answer to the problem.

Examples
input
Copy
7
output
Copy
4
input
Copy
8
output
Copy
5
input
Copy
9
output
Copy
5
Note

In the first sample, there are following possible weights of splits of 7:

Weight 1: []

Weight 2: [, 1]

Weight 3: [, 1]

Weight 7: []

 由n分割成非增序列,第一个数的数量就是他的权重,求权重的个数有多少个。数学题,n/2+1

1 #include <bits/stdc++.h>
2 using namespace std;
3 
4 int main() {
5     int n, nas = 0;
6     cin >> n;
7     cout << n/2+1 << endl;
8     return 0;
9 }

B. Messages

There are n incoming messages for Vasya. The i-th message is going to be received after ti minutes. Each message has a cost, which equals to A initially. After being received, the cost of a message decreases by B each minute (it can become negative). Vasya can read any message after receiving it at any moment of time. After reading the message, Vasya's bank account receives the current cost of this message. Initially, Vasya's bank account is at 0.

Also, each minute Vasya's bank account receives C·k, where k is the amount of received but unread messages.

Vasya's messages are very important to him, and because of that he wants to have all messages read after T minutes.

Determine the maximum amount of money Vasya's bank account can hold after T minutes.

Input

The first line contains five integers nABC and T (1 ≤ n, A, B, C, T ≤ 1000).

The second string contains n integers ti (1 ≤ ti ≤ T).

Output

Output one integer  — the answer to the problem.

Examples
input
Copy
4 5 5 3 5
1 5 5 4
output
Copy
20
input
Copy
5 3 1 1 3
2 2 2 1 1
output
Copy
15
input
Copy
5 5 3 4 5
1 2 3 4 5
output
Copy
35
Note

In the first sample the messages must be read immediately after receiving, Vasya receives A points for each message, n·A = 20 in total.

In the second sample the messages can be read at any integer moment.

In the third sample messages must be read at the moment T. This way Vasya has 1, 2, 3, 4 and 0 unread messages at the corresponding minutes, he gets 40 points for them. When reading messages, he receives (5 - 4·3) + (5 - 3·3) + (5 - 2·3) + (5 - 1·3) + 5 =  - 5 points. This is 35 in total.

 有n封消息,题 ti 分钟收到第 i 封消息,初始价格为A,每分钟价格减少B,每分钟价格加C。在T分钟之内要全部收完。

B >= C 时,就收到消息的时候就看,总价格为n*A

B < C 时,收到的消息都不看,等到T分钟看。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int a[1010];
 4 int main() {
 5     int n, A, b, c, t;
 6     cin >> n >> A >> b >> c >> t;
 7     for(int i = 1; i <= n; i ++) cin >> a[i];
 8     if(b >= c) {
 9         printf("%d\n",n*A);
10     } else {
11         int ans = 0;
12         for(int i = 1; i <= n; i ++) {
13             ans += (t-a[i])*(c-b)+A;
14         }
15         printf("%d\n",ans);
16     }
17     return 0;
18 }

C. Alternating Sum

Examples
input
Copy
2 2 3 3
+-+
output
Copy
7
input
Copy
4 1 5 1
-
output
Copy
999999228

,第 i 个字符为‘+’ 或‘-’ 代表了si 为 1 还是 -1 。当K小于n时,其它的可以按周期性得到,即(n+1)%k == 0,数学题目。

所以是个等比公式,等比公式为 S = a0(1-qn)/(1-q),q 为 (b/a)k ,a0为0-k-1个数的叠加。

q为1时,就让a0乘以(n+1)/k,否则 a0(1-q(n+1)/k)/(1-q)  需要用逆元来算。

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N = 1e5+10;
 5 const int mod = 1e9+9;
 6 char str[N];
 7 ll n, a, b, k, ans;
 8 ll pow_mod(ll x, ll n){  
 9     ll ans=1;  
10     while(n>0){  
11         if(n&1)ans=ans*x%mod;  
12         x=x*x%mod;  
13         n>>=1;  
14     }  
15     return ans;  
16 }
17 int main() {
18     cin >> n >> a >> b >> k;
19     cin >> str;
20     for(int i = 0; i < k; i ++) {
21         if(str[i] == '-') {
22             ans = (ans - pow_mod(a,n-i) * pow_mod(b, i)%mod+mod)%mod;
23         } else {
24             ans = (ans + pow_mod(a,n-i) * pow_mod(b, i)%mod)%mod;
25         }
26     }
27     ll t = pow_mod(b, k) % mod * pow_mod(a, k*(mod-2))%mod;
28     if(t == 1) {
29         ans = ans * ((n+1)/k) % mod;
30     } else {
31         ans = ans * (1 - pow_mod(t, (n+1)/k))% mod * pow_mod(1 - t, mod-2) % mod;
32     }
33     cout << ans << endl;
34     return 0;
35 }

猜你喜欢

转载自www.cnblogs.com/xingkongyihao/p/8876780.html