1. 程式人生 > >HDU 思維規律數學題

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;
}