hdu 1264
这个用不到神马hash,直接暴力就OK了,不过要注意输入的顶点问题,处理一下矩形对角线的顶点。
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
int visit[110][110];
int main()
{
int a,b,c,d,i,j,sum;
int temp;
for(i = 0;i < 110;i ++)
for(j = 0;j < 110;j ++)
visit[i][j] = 0;
while(~scanf("%d%d%d%d",&a,&b,&c,&d))
{
if(a == b && c == d && a == c)
{
if(a == -1 || a == -2)
{
sum = 0;
for(i = 1;i <= 100;i ++)
for(j = 1;j <= 100;j ++)
sum += visit[i][j];
printf("%d\n",sum);
memset(visit,0,sizeof(visit));
if(a == -2) break;
}
}
else
{
if(a > c)
temp = c,c = a,a = temp;
if(b > d)
temp = d,d = b,b = temp;
for(i = a+1;i <= c;i ++)
for(j = b+1;j <= d;j ++)
visit[i][j] = 1;
}
}
return 0;
}
hdu 1496
这个注意到数据的范围[-100,100],但是后面的多项式中是x^2,所以数据的正负对其值没有影响。
将多项式分为2段,一段为a*x1*x1+b*x2*x2,另一段为c*x3*x3+d*x4*x4,然后利用hash
由于xi的取值有正和负两种,所以共有2^4种。
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
int hash1[1000005];
int hash2[1000005];
int main()
{
int a,b,c,d;
int i,j,s1;
int ans;
while(~scanf("%d%d%d%d",&a,&b,&c,&d))
{
if((a > 0 && b > 0 && c > 0 && d > 0) || (a < 0 && b < 0 && c < 0 && d < 0))
{
printf("0\n");
continue;
}
memset(hash1,0,sizeof(hash1));
memset(hash2,0,sizeof(hash2));
for(i = 1;i <= 100;i ++)
{
for(j = 1;j <= 100;j ++)
{
s1 = i*i*a + j*j*b;
if(s1 >= 0) hash1[s1] += 1;
else hash2[-s1] += 1;
}
}
ans = 0;
for(i = 1;i <= 100;i ++)
{
for(j = 1;j <= 100;j ++)
{
s1 = i*i*c + j*j*d;
if(s1 > 0) ans += hash2[s1];
else ans += hash1[-s1];
}
}
printf("%d\n",16*ans);
}
return 0;
}
hdu 2522
判断是否曾经出现过该余数。
#include<stdio.h>
#include<string.h>
char u[100001];
int main(){
int t,a,b;
scanf("%d",&t);
while(t--)
{
scanf("%d",&b);
if(b<0){putchar('-');b = -b;}
memset(u,1,b + 1);
a = 1;
u[1] = u[0] = 0;
printf("0.");
do{
u[a] = 0;
a *= 10;
putchar(a/b + '0');
a %= b;
}while(u[a]);
putchar('\n');
}
return 0;
}
hdu 2600
nlgn版本
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
typedef struct node
{
int num,sta;
}node;
node arr[220];
char ch[50];
int N;
int cmp(const void *a,const void *b)
{
node aa,bb;
aa = *(node *)a,bb = *(node *)b;
return aa.num - bb.num;
}
int main()
{
int p,q,i,max,sum;
while(~scanf("%d",&N))
{
scanf("%d%d",&p,&q);
for(i = 0;i < N;i ++)
{
scanf("%d%d",&arr[i].num,&arr[i+N].num);
gets(ch);
arr[i].sta = -1,arr[i].sta = 1;
}
qsort(arr,2*N,sizeof(node),cmp);
max = arr[0].num - 1;
if(arr[2*N-1].num < q)
printf("%d\n",q);
else
{
sum = 0;
for(i = 0;i < 2*N;i ++)
{
sum += arr[i].sta;
if(sum == 0&&i != 2*N-1)
{
if(max < arr[i+1].num-1)
max = arr[i+1].num - 1;
}
}
if(max >= p) printf("%d\n",max);
else puts("Badly!");
}
}
return 0;
}
n^2版本
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
char name[50];
int large[110][2];
int input[110][2];
int main()
{
int N,p,q,flag0,flag1,i,j,max;
while(~scanf("%d",&N))
{
scanf("%d%d",&p,&q);
for(i = 0;i < N;i ++)
{
scanf("%d %d",&input[i][0],&input[i][1]);
gets(name);
large[i][0] = input[i][0] - 1,large[i][1] = input[i][1] + 1;
}
max = -6000001;
for(i = 0;i < N;i ++)
{
flag0 = flag1 = 0;
for(j = 0;j < N;j ++)
{
if(large[i][0]<=input[j][1]&&large[i][0]>=input[j][0])
flag0 = 1;
if(large[i][1]<=input[j][1]&&large[i][0]>=input[j][0])
flag1 = 1;
}
if(flag0 == 0 && flag1 == 0)
{
if(large[i][1] > max&&large[i][1] <= q)
max = large[i][1];
else if(large[i][0] > max)
max = large[i][0];
}
else if(flag0 == 0)
{
if(large[i][0] > max)
max = large[i][0];
}
else if(flag1 == 0)
{
if(large[i][1] > max && large[i][1] <= q)
max = large[i][1];
}
}
if(max >= p && max <= q)
printf("%d\n",max);
else puts("Badly!");
}
return 0;
}