HDU 思维规律数学题

目录


Cube(组合数+规律)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2717    Accepted Submission(s): 2146

 

Problem Description

Cowl is good at solving math problems. One day a friend asked him such a question: You are given a cube whose edge length is N, it is cut by the planes that was paralleled to its side planes into N * N * N unit cubes. Two unit cubes may have no common points or two common points or four common points. Your job is to calculate how many pairs of unit cubes that have no more than two common points.
Process to the end of file.

Input

There will be many test cases. Each test case will only give the edge length N of a cube in one line. N is a positive integer(1<=N<=30).

Output

For each test case, you should output the number of pairs that was described above in one line.

Sample Input

1

2

3

Sample Output

0

16

297

Hint

Hint The results will not exceed int type.

Author

Gao Bo

Source

杭州电子科技大学第三届程序设计大赛

【分析】题意就是要找交点≤2的单位立方体的对数。

画图之后,很容易看出,两个立方体之间的交点只能是0、1、2、4,所以只要拿全部的数目减去有4个交点的立方体对数即可。

从n^3个小立方体中选两个令N=n^3,有\frac{N(N-1)}{2}种选择。有4个交点的小立方体,有3n(n-1)种。每一面n^3列,每列(n-1)对。

【代码】

#include<iostream>
using namespace std;
 
int main()
{
	int n,s;
	while(scanf("%d",&n)!=EOF)
	{
		s=n*n*n*(n*n*n-1)/2-3*n*n*(n-1);
		cout<<s<<endl;
	}
	return 0;
}

Rectangle and Circle(矩形与圆相交问题)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3533    Accepted Submission(s): 931


 

Problem Description

Given a rectangle and a circle in the coordinate system(two edges of the rectangle are parallel with the X-axis, and the other two are parallel with the Y-axis), you have to tell if their borders intersect.

Note: we call them intersect even if they are just tangent. The circle is located by its centre and radius, and the rectangle is located by one of its diagonal.

Input

The first line of input is a positive integer P which indicates the number of test cases. Then P test cases follow. Each test cases consists of seven real numbers, they are X,Y,R,X1,Y1,X2,Y2. That means the centre of a circle is (X,Y) and the radius of the circle is R, and one of the rectangle's diagonal is (X1,Y1)-(X2,Y2).

Output

For each test case, if the rectangle and the circle intersects, just output "YES" in a single line, or you should output "NO" in a single line.

Sample Input

2

1 1 1 1 2 4 3

1 1 1 1 3 4 4.5

Sample Output

YES

NO

Author

weigang Lee

【分析】矩形与圆是否相交问题。 要画一下图来帮助理解

  • 最远距离,即圆心到4个顶点的距离。如果这个距离中的最大值仍小于半径r说明圆内含与矩形,并不相交。
  • 最近距离。即圆心到四条边的距离。注意本题的4条边都是与坐标轴平行的。如果该距离的最小值仍大于半径r,说明不相交。

