Codeforces #304 (div2)

来源:互联网 发布:php网店 编辑:程序博客网 时间:2024/04/29 23:33

A. Soldier and Bananas
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A soldier wants to buy w bananas in the shop. He has to pay k dollars for the first banana, 2k dollars for the second one and so on (in other words, he has to pay i·k dollars for the i-th banana).

He has n dollars. How many dollars does he have to borrow from his friend soldier to buy w bananas?

Input

The first line contains three positive integers k, n, w (1  ≤  k, w  ≤  10000 ≤ n ≤ 109), the cost of the first banana, initial number of dollars the soldier has and number of bananas he wants.

Output

Output one integer — the amount of dollars that the soldier must borrow from his friend. If he doesn't have to borrow money, output 0.

Sample test(s)
input
3 17 4
output
13
- - 水题忘了

AC代码如下:

////  Created by TaoSama on 2015-05-23//  Copyright (c) 2015 TaoSama. All rights reserved.//#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const int N = 1e5 + 10;long long n, k, w;int main() {#ifdef LOCAL    freopen("in.txt", "r", stdin);//  freopen("out.txt","w",stdout);#endif    ios_base::sync_with_stdio(0);    cin >> k >> n >> w;    long long sum = 0;    for(long long i = 1; i <= w; ++i)        sum += i * k;    cout << (sum - n < 0 ? 0 : sum - n) << endl;    return 0;}

B. Soldier and Badges
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin.

For every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors.

Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness.

Input

First line of input consists of one integer n (1 ≤ n ≤ 3000).

Next line consists of n integers ai (1 ≤ ai ≤ n), which stand for coolness factor of each badge.

Output

Output single integer — minimum amount of coins the colonel has to pay.

Sample test(s)
input
41 3 1 4
output
1
input
51 2 3 2 5
output
2
暴力枚举就好了 - - 当时二了 数组开小了 不知道咋想的 6和3的区别 - - 

AC代码如下:

////  Created by TaoSama on 2015-05-23//  Copyright (c) 2015 TaoSama. All rights reserved.//#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const int N = 1e5 + 10;int n;bool vis[6005];int main() {#ifdef LOCALfreopen("in.txt", "r", stdin);//freopen("out.txt","w",stdout);#endifios_base::sync_with_stdio(0);while(cin >> n){memset(vis, false, sizeof vis);int ans = 0;for(int i = 1; i <= n; ++i){int x; cin >> x;if(vis[x]){for(int j = 1; j <= n; ++j){if(!vis[x + j]){ans += j;vis[x + j] = true;break;}}}else vis[x] = true;}cout << ans << endl;}return 0;}

C. Soldier and Cards
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to nall values are different. They divide cards between them in some manner, it's possible that they have different number of cards. Then they play a "war"-like card game.

The rules are following. On each turn a fight happens. Each of them picks card from the top of his stack and puts on the table. The one whose card value is bigger wins this fight and takes both cards from the table to the bottom of his stack. More precisely, he first takes his opponent's card and puts to the bottom of his stack, and then he puts his card to the bottom of his stack. If after some turn one of the player's stack becomes empty, he loses and the other one wins.

You have to calculate how many fights will happen and who will win the game, or state that game won't end.

Input

First line contains a single integer n (2 ≤ n ≤ 10), the number of cards.

Second line contains integer k1 (1 ≤ k1 ≤ n - 1), the number of the first soldier's cards. Then follow k1 integers that are the values on the first soldier's cards, from top to bottom of his stack.

Third line contains integer k2 (k1 + k2 = n), the number of the second soldier's cards. Then follow k2 integers that are the values on the second soldier's cards, from top to bottom of his stack.

All card values are different.

Output

If somebody wins in this game, print 2 integers where the first one stands for the number of fights before end of game and the second one is 1 or 2 showing which player has won.

If the game won't end and will continue forever output  - 1.

Sample test(s)
input
42 1 32 4 2
output
6 2
input
31 22 1 3
output
-1
直接模拟一下就好了 - - 然后出现重复状态说明就有循环了 然后就-1就好了

AC代码如下:

