Codeforces Round 206 DIV 2

A. Vasya and Digital Root
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya has recently found out what a digital root of a number is and he decided to share his knowledge with you.

Let's assume that S(n) is the sum of digits of number n, for example, S(4098) = 4 + 0 + 9 + 8 = 21. Then the digital root of number nequals to:

  1. dr(n) = S(n), if S(n) < 10;
  2. dr(n) = dr( S(n) ), if S(n) ≥ 10.

For example, dr(4098)  =  dr(21)  =  3.

Vasya is afraid of large numbers, so the numbers he works with are at most 101000. For all such numbers, he has proved that dr(n)  =  S( S( S( S(n) ) ) ) (n ≤ 101000).

Now Vasya wants to quickly find numbers with the given digital root. The problem is, he hasn't learned how to do that and he asked you to help him. You task is, given numbers k and d, find the number consisting of exactly k digits (the leading zeroes are not allowed), with digital root equal to d, or else state that such number does not exist.

Input

The first line contains two integers k and d (1 ≤ k ≤ 1000; 0 ≤ d ≤ 9).

Output

In a single line print either any number that meets the requirements (without the leading zeroes) or "No solution" (without the quotes), if the corresponding number does not exist.

The chosen number must consist of exactly k digits. We assume that number 0 doesn't contain any leading zeroes.

Sample test(s)
input
4 4
output
5881
input
5 1
output
36172
input
1 0
output
0
Note

For the first test sample dr(5881)  =  dr(22)  =  4.

For the second test sample dr(36172)  =  dr(19)  =  dr(10)  =  1.

A题就是简单的找规律,题意是让你构造一个k位的位之和为d的数,我们只要考虑最简单的情况,分清楚情况即可。因为没考虑No Solution的情况WA了一次。

#include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <map>
    #include <string>
    #include <stack>
    #include <cctype>
    #include <vector>
    #include <queue>
    #include <set>
    #include <utility>
    #include <cassert>
    using namespace std;
    ///#define Online_Judge
    #define outstars cout << "***********************" << endl;
    #define clr(a,b) memset(a,b,sizeof(a))
    #define lson l , mid  , rt << 1
    #define rson mid + 1 , r , rt << 1 | 1
    #define mk make_pair
    #define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
    #define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
    #define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
    #define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
    const int MAXN = 10000 + 50;
    const int MAXS = 10000 + 50;
    const int sigma_size = 26;
    const long long LLMAX = 0x7fffffffffffffffLL;
    const long long LLMIN = 0x8000000000000000LL;
    const int INF = 0x7fffffff;
    const int IMIN = 0x80000000;
    const int inf = 1 << 30;
    #define eps 1e-8
    const long long MOD = 1000000000 + 7;
    const int mod = 100000;
    typedef long long LL;
    const double PI = acos(-1.0);
    typedef double D;
    typedef pair<int , int> pii;
    #define Bug(s) cout << "s = " << s << endl;
    ///#pragma comment(linker, "/STACK:102400000,102400000")

    int main()
    {
        int n;
        int k , d;
        scanf("%d%d" , &k , &d);
        if(d == 0)
        {
            if(k != 1)puts("No solution");
            else printf("0\n");
            return 0;
        }
        if(k == d)
        {
            while(k--)putchar('1');
            putchar('\n');
        }
        else if(k > d)
        {
            int m = k - d;
            while(d--)putchar('1');
            while(m--)putchar('0');
            putchar('\n');
        }
        else
        {
            int m = d - k;
            printf("%d" , m + 1);
            k--;
            while(k--)putchar('1');
            putchar('\n');
        }
        return 0;
    }

————————————————————————————————————————————————————

******************************************************************************************************************************************

————————————————————————————————————————————————————

B. Vasya and Public Transport
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya often uses public transport. The transport in the city is of two types: trolleys and buses. The city has n buses and m trolleys, the buses are numbered by integers from 1 to n, the trolleys are numbered by integers from 1 to m.

Public transport is not free. There are 4 types of tickets:

  1. A ticket for one ride on some bus or trolley. It costs c1 burles;
  2. A ticket for an unlimited number of rides on some bus or on some trolley. It costs c2 burles;
  3. A ticket for an unlimited number of rides on all buses or all trolleys. It costs c3 burles;
  4. A ticket for an unlimited number of rides on all buses and trolleys. It costs c4 burles.

Vasya knows for sure the number of rides he is going to make and the transport he is going to use. He asked you for help to find the minimum sum of burles he will have to spend on the tickets.

Input

The first line contains four integers c1, c2, c3, c4 (1 ≤ c1, c2, c3, c4 ≤ 1000) — the costs of the tickets.

The second line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of buses and trolleys Vasya is going to use.

The third line contains n integers ai (0 ≤ ai ≤ 1000) — the number of times Vasya is going to use the bus number i.