【代码】

  • 定义两个函数,分别计算圆心到顶点的距离,与圆心到边的距离。
  • 构造矩形的4个顶点。(思路及代码参考
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
double dis1(double x1,double y1,double x2,double y2)
{
	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double dis2(double x,double y,double x1,double y1,double x2,double y2)
{
	if(x1==x2)
	{
		if(y>=min(y1,y2)&&y<=max(y1,y2))
			return fabs(x-x1);
		else
		{
			double a=dis1(x,y,x1,y1);
			double b=dis1(x,y,x2,y2);
			return min(a,b);
		}
	}
	else if(y1==y2)
	{
		if(x>=min(x1,x2)&&x<=max(x1,x2))
			return fabs(y-y1);
		else
		{
			double a=dis1(x,y,x1,y1);
			double b=dis1(x,y,x2,y2);
			return min(a,b);
		}
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		double x,y,r,x1,y1,x2,y2;
		scanf("%lf%lf%lf%lf%lf%lf%lf",&x,&y,&r,&x1,&y1,&x2,&y2);
		double x3=x2,x4=x1;
		double y3=y1,y4=y2;
		double l1=dis1(x,y,x1,y1);
		double l2=dis1(x,y,x1,y2);
		double l3=dis1(x,y,x3,y3);
		double l4=dis1(x,y,x4,y4);
		double lenMax=max(l1,max(l2,max(l3,l4)));
		
		double l5=dis2(x,y,x1,y1,x4,y4);
		double l6=dis2(x,y,x1,y1,x3,y3);
		double l7=dis2(x,y,x2,y2,x3,y3);
		double l8=dis2(x,y,x2,y2,x4,y4);
		double lenMin=min(l5,min(l6,min(l7,l8)));
		
		if(lenMax<r || lenMin>r)
			printf("NO\n");
		else printf("YES\n");
	}
	return 0;
}

Wolf and Rabbit(思维+规律)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10348    Accepted Submission(s): 5242

 

Problem Description

There is a hill with n holes around. The holes are signed from 0 to n-1.

A rabbit must hide in one of the holes. A wolf searches the rabbit in anticlockwise order. The first hole he get into is the one signed with 0. Then he will get into the hole every m holes. For example, m=2 and n=6, the wolf will get into the holes which are signed 0,2,4,0. If the rabbit hides in the hole which signed 1,3 or 5, she will survive. So we call these holes the safe holes.

Input

The input starts with a positive integer P which indicates the number of test cases. Then on the following P lines,each line consists 2 positive integer m and n(0<m,n<2147483648).

Output

For each input m n, if safe holes exist, you should output "YES", else output "NO" in a single line.

Sample Input

2

1 2

2 2

Sample Output

NO

YES

【分析】思维+规律。举个例子写写就出来了。就是看两个数是否互质。

【代码】

#include<iostream>
#include<cstdio>
using namespace std;
int gcd(int x,int y)
{
	return y==0?x:gcd(y,x%y);
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,m;
		scanf("%d%d",&n,&m);
		int x=gcd(n,m);
		if(x!=1)
			printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

Coin Change(思维+暴力)

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23473    Accepted Submission(s): 8233

 

Problem Description

Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.

For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, or two 5-cent coins and one 1-cent coin, or one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.
Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 100 coins.

Input

The input file contains any number of lines, each one consisting of a number ( ≤250 ) for the amount of money in cents.

Output

For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.

Sample Input

11

26

Sample Output

4

13

Author

Lily

Source

浙江工业大学网络选拔赛

 【分析】暴力。根据题目,所用金币总共不超过100个。数据比较小,暴力下好了咯

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		int sum=0;
		for(int a=0;a*50<=n;a++)
			for(int b=0;b*25<=n;b++)
				for(int c=0;c*10<=n;c++)
					for(int d=0;d*5<=n;d++)
						if(n+a+b+c+d-a*50-b*25-c*10-d*5<=100&&n-a*50-b*25-c*10-d*5>=0)
							sum++;
		printf("%d\n",sum);					
	}
	return 0;
}

无限的路(思维规律)

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11915    Accepted Submission(s): 6446

 

Problem Description

甜甜从小就喜欢画图画,最近他买了一支智能画笔,由于刚刚接触,所以甜甜只会用它来画直线,于是他就在平面直角坐标系中画出如下的图形:

甜甜的好朋友蜜蜜发现上面的图还是有点规则的,于是他问甜甜:在你画的图中,我给你两个点,请你算一算连接两点的折线长度(即沿折线走的路线长度)吧。

Input

第一个数是正整数N(≤100)。代表数据的组数。
每组数据由四个非负整数组成x1,y1,x2,y2;所有的数都不会大于100。

Output

对于每组数据,输出两点(x1,y1),(x2,y2)之间的折线距离。注意输出结果精确到小数点后3位。

Sample Input

5

0 0 0 1

0 0 1 0

2 3 3 1

99 99 9 9

5 5 5 5

 

Sample Output

1.000

2.414

10.646

54985.047

0.000

Author

Lily

 【分析】画图之后,会发现,两点之间的值只有1、\sqrt{2}\sqrt{x1^{2}+x2^{2}} 这三种情况。而1只存在于(0,0)到(0,1)之间,所以ans初值为1。所以,求两点之间的距离时,就是求两点到原点的距离的差。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
const double q=sqrt(2);
double sum(int x,int y)
{
	double ans=1;
	for(int i=1;i<=x+y;i++)
		ans+=q*i;
	ans-=y*q;
	for(int i=0;i<x+y;i++)
		ans+=sqrt(i*i+(i+1)*(i+1));
	return ans;
}
int main()
{
	int x1,y1,x2,y2,t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
		printf("%.3lf\n",fabs(sum(x1,y1)-sum(x2,y2)));
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38735931/article/details/82837972