////  Created by TaoSama on 2015-05-23//  Copyright (c) 2015 TaoSama. All rights reserved.//#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <deque>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const int N = 1e5 + 10;typedef pair<string, string> P;int n, k1, k2;map<P, bool> mp;int main() {#ifdef LOCAL    freopen("in.txt", "r", stdin);//  freopen("out.txt","w",stdout);#endif    ios_base::sync_with_stdio(0);    while(cin >> n) {        deque<int> l, r;        mp.clear();        cin >> k1;        string s, t;        for(int i = 1; i <= k1; ++i) {            int x; cin >> x;            l.push_back(x); s += char(x + '0');        }        cin >> k2;        for(int i = 1; i <= k2; ++i) {            int x; cin >> x;            r.push_back(x); t += char(x + '0');        }        mp[P(s, t)] = true;        bool cycle = false;        int ans = 0;        //cout<<s <<' '<<t<<endl;        while(!l.empty() && !r.empty()) {            int ll = l.front(), rr = r.front();            l.pop_front(); r.pop_front();            char cl = s[0], cr = t[0];            s.erase(0, 1); t.erase(0, 1);            if(ll < rr) {                r.push_back(ll);                r.push_back(rr);                t = t + cl + cr;            } else {                l.push_back(rr);                l.push_back(ll);                s = s + cr + cl;            }            //cout<< s << ' '<<t<<endl;            if(mp.count(P(s, t))) {                cycle = true;                break;            } else mp[P(s, t)] = true;            ++ans;        }        if(cycle) cout << "-1\n";        else {            cout << ans;            if(l.empty()) cout << " 2\n";            else cout << " 1\n";        }    }    return 0;}

D. Soldier and Number Game
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and gives it to the second soldier. Then the second one tries to make maximum possible number of rounds. Each round consists of choosing a positive integer x > 1, such that n is divisible by x and replacing n with n / x. When n becomes equal to 1 and there is no more possible valid moves the game is over and the score of the second soldier is equal to the number of rounds he performed.

To make the game more interesting, first soldier chooses n of form a! / b! for some positive integer a and b (a ≥ b). Here by k! we denote the factorial of k that is defined as a product of all positive integers not large than k.

What is the maximum possible score of the second soldier?

Input

First line of input consists of single integer t (1 ≤ t ≤ 1 000 000) denoting number of games soldiers play.

Then follow t lines, each contains pair of integers a and b (1 ≤ b ≤ a ≤ 5 000 000) defining the value of n for a game.

Output

For each game output a maximum score that the second soldier can get.

Sample test(s)
input
23 16 3
output
25
用素数筛一下所有的数  - - dp也行 做个素分解 然后 ans其实两个数素因子个数的差 要预处理下不然会T

</pre>AC代码如下:<pre name="code" class="cpp">////  Created by TaoSama on 2015-05-23//  Copyright (c) 2015 TaoSama. All rights reserved.//#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const int N = 5e6;long long fact[N];int prime[N], p;bool is[N];void seive() {    memset(is, true, sizeof is);    for(int i = 2; i <= N; ++i) {        if(is[i]) {            prime[p++] = i;            for(int j = i + i; j <= N; j += i) is[j] = false;        }    }}void init() {    seive();    for(int i = 2; i <= N; ++i) {        int t = i;        //cout<<i<<":"<<endl;        for(int j = 0; j < p; ++j) {            if(prime[j] * prime[j] > t) break;            while(t % prime[j] == 0) {                fact[i]++;                //cout<<j<<' ';                t /= prime[j];            }        }        //cout<<endl;        if(t != 1) fact[i] ++;    }    /*for(int i = 1; i <= 10; ++i)        cout << i << ' ';    cout << endl;    for(int i = 1; i <= 10; ++i)        cout << fact[i] << ' ';    cout << endl;*/    for(int i = 2; i <= N; ++i)        fact[i] += fact[i - 1];}int main() {#ifdef LOCAL    freopen("in.txt", "r", stdin);//  freopen("out.txt","w",stdout);#endif    ios_base::sync_with_stdio(0);    //time_t st = clock();    init();    int t; scanf("%d", &t);    while(t--) {        int x, y; scanf("%d%d", &x, &y);        //cout << fact[x] << ' ' << fact[y] << endl;        printf("%I64d\n", fact[x] - fact[y]);    }    //time_t ed = clock();    //printf("%.3fs\n", (ed - st + 0.0) / CLOCKS_PER_SEC);    return 0;}


0 0
原创粉丝点击