The fourth line contains m integers bi (0 ≤ bi ≤ 1000) — the number of times Vasya is going to use the trolley number i.

Output

Print a single number — the minimum sum of burles Vasya will have to spend on the tickets.

Sample test(s)
input
1 3 7 19
2 3
2 5
4 4 4
output
12
input
4 3 2 1
1 3
798
1 2 3
output
1
input
100 100 8 100
3 5
7 94 12
100 1 47 0 42
output
16
Note

In the first sample the profitable strategy is to buy two tickets of the first type (for the first bus), one ticket of the second type (for the second bus) and one ticket of the third type (for all trolleys). It totals to (2·1) + 3 + 7 = 12 burles.

In the second sample the profitable strategy is to buy one ticket of the fourth type.

In the third sample the profitable strategy is to buy two tickets of the third type: for all buses and for all trolleys.

B题很水,题意是给你四种价格:汽车或电车的单票, 某种 汽车或电车的通票,汽车或电车的通票,汽车和电车的通票。然后给你他要乘坐某种车的次数,问你他的最小花费。直接遍历一遍,取最小值即可。

 #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <map>
    #include <string>
    #include <stack>
    #include <cctype>
    #include <vector>
    #include <queue>
    #include <set>
    #include <utility>
    #include <cassert>
    using namespace std;
    ///#define Online_Judge
    #define outstars cout << "***********************" << endl;
    #define clr(a,b) memset(a,b,sizeof(a))
    #define lson l , mid  , rt << 1
    #define rson mid + 1 , r , rt << 1 | 1
    #define mk make_pair
    #define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
    #define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
    #define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
    #define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
    const int MAXN = 1000 + 50;
    const int MAXS = 10000 + 50;
    const int sigma_size = 26;
    const long long LLMAX = 0x7fffffffffffffffLL;
    const long long LLMIN = 0x8000000000000000LL;
    const int INF = 0x7fffffff;
    const int IMIN = 0x80000000;
    const int inf = 1 << 30;
    #define eps 1e-8
    const long long MOD = 1000000000 + 7;
    const int mod = 100000;
    typedef long long LL;
    const double PI = acos(-1.0);
    typedef double D;
    typedef pair<int , int> pii;
    #define Bug(s) cout << "s = " << s << endl;
    ///#pragma comment(linker, "/STACK:102400000,102400000")
    int a[MAXN] , b[MAXN];
//    int ha[MAXN] , hb[MAXN];
    int main()
    {
        int n , m , c1 , c2 ,c3 , c4;
        while(~scanf("%d%d%d%d" , &c1 , &c2 , &c3 , &c4))
        {
//            clr(ha , 0) , clr(hb , 0);
            scanf("%d%d" , &n , &m);
            for(int i = 0 ; i < n ; i++)
            {
                scanf("%d" , &a[i]);
//                ha[a[i]] ++;
            }
            for(int i = 0 ; i < m ; i++)
            {
                scanf("%d" , &b[i]);
//                hb[b[i]]++;
            }
            int d1 = 0;
            for(int i = 0 ; i < n ; i++)d1 += min(a[i] * c1 , c2);
//            d1 = min(d1 , n * c2);
            d1 = min(d1 , c3);
            int d2 = 0;
            for(int i = 0 ; i < m ; i++)d2 += min(b[i] * c1 , c2);
//            d2 = min(d2 , m * c2);
            d2 = min(d2 , c3);
//            cout << d1 << ' ' << d2 << endl;
            printf("%d\n" , min(d1 + d2 , c4));
        }
        return 0;
    }

————————————————————————————————————————————————————

******************************************************************************************************************************************————————————————————————————————————————————————————

C. Vasya and Robot
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya has n items lying in a line. The items are consecutively numbered by numbers from 1 to n in such a way that the leftmost item has number 1, the rightmost item has number n. Each item has a weight, the i-th item weights wi kilograms.

Vasya needs to collect all these items, however he won't do it by himself. He uses his brand new robot. The robot has two different arms — the left one and the right one. The robot can consecutively perform the following actions:

  1. Take the leftmost item with the left hand and spend wi · l energy units (wi is a weight of the leftmost item, l is some parameter). If the previous action was the same (left-hand), then the robot spends extra Ql energy units;
  2. Take the rightmost item with the right hand and spend wj · r energy units (wj is a weight of the rightmost item, r is some parameter). If the previous action was the same (right-hand), then the robot spends extra Qr energy units;

Naturally, Vasya wants to program the robot in a way that the robot spends as little energy as possible. He asked you to solve this problem. Your task is to find the minimum number of energy units robot spends to collect all items.

Input

The first line contains five integers n, l, r, Ql, Qr (1 ≤ n ≤ 105; 1 ≤ l, r ≤ 100; 1 ≤ Ql, Qr ≤ 104).

The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 100).

Output

In the single line print a single number — the answer to the problem.

Sample test(s)
input
3 4 4 19 1
42 3 99
output
576
input
4 7 2 3 9
1 2 3 4
output
34
Note

Consider the first sample. As l = r, we can take an item in turns: first from the left side, then from the right one and last item from the left. In total the robot spends 4·42 + 4·99 + 4·3 = 576 energy units.

The second sample. The optimal solution is to take one item from the right, then one item from the left and two items from the right. In total the robot spends (2·4) + (7·1) + (2·3) + (2·2 + 9) = 34 energy units.

题意是给你一个长度为n的串a,给你l,r,Ql,Qr。每一次有两个操作,干掉当前串的最左端元素:花费能量l * a[i] , 如果上一次操作也是向左取还要加一个Ql;干掉当前串最右端元素:花费能量r*a[i] , 如果上一次操作也是向右取还要加上一个Qr。让你输出干掉整个串后的最小花费。

一开始看这题长得一脸DP的样子,就去搞DP了,后来才得知可以直接贪心的。

思路是我先预处理出前缀和suml,后缀和sumr,然后去遍历一遍数组,贪心的原则就是尽量减少Ql和Qr,取最小值即可。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 100000 + 50;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-8
const long long MOD = 1000000000 + 7;
const int mod = 100000;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int a[MAXN] , suml[MAXN] ,sumr[MAXN] , n , l , r , Ql , Qr;
int main()
{
    scanf("%d%d%d%d%d" , &n , &l , &r , &Ql , &Qr);
    FORR(i , 1 , n)scanf("%d" , &a[i]);
    suml[0] = sumr[0] = 0;
    FORR(i , 1 , n)suml[i] = suml[i - 1] + a[i] * l;
    int j = 1;
    REPP(i , n , 1)
    {
        sumr[j] = sumr[j - 1] + a[i] * r;
        j++;
    }
    int ans = min(suml[n] + (n - 1) * Ql , sumr[n] + (n - 1) * Qr);
    FORR(i , 1 , n - 1)
    {
        int x = i , y = n - i;
        if(x > y)
        {
            int t = suml[x] + (x - y - 1) * Ql + sumr[y];
            ans = min(ans , t);
        }
        else if(x == y)
        {
            int t = suml[x] + sumr[y];
            ans = min(ans , t);
        }
        else
        {
            int t = suml[x] + (y - x - 1) * Qr + sumr[y];
            ans = min(ans , t);
        }
    }
    printf("%d\n" ,ans);
    return 0;
}

————————————————————————————————————————————————————

******************************************************************************************************************************************————————————————————————————————————————————————————

D题不会委屈

————————————————————————————————————————————————————

******************************************************************************************************************************************————————————————————————————————————————————————————

E. Vasya and Beautiful Arrays
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya's got a birthday coming up and his mom decided to give him an array of positive integers a of length n.

Vasya thinks that an array's beauty is the greatest common divisor of all its elements. His mom, of course, wants to give him as beautiful an array as possible (with largest possible beauty). Unfortunately, the shop has only one array a left. On the plus side, the seller said that he could decrease some numbers in the array (no more than by k for each number).

The seller can obtain array b from array a if the following conditions hold: bi > 0; 0 ≤ ai - bi ≤ k for all 1 ≤ i ≤ n.

Help mom find the maximum possible beauty of the array she will give to Vasya (that seller can obtain).

Input

The first line contains two integers n and k (1 ≤ n ≤ 3·105; 1 ≤ k ≤ 106). The second line contains n integers ai (1 ≤ ai ≤ 106) — array a.

Output

In the single line print a single number — the maximum possible beauty of the resulting array.

Sample test(s)
input
6 1
3 6 10 12 13 16
output
3
input
5 3
8 21 52 15 77
output
7
Note

In the first sample we can obtain the array:

3 6 9 12 12 15

In the second sample we can obtain the next array:

7 21 49 14 77

题意是给你长度为n的数组和一个数k,为你是否有可能通过把某些数减去一个不大于k的数字使得数组中所有数都有一个共同的质因子,输出它。

思路就是先排个序,用最小的数字当作质因子,如果遇到一个不满足的就取该数的最大质因子从头判断,直到找到结果。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 300000 + 50;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-8
const long long MOD = 1000000000 + 7;
const int mod = 100000;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int a[MAXN] , n  , k;
int main()
{
    scanf("%d%d" , &n ,&k);
    FOR(i , 0 , n)scanf("%d" , &a[i]);
    sort(a , a + n);
    int ans = a[0], b;
    while(1)
    {
        for(b = 0; b < n ; b++)
        {
            if(a[b] % ans > k)
            {
                ans = a[b] / (a[b] / ans + 1);
                break;
            }
        }
        if(b == n)break;
    }
    printf("%d\n" , ans);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值