A. Vanya and Table
水题、求覆盖矩形面积和
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf 1000000005
struct node {
int x, y;
}dot[1005 * 4];
int main() {
int n;
scanf("%d", &n);
int sum = 0;
while (n--) {
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
sum += (x2 - x1 + 1) * (y2 - y1 + 1);
}
printf("%d\n", sum);
return 0;
}
B - Vanya and Books
第一反应是数位dp、但想了想cf应该考思维比算法多、
找规律、预处理出每一位的总数、最后再算最后一位的位数和
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
#define LL __int64
LL dp[15];
LL sum[15];
LL n;
int main() {
dp[1] = 9;
for(LL i = 2; i <= 10; i++) {
dp[i] = dp[i-1] * 10;
}
LL k = 1;
for(LL i = 1; i <= 10; i++){
dp[i] = dp[i] * k;
k++;
}
for(LL i = 1; i <= 10; i++) {
dp[i] += dp[i-1];
}
scanf("%I64d", &n);
LL m = n;
LL temp = 1;
LL sum = 0;
LL len = 0;
while (n) {
len++;
n /= 10;
temp *= 10;
}
sum += dp[len-1];
temp /= 10;
sum += (m - temp + 1) * len;
printf("%I64d\n", sum);
return 0;
}
C - Vanya and Scales
感觉自己好逗、读了半个多小时没读懂题意、
后来更新了提示、才读懂、、、
其实就是两个W进制的数X和Y、每一位上是0或1、
m = X + Y or m = X - Y
由此可以发现规律、m的每一位上可以为0、1、w-1、
但是每一次>=w-1都要往前进一位、然后判断下一位时候满足、
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
#define LL __int64
int main() {
LL w,m;
scanf("%I64d%I64d",&w,&m);
LL flag = 0;
LL temp=0;
while(m){
LL t=m%w;
if(temp)t++;
if(t!=0&&t!=1&&t<w-1)flag=1;
if(t>=w-1)temp=1;
else temp=0;
m=m/w;
}
if(flag)printf("NO\n");
else printf("YES\n");
return 0;
}
D - Vanya and Triangles
其实这题比较简单、
利用补集的思想、先求总数、然后求共线的三个点的组合数、相减就是答案、
求是否共线直接用斜率判断就行、
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
#define LL __int64
const double eps=1e-8;
struct node {
int x, y;
}dot[2005];
int main() {
int n;
scanf("%d",&n);
for (int i = 1; i <= n; i++)scanf("%d%d",&dot[i].x,&dot[i].y);
double k[2005];
if(n<3) {
printf("0\n");
return 0;
}
LL ans = 0;
for(int i=1;i<=n;i++) {
int cnt = 0;
for(int j=1;j<=n;j++) {
if(i==j)continue;
if(dot[i].x==dot[j].x) k[++cnt]=1e30;
else k[++cnt]=1.0*(dot[i].y-dot[j].y)/(dot[i].x-dot[j].x);
}
sort(k+1,k+cnt+1);
int j=1;
while(j<=cnt){
int t=j;
while(t<=cnt&&fabs(k[t]-k[j])<eps)t++;
ans+=(t-j)*(t-j-1)/2;
j=t;
}
}
ans= (LL)n*(n-1)*(n-2)/6 - ans/3;
printf("%d\n",ans);
return 0;
}
最后一题实在懒得做、大概就这样了吧、