P5020 monetary system solution to a problem

Original title link

Briefly meaning of the questions:

Currency minimum requirements of a given length equivalent monetary system. The length of this request money system. See the definitions of the equivalent subject, will not be repeated.

This article may be used a number of set theory, please be safe to eat.

A algorithm

\ (n = 2 \) , simply determining the number of multiples of two. There are multiple relationships, the answer is \ (1 \) , otherwise \ (2 \) .

Time complexity: \ (O (T \ n-Times) \) .

Actual score: \ (15pts \) .

Algorithms two

\ (n = 3 \) , first, if the two numbers are multiples of another number, then the answer is \ (1 \) .

Otherwise, if there are still multiple relationships, the answer is \ (2 \) .

The rest of the case, only need to consider, and the minimum number of times a small number could indicate what the maximum number.

It can be compared to \ (2 \) , otherwise, \ (3 \) .

There are many ways determined. such as:

  1. Violence, directly in buckets, \ (O (\ max a_i) \) .

  2. Consider solving equations with \ (\ texttt {exgcd} \ ) write, \ (O (\ log \ max a_i) \) .

In short, the time complexity is \ (O (T \ the n-Times \ Times (\ max a_i)) \) . (No need optimization, it is not necessary)

Actual score: \ (30pts \) .

Algorithm for Three

First, assume that given \ (S \) , answer is \ (T \) certainly has:

\[T \in S \]

Let's prove this conclusion.

If \ (T \ Not \ in S \) , is considered taken \ (X = \ min (I \ in S) \) , \ (Y = \ min (I \ in T) \) .

If \ (the X-<the y-\) , then \ (x \) it can not be expressed.

If \ (the y-> the X-\) , then \ (y \) can not be expressed.

If \ (the y-the X-= \) , then continue recursion, is proved.

So, we just need to find the answer to the monetary system given.

Here we enumerate selected answers, then 11 verification.

Time complexity: \ (O (n-2 ^ \ n-Times \ Times (\ max a_i) \ Times T) \) , can pass.

Actual score: \ (65pts \) .

Optimal Four

NOTE: \ (the n-25 = \) , I did not think nothing \ (O (2 ^ n \ times T) \) had lost algorithms, so this part of the file may be used to divide the players messing around. ? ? ?

You do not need to dwell on discovery. First you have to sort out.

You just use the existing number of current judges are currently making this number can be expressed.

Can, then no need to explain this number, put it away.

Otherwise, you must choose. Just because, \ (> \) its number means no, and \ (<\) also said that not only that he can express himself, it must choose.

Well, we can uniquely determine whether a number is selected. (Sorted from small to election)

How can I tell?

We found that each new addition of a number \ (the X-\) , we can determine the need to maintain bucket.

In this case, in the original bucket, each number can be expressed in \ (K \) , the \ (k + x, k + 2x, k + 3x \ cdots k + \ infty x \) all as may judgment. This is obvious.

As \ (\ infty \) upper limit, as long as the standard \ (\ max a_i \) can be, because the number of the latter useless.

Time complexity: \ (O (n-\ Times T \ Times (\ max a_i)) \) .

Actual score: \ (100 pts \) .

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;

const int N=5e4+1;

inline int read(){char ch=getchar();int f=1;while(ch<'0' || ch>'9') {if(ch=='-') f=-f; ch=getchar();}
	int x=0;while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f;}

int T,n,a[N];
int cnt=0;
int maxi; bool h[N];

int main(){
	T=read(); while(T--) {
		n=read(); maxi=0; cnt=0;
		memset(h,0,sizeof(h)); //初始化
		for(int i=1;i<=n;i++) a[i]=read(),maxi=max(maxi,a[i]);
		sort(a+1,a+1+n); //排序
		for(int i=1;i<=n;i++)
			if(!h[a[i]]) { //不能被表示
				h[a[i]]=1; cnt++;
				for(int j=1;j<=maxi;j++)
					if(h[j]) {
						for(int k=a[i];j+k<=maxi;k+=a[i])
							h[j+k]=1;
					} //维护能被表示的桶
			} printf("%d\n",cnt);
	}
	return 0;
}

Guess you like

Origin www.cnblogs.com/bifanwen/p/12576482.html