A - Vanya and Table 每次都给出一个子矩阵并让+1,最后求矩阵内数之和
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, x1, y1, x2, y2, s = 0;
scanf("%d",&n);
while( n-- )
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
s = s + (x2 - x1 + 1) * (y2 - y1 + 1);
}
printf("%d\n", s);
return 0;
}
B - Vanya and Books 1~n的数里面有多少位。暴力模拟
#include <bits/stdc++.h>
using namespace std;
typedef long long lol;
int main() {
lol n;
while ( cin >> n ) {
lol temp = 1000000000;
lol cnt = 10;
lol res = 0;
while ( temp>0&&n>0 ) {
if ( n-(temp-1)>0 ) {
res += (n-(temp-1))*(cnt);
n = temp - 1;
}
temp /= 10;
-- cnt;
}
cout << res << endl;
}
return 0;
}
C - Vanya and Scales 给出w进制的秤和m重的东西,问能否用秤称出m重的东西。
m化成w进制的数,则表示成 p0 * w^0 + p1 * w^1 + p2 * w^2.......px * w^x。取其中一项pc * w^c,若pc = w-1,则pc * w^c + w^c = w * w^(c+1),即若最开始m重的东西放在左边,w进制里面若有一项系数为w-1,即等价于在左边放一个 w^c重的秤砣,右边放一个 w^(c+1)的秤砣。其他情况下,pc系数为0或1表示右边放或不放,是满足的
int main()
{
int dig[222], tot;
ll m, w;
while( ~scanf("%lld%lld", &w, &m) ) {
memset( dig, 0, sizeof( dig ) );
tot = 0;
ll x = m;
while( x ) {
dig[tot++] = x % w;
x /= w;
}
bool OK = 1;
for( int i = 0; i < tot; ++i ) {
if( dig[i] == 0 || dig[i] == 1 )
continue;
if( dig[i] >= w-1 ) {
dig[i+1]++;
tot++;
}
else {
OK = 0;
break;
}
}
puts( OK? "YES" : "NO" );
}
return 0;
}
D - Vanya and Triangles 给出平面上n个点,问能组成多少个三角形。
枚举每个点出发别的点到他的斜率即可
const int N = 2020;
ll x[N], y[N];
ll n;
map <ll, ll> mp;
map <ll, ll> :: iterator it;
pair <ll, ll> f( int i, int j )
{
pair <ll, ll> re;
ll xx = x[i] - x[j], yy = y[i] - y[j];
if( xx == 0 ) {
re.first = 1LL*N, re.second = 1LL*N;
return re;
}
if( yy == 0 ) {
re.first = 1LL*0, re.second = 1LL*0;
return re;
}
ll xxx = xx / abs(xx), yyy = yy / abs(yy);
yyy *= xxx, xxx = 1;
xx = abs(xx), yy = abs(yy);
ll gcd = __gcd( xx, yy );
re.first = xx / gcd * xxx, re.second = yy / gcd * yyy;
return re;
}
int main()
{
while( ~scanf("%lld", &n) ) {
mp.clear();
ll ans = n * (n-1) * (n-2) / 6, tmp = 0;
for( int i = 1; i <= n; ++i )
scanf("%lld %lld", &x[i], &y[i]);
for( int i = 1; i <= n; ++i ) {
mp.clear();
for( int j = 1; j <= n; ++j ) {
if( i == j )
continue;
pair <ll, ll> c = f( i, j );
ll xx = c.first, yy = c.second;
mp[xx*N+yy]++;
}
for( it = mp.begin(); it != mp.end(); ++it ) {
tmp += (it->second >= 2) ? ((it->second-1) * it->second / 2) :0;
}
}
ans -= tmp / 3;
printf("%lld\n", ans);
}
return 0;
}
表达式加一对括号,求表达式的最大值
括号加在*旁边就行了,暴力模拟过去,由于*最多15,复杂度15*15*n
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define ls rt << 1
#define rs rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
#define asd puts("sdfsdfsdfsdfsdfsdf");
typedef long long ll;
///typedef __int64 LL;
const int inf = 0x3f3f3f3f;
const int N = 5050;
int p[20], pos;
char s[N];
int main()
{
while( ~scanf("%s", s+1 ) ) {
int len = strlen( s+1 );
s[len+1] = '+';
pos = 0, p[pos++] = 0;
for( int i = 1; i <= len+1; ++i ) {
if( s[i] == '*' )
p[++pos] = i;
}
p[++pos] = len+1;
ll ans = 0;
if( pos == 2 ) {
ans = s[1] - '0';
for( int i = 2; i <= len+1; ++i ) {
if( s[i] != '+' )
ans += s[i] - '0';
}
printf("%lld\n", ans);
continue;
}
for( int i = 0; i < pos; ++i ) {
for( int j = i; j <= pos; ++j ) {
int l = p[i], r = p[j];
ll x = 0, tmp = 1, op = 0; //op = 0----+ op = 1 ----- *
for( int k = 1; k <= l; ++k ) {
if( s[k] >= '0' && s[k] <= '9' ) {
if( op == 0 )
tmp = s[k] - '0';
else
tmp *= s[k] - '0';
}
else if( s[k] == '+' ) {
x += tmp;
op = 0;
}
else {
op = 1;
}
}
//printf("%d %d %lld %lld\n", l, r, x, tmp);
ll y = 0, op1 = 0, tmp1 = 1;
for( int k = l+1; k <= r; ++k ) {
if( s[k] >= '0' && s[k] <= '9' ) {
if( op1 == 0 )
tmp1 = s[k] - '0';
else
tmp1 *= s[k] - '0';
}
else if( s[k] == '+' ) {
y += tmp1;
op1 = 0;
}
else {
op1 = 1;
}
}
if( op1 == 1 )
y += tmp1;
//printf("%d %d %lld %lld\n", l, r, y, tmp1);
ll tmp2 = 1;
int o = r+1;
for( ; o <= len+1 && s[o] != '+'; ++o ) {
if( s[o] >= '0' && s[o] <= '9' )
tmp2 *= s[o]-'0';
}
//printf("%d %d %lld \n", l, r, tmp2);
x += tmp * y * tmp2;
op = 0, tmp = 0;
for( int k = o+1; k <= len+1; ++k ) {
if( s[k] >= '0' && s[k] <= '9' ) {
if( op == 0 )
tmp = s[k] - '0';
else
tmp *= s[k] - '0';
}
else if( s[k] == '+' ) {
x += tmp;
op = 0;
}
else {
op = 1;
}
}
//printf("%lld\n\n", x);
ans = max( ans, x );
}
}
printf("%lld\n", ans);
}
return 0;
}
// 1+2*3+4