Thinking and practice of programming CSP-M4 supplementary questions

Posted by ubaldc36 on Tue, 09 Jun 2020 07:46:00 +0200

A - TT count ducks

meaning of the title

On this day, TT was suffering from the epidemic at home. After one hour's inhalation of cat in the cloud, TT decided to go to the mountain nearby.
TT came to a small lake and saw many ducks playing by the lake. TT was envious. At this time, he found that every duck is different, or has different feathers, or different personalities. TT opens a map < duck, integer > Tong in his mind, and turns the duck into some numbers. Now he wondered how many ducks were mapped to numbers with different numbers less than k.

Input

The first input line contains two numbers, n,k, representing the number of ducks and k required by the title.
There are n numbers in the next line, aia_iai, each number represents the value after the duck is mapped by TT.

Output

Output a line, a number, indicating the number of ducks that meet the description of the topic.
No space at end of line

Example

Input
6 5 123456789 9876543210 233 666 1 114514

Output
4

thought

This problem is actually a modular operation for long integers
But he has been in test5 time TLE, and then I changed it many times, and finally changed c + + to c....

code

#include <iostream>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
int n,k,num;
long long int duck;
bool a[15];

int main()
{
	scanf("%d %d",&n,&k);
	num=0;
	for(long long int i=1;i<=n;i++)
	{
		memset(a,0,sizeof(a));
		int m=0;
		scanf("%lld",&duck);
		for(duck;duck;duck/=10)
		{
			if(!a[duck%10]) 
			{
				a[duck%10]=1;
				++m;
			}
		}
		if(m<k) num++;
    }
    printf("%d",num);
	return 0;
}

B - ZJM to resist cosmic rays

meaning of the title

It is said that 2020 is the year when cosmic rays are concentrated, which has something to do with the mysterious cosmic dog! But Ruishen and Dongdong are busy fighting the cosmic dog head-on, and the defense of cosmic rays falls to ZJM. Assuming that the emission point of cosmic rays is in a plane, ZJM has obtained all the emission points of cosmic rays by special means, and their coordinates are integers. ZJM is going to build a protective cover, which is a circle with the center on the emission point of cosmic rays. At the same time, because most of the funds are allocated to Ruishen, ZJM needs to save money and make a protective cover with the smallest area. When ZJM decides, Dongdong comes to ZJM to fight against the space dog, so ZJM throws the problem to you~

Input

Enter a positive integer N in the first line to indicate the number of cosmic ray emission points
Next N lines, two integers X,Y for each line, represent the positions of cosmic ray emission points

Output

The output consists of two lines
The first line outputs the central coordinates x and y of the protective cover separated by spaces
Square of the radius of the output protective cover of the second line
(two decimal places are reserved for all outputs. If there are multiple solutions, the output x is smaller. If there are multiple solutions thrown, the input y is smaller.)
No space at end of line

Example

Input
5
0 0
0 1
1 0
0 -1
-1 0

Output
0.00 0.00
1.00

thought

Enumerate to find the distance between points, find the maximum value in each group, and then find the minimum value of each group's maximum value

code

#include <iostream>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
long long int mn=0x0f0f0f0f0f;
long long int mx;
struct node
{
	int x,y;
	bool operator < (const node& a)
	{
		if(x!=a.x)
		{
			return x<a.x;
		}
		return y<a.y;
	}
}list[2000];

int main()
{
	int n,a,b;
	long long mn=1e12;
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>list[i].x>>list[i].y;
	sort(list+1,list+1+n);
	node p;
	for(int i=1;i<=n;i++)
	{
		long long mx=0;
		for(int j=1;j<=n;j++)
		{
			long long xx=list[i].x-list[j].x;
			long long yy=list[i].y-list[j].y;
			long long ans=xx*xx+yy*yy;
			if(ans>mx)    mx=ans;
		}
		if(mx<mn)
		{
			mn=mx;
			a=list[i].x;
			b=list[i].y;
		}
	}
	cout<<a<<".00 "<<b<<".00"<<endl;
	cout<<mn<<".00";
	return 0;
	return 0;
}

C - the crisis of the cosmic dog

meaning of the title

We have learned the power of the universe dog in the cosmic ray of the great war of the gods. Although the universe dog is vicious, it has a lovely girlfriend.
Recently, his girlfriend got some numbers. At the same time, she likes trees very much, so she plans to put the numbers into a tree.
This day, she is almost finished, and she and her friends make an appointment to go out on holiday. The greedy cosmic dog accidentally ate all the branches of the tree. So fear surrounds the universe dog. Now he wants to restore the whole tree, but he only knows that the tree is a binary search tree, and the GCD (greater common divisor) of two nodes connected to any tree edge is more than 1.
But the cosmic dog only emits cosmic rays. He asks for your help and asks if you can help him solve this problem.
Supplementary knowledge:
GCD: maximum common divisor, the largest of two or more integer common divisors, for example, the maximum common divisor of 8 and 6 is 2.
A brief example of calculating gcd by rolling phase division:
int gcd(int a,int b){return b == 0 ? a : gcd(b,a%b);}

Input

Enter a t in the first row to indicate the number of data groups.
For each group of data, enter an n in the first row to represent the number of numbers
Next line has n numbers aia_iai, input guarantee is in ascending order.

Output

Output one row for each group of data. If you can create a tree that meets the description of the topic, output Yes. Otherwise, output No.
There is no space at the end of the line.

Example

Input1
1
6
3 6 9 18 36 108
Output1
Yes

Input2
2
2
7 17
9
4 8 10 12 15 18 33 44 81
Output2
No
Yes

Example 1 can be constructed as follows

thought

When GCD of two numbers is more than 1, it can be the parent-child relationship of binary search tree f[i] [j]= f[j] [i]=1
definition:

  • lef [i] [j] indicates whether [I, J] is feasible as the left son of j+1
  • righ [i] [j] indicates whether [I, J] is feasible as the right son of i-1
  • Enumeration k is the root of [I, J],
  • Update status if (k = = i| lef [i] [k-1]) & & (k = = j| righ [K + 1] [J]) is transferable
  • Finally, if (I = = 1 | lef [1] [I-1]) & & (I = = n | righ [i + 1] [n]) indicates that a complete binary search tree can be established, output Yes

code

#include <iostream>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
const int N=1000;
int a[N];
bool f[N][N],lef[N][N],righ[N][N];
int gcd(int a,int b)
{  
	return b == 0 ? a : gcd(b,a%b); 
}

void solve()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<n;i++)
		for(int j=i+1;j<=n;j++)
			if(gcd(a[i],a[j])>1) 
				f[i][j]=f[j][i]=1;  //Judge whether it can be a father and son 
	for(int i=1;i<=n;i++) 
		f[0][i]=f[i][0]=f[i][n+1]=f[n+1][i]=1;
	for(int i=n;i>=1;i--)
		for(int j=i;j<=n;j++)
			for(int k=i;k<=j;k++)
				if((k==i||lef[i][k-1])&&(k==j||righ[k+1][j]))
				{
					righ[i][j]|=f[k][i-1];
					lef[i][j]|=f[k][j+1];
				}
	for(int i=1;i<=n;i++)
		if((i==1||lef[1][i-1])&&(i==n||righ[i+1][n])) 
		{
			printf("Yes\n"); 
			return ;	
		}
	printf("No\n");
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		memset(f,0,sizeof(f));
     	memset(lef,0,sizeof(lef));
    	memset(righ,0,sizeof(righ));
		solve();
	}
	return 0;
 } 

Topics: less