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.
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.
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
3 7
YES
100 99
YES
100 50
NO
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;
}
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.
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.
In the first line print an integer — the number of triangles with the non-zero area among the painted points.
4 0 0 1 1 2 0 2 2
3
3 0 0 1 1 2 0
1
1 1 1
0
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;
}
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.
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.
In the first line print the maximum possible value of an expression.
3+5*7+8*4
303
2+3*5
25
3*4*5
60
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).
#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;
}