[With] more than the shortest Luo Gu P2662 cattle fence

Congruences shortest part [shortest] congruence jumping machine P3403 / P2371 ink in the ink equation

[P2662] cattle fence

Topic background

L by helping small mud Meng, successfully resolved the question of amending the binary tree, and therefore wrote a paper,

Successfully submitted to the fork Institute (envy not?). Dickens's hard work and his successful switch system during my graduate, was admitted to the Guanghua School of Management, Peking University! After graduation, with a strong foundation in economics and computer science at their accumulated, the successful construction of a modern dairy farm!

Title Description

Cows are very smart, he intended L wits and small cattle farm in the construction of the fence! There are small L N kinds of wood fence can be built, the length are l1, l2 ... lN, each timber of infinite length.

When built, he would all selected wood spliced ​​together, the length of the fence is his use of timber and length. But smart small length L soon found that many are not obtained by adding the length of the timber and decided when necessary to cut a portion of the timber later reuse.

However, because of relatively small L savings, he set himself: Any timber can only be a short cut up to M meters. Of course, each of the timber scraped wood does not require all the same length. However, because the measurement tools are too primitive, small integer L only accurate slashing meters of wood, so if he has two lengths of wood were 7 and 11, each can only cut one meter, then in effect there are four lengths of wood may be used, are 6, 7, 10, 11.        

Because of the small L believe my cows unique in the world, so let them design their own fence. The cows do not want their peers and are limited in the game fence, so want to make things difficult at small L, L little hope of wood no matter through what process, length and the total length of the fence can not get their designs. But small L know, if the length of the fence is too small, small L will soon find that it is not good to build. So she wants your help to find the maximum length of the fence can not be built.

This must beat clever you! If you can help solve this problem of small L, maybe he'll give you the last 1/8 of the assets of Oh!

Input and output formats

Input formats:

 

The first line of input contains two integers N, M, respectively, represents the maximum value of each type of wood and timber scraped off. The following lines each an integer li (1 <li <3000), represents the original length of the i-th timber.

 

Output formats:

 

Only one line of output contains an integer indicating the maximum length of the fence can not be built. If the length of the fence can be of any construction or the maximum value does not exist, the output of -1.

 

Sample input and output

Input Sample # 1:
2 1
7 11
Output Sample # 1:
15

Explanation

40 % :1< N< 10,  0< M< 300

100 % :1< N< 100,  0< M< 3000 

Thinking

Something about the shortest congruence already written, here is directly related to this question.

Still by some numbers to scrape together a number of the remaining lines, This question due to multipath conditions M, we need to put all the violence can seek out the numbers, and let that be the smallest surplus provides the Department of x.

Then run through all numbers can be used, and the remaining lines built x side, and finally run the shortest path.

The maximum number of Couchu can not find the time, we must first consider the significance d array. d [i] is the other figures can scrape out the minimum number% x = i, then d [i] + x, d [i] + 2x, d [i] + 3x ... d [i] than jumping All the x's to achieve.

Then digital% x = i, and the maximum scrape out is d [i] -x.

It is clear, and all the d [i] -x seek out, which takes a maximum value.

But there are places to note, there is this question requires output -1. Wherein the case of outputting -1 is not the number of hash out, then we can use the timber as long as the presence of 1, all the natural length can be achieved.

Another case is that the maximum value does not exist. There are two considerations direction A is obtained GCD of all numbers, if it is not equal to 1, a series of natural numbers not scrape out. Because we can scrape out of this number must be a multiple of gcd, it is not a time there must scrape out the vacancy. Another method is to find the last time ans have a way to see if d [i]> max (max is a maximum run d array disposed before the shortest), and if so, then there must be a string of% x = i Minato figures are not out. (In fact, this practice is roughly equivalent to guess the use of the data is relatively small)

Code:

#include<iostream> 
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n,m;
int x,a[101];
int ver[5100001],Next[5100001],head[2600001],edge[5100001],tot,d[2600001],vis[2600001],ans=0;
queue<int>q;
void add(int x,int y,int z){
    ver[++tot]=y;
    Next[tot]=head[x];
    edge[tot]=z;
    head[x]=tot;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    sort(a+1,a+n+1);
    x=max(1,a[1]-m);
    if(x==1){
        printf("-1");
        return 0;
    }
    for(int i=1;i<=n;i++){
        for(int j=max(a[i-1]+1,a[i]-m);j<=a[i];j++){
            if(j!=x){
                for(int k=0;k<x;k++){
                    add(k,(k+j)%x,j);
                }
            }
        }
    }
    memset(d,0x3f,sizeof(d));
    d[0]=0;
    q.push(0);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];i;i=Next[i]){
            int v=ver[i],z=edge[i];
            if(d[v]>d[u]+z){
                d[v]=d[u]+z;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    d[x]=0;
    for(int i=1;i<x;i++){
//        printf("%d %d\n",i,d[i]);
        if(d[i]>100000000){
            printf("-1");
            return 0;
        }
        ans=max(ans,d[i]-x);
    }
    printf("%d",ans);
    return 0;
}

the above.

Guess you like

Origin www.cnblogs.com/chloris/p/11021092.html