Codeforces Round #308 (Div. 2)

C. Vanya and Scales

Vanya has a scales for weighing loads and weights of masses w0, w1, w2, ..., w100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.

Input

The first line contains two integers w, m (2 ≤ w ≤ 109, 1 ≤ m ≤ 109) — the number defining the masses of the weights and the mass of the item.

Output

Print word 'YES' if the item can be weighted and 'NO' if it cannot.

Sample test(s)
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
Note

Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.

Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.

Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input.



思路:看能不能表示成W进制,从低位到高位,不够,则加一,还不够说明不行

#include<bits/stdc++.h>
using namespace std;
const int maxn=40;
int W,N;
int dig[maxn];
bool solve(int x)
{
    int len=0;
    memset(dig,0,sizeof(dig));
    while(x)
    {
        dig[len++]=x%W;
        x/=W;
    }
    for(int i=0;i<=len;i++)
    {
        if(dig[i]==0||dig[i]==1)continue;
        if(dig[i]%W==0){dig[i+1]++;continue;}
        dig[i]+=1;
        if(dig[i]<W)return false;
        dig[i+1]++;
    }
    return true;
}
int main()
{
    while(scanf("%d%d",&W,&N)!=EOF)
    {
        if(solve(N))printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

D. Vanya and Triangles

Vanya got bored and he painted n distinct points on the plane. After that he connected all the points pairwise and saw that as a result many triangles were formed with vertices in the painted points. He asks you to count the number of the formed triangles with the non-zero area.

Input

The first line contains integer n (1 ≤ n ≤ 2000) — the number of the points painted on the plane.

Next n lines contain two integers each xi, yi ( - 100 ≤ xi, yi ≤ 100) — the coordinates of the i-th point. It is guaranteed that no two given points coincide.

Output

In the first line print an integer — the number of triangles with the non-zero area among the painted points.

Sample test(s)
Input
4
0 0
1 1
2 0
2 2
Output
3
Input
3
0 0
1 1
2 0
Output
1
Input
1
1 1
Output
0
Note

Note to the first sample test. There are 3 triangles formed: (0, 0) - (1, 1) - (2, 0); (0, 0) - (2, 2) - (2, 0); (1, 1) - (2, 2) - (2, 0).

Note to the second sample test. There is 1 triangle formed: (0, 0) - (1, 1) - (2, 0).

Note to the third sample test. A single point doesn't form a single triangle.

思路:枚举每个点,然后计算每个点跟他的斜率,统计这个斜率下投多少个点,然后减去
#include<bits/stdc++.h>
using namespace std;
const int maxn=2010;
const int INF=1000000000;
typedef long long LL;
int N;
struct Node
{
    int x,y;
}p[maxn];
int vis[maxn];
map<pair<int,int>,int> mp;
int main()
{
    scanf("%d",&N);
    for(int i=1;i<=N;i++)scanf("%d%d",&p[i].x,&p[i].y);
    if(N<=2){printf("0\n");return 0;}
    memset(vis,0,sizeof(vis));
    LL ans=(LL)N*(N-1)*(N-2)/6;
    for(int i=1;i<=N;i++)
    {
        mp.clear();
        for(int j=1;j<=N;j++)
        {
            if(i==j||vis[j])continue;
            if(p[i].x==p[j].x)mp[make_pair(INF,INF)]++;
            else if(p[i].y==p[j].y)mp[make_pair(0,0)]++;
            else
            {
                bool flag=true;
                int up=p[j].y-p[i].y;
                int down=p[j].x-p[i].x;
                if(up*down<0)flag=false;
                up=abs(up);down=abs(down);
                int g=__gcd(up,down);
                if(flag)mp[make_pair(up/g,down/g)]++;
                else mp[make_pair(-up/g,down/g)]++;
            }
        }
        vis[i]=1;
        for(auto x : mp)
        {
            if(x.second<2)continue;
            auto tmp=x.second;
            ans-=(LL)tmp*(tmp-1)/2;
        }
    }
    cout<<ans<<endl;
    return 0;
}
E. Vanya and Brackets

Vanya is doing his maths homework. He has an expression of form , where x1, x2, ..., xn are digits from 1 to 9, and sign represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.

Input

The first line contains expression s (1 ≤ |s| ≤ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs  +  and  * .

The number of signs  *  doesn't exceed 15.

Output

In the first line print the maximum possible value of an expression.

Sample test(s)
Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60
Note

Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.

Note to the second sample test. (2 + 3) * 5 = 25.

Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60).

暴力(代码从codeforces粘的,懒得写了,这个大神写得很舒服)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
string str;
vector<int> pos;
stack<char> sc;
stack<ll> sn;

ll tworesult(ll a, ll b, char c)
{
    return (c == '*') ? a * b : a + b;
}
void cal()
{
    char t = sc.top();
    sc.pop();
    ll a = sn.top();
    sn.pop();
    ll b = sn.top();
    sn.pop();
    sn.push(tworesult(a, b, t));
}
ll expression(string s)
{
    for(int i = 0; i < s.length(); ++i)
    {
        char c = s[i];
        if(isdigit(c)) sn.push(c - '0');
        else if(c == '(') sc.push(c);
        else if(c == ')')
        {
            while(sc.top() != '(') cal();
            sc.pop();
        }
        else
        {
            if(c == '+')
            {
                while(!sc.empty() && sc.top() == '*') cal();
                sc.push(c);
            }
            else sc.push(c);
        }
    }
    while(!sc.empty()) cal();
    return sn.top();
}
int main(int argc, char const *argv[])
{
    cin >> str;
    n = str.size();
    pos.push_back(-1);
    for(int i = 1; i < n; i += 2)
        if(str[i] == '*') pos.push_back(i);
    pos.push_back(n);
    int len = pos.size();
    ll ans = 0;
    for(int i = 0; i < len - 1; ++i)
        for(int j = i + 1; j < len; ++j)
        {
            string s = str;
            s.insert(pos[i] + 1, 1, '(');
            s.insert(pos[j] + 1, 1, ')');
            ans = max(ans, expression(s));
        }
    cout << ans << endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值