Hdu 4294 Multiple number theory + bfs

Source: Internet
Author: User

This was the topic of the Chengdu online competition the day before yesterday. There was no idea at all during the competition. After reading the report, I still cannot find out the answer, which is too weak.
The meaning of this question is given N, K, M, so that M is a positive multiple of N, and M represents the different numbers ,..., k-1) at least, if there are multiple groups
In this case, find the minimum M.
Very mathematical questions. The conclusion is that the positive multiples of any number can be obtained by a number of no more than two different numbers.
The proof is as follows:
Any number of M % N has N results in total. If N + 1 is different from each other, then there must be two M pairs with the same result after N modulo, this is the so-called Pigeon nest principle.
So, I will take a, aa, aaa,..., aaaaaaaaaa..., N + 1 in total, which also satisfies the above conclusion. Then I subtract the two numbers that share the same modulus of N to get
The number aaaaa... 000... must be a multiple of N.
Based on the above proof, only two digits can be obtained to represent a multiple of N. But it cannot be said that the form is aaaaa... 000 .....

At this point, I still have no idea at all, and I don't know how to search at all...
After thinking for more than an hour, I had no clue. The students who asked this question still had no clue. Looking at the problem-solving report, their code is too good to understand and have no clue.
Maybe I understand bfs too much to understand their search code. In addition, I don't even know where I can search.
After thinking for a long time, when I had dinner yesterday, I finally found that I could search for the remainder.
For any N, the rest is in the range of [0, N-1]. This can actually represent the status, or the point in bfs. Transfer from current remainder to other
The remainder is MOD * K + A or MOD * K + B. If the remainder is not searched before, it can be transferred. This is exactly the same
Optimized. It can also be seen as a subproblem. However, dfs does not work at all. When dfs is used at the beginning, it definitely times out.
I am not very familiar with my ideas when using dfs. I am lucky to think that I can do it... Later, we found that this question exactly matches bfs. [0, N-1] indicates N points.
From a point outside, the shortest traversal is to 0, which can be bfs or the shortest path algorithm. I think another difficulty is to save the answer because the answer length is the longest.
It may be N (N <= 10000), so it is definitely not feasible to put the answer directly into the node. However, I have carefully read the introduction to algorithms. So we thought we could use bfs
Search for the tree or the tree run by the shortest algorithm. Search for the answer from the target node in reverse order, find the Start Node, and then reverse the answer.
Note that 0 cannot be a multiple of N, so pay attention to the handling of bfs (0, I.

The Code is as follows:
# Include <stdio. h>
# Include <string. h>
# Include <queue>
# Include <algorithm>
Using namespace std;

Const int MAX_N = 10010;
Int nOut [MAX_N];
Int nOLen;
Int nAns [MAX_N];
Int nALen;
Bool bMod [MAX_N];
Int nFather [MAX_N];
Int nChoose [MAX_N];
Int nN;
Int nK;
Bool bFind;

Int Cmp (int * A, int nLA, int * B, int nLB)
{
If (nLA! = NLB)
{
Return nLA-nLB;
}
For (int I = 0; I <nLA; ++ I)
{
If (A [I]! = B [I])
{
Return A [I]-B [I];
}
}
Return 0;
}

Void Bfs (int nA, int nB)
{
Memset (bMod, false, sizeof (bMod ));
Queue <int> que;
Que. push (0 );
Int nTemp;
Bool bFirst = true;
BFind = false;

If (nA> nB) swap (nA, nB );
// Printf ("nA: % d, nB: % d \ n", nA, nB );
While (! Que. empty ())
{
// Printf ("nMod: % d \ n", que. front ());
Int nMod = que. front ();
Que. pop ();
If (nMod = 0)
{
If (bFirst) bFirst = false;
Else
{
BFind = true;
Break;
}
}

NTemp = (nMod * nK + nA) % nN;
If (! (NMod = 0 & nA = 0 )&&! BMod [nTemp])
{
NFather [nTemp] = nMod;
NChoose [nTemp] = nA;
Que. push (nTemp );
BMod [nTemp] = true;
// Printf ("nTemp: % d \ n", nTemp );
}
If (nA = nB) continue;
NTemp = (nMod * nK + nB) % nN;
If (! BMod [nTemp])
{
NFather [nTemp] = nMod;
NChoose [nTemp] = nB;
Que. push (nTemp );
BMod [nTemp] = true;
// Printf ("nTemp: % d \ n", nTemp );
}
}

If (bFind)
{
Int nF = 0;
NALen = 0;
Do
{
NAns [nALen ++] = nChoose [nF];
NF = nFather [nF];
} While (nF );
Reverse (nAns, nAns + nALen );
}
}

Int main ()
{
While (scanf ("% d", & nN, & nK) = 2)
{
Bool bOk = false;
NOLen = 0;
For (int I = 1; I <nK; ++ I)
{
Bfs (I, I );
If (bFind)
{
If (nOLen = 0 | Cmp (nOut, nOLen, nAns, nALen)> 0)
{
NOLen = nALen;
Memcpy (nOut, nAns, sizeof (int) * nALen );
}
BOk = true;
}
}
If (! BOk)
For (int I = 0; I <nK; ++ I)
{
For (int j = I + 1; j <nK; ++ j)
{
Bfs (I, j );
If (bFind)
{
If (nOLen = 0 | Cmp (nOut, nOLen, nAns, nALen)> 0)
{
NOLen = nALen;
Memcpy (nOut, nAns, sizeof (int) * nALen );
}
}
}
}
For (int k = 0; k <nOLen; ++ k)
{
Printf ("% d", nOut [k]);
}
Printf ("\ n ");
}

Return 0;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.