[BFS + remainder judgment + path Record] HDU 4474 yet another multiple problem

Source: Internet
Author: User

Question:

Give N and M numbers (one digit)

The multiples of the minimum N value do not include the M number and output-1 does not exist.

Ideas:

First, it is possible that this number is too long, so it cannot be solved by brute force.

Therefore, this question should be a BFS

Why can I use the remainder?

For the current remainder into the queue, it must be the minimum value of the remainder

Next, how should we add things that meet the conditions?

Therefore, the remainder can be determined again, similar to the digital DP record method.

Then add a path record.

Code:

# Include "cstdlib" # include "cstdio" # include "cstring" # include "cmath" # include "queue" # include "algorithm" # include "map" # include "iostream" using namespace STD; int used [12]; struct node {int F, pre, now; // F indicates whether or not, pre precursor, current value of now} mark [12345]; int N, m; void DFS (int x) // print path {If (MARK [X]. PRE) DFS (MARK [X]. PRE); printf ("% d", Mark [X]. now);} void BFS () {int cur, next; queue <int> q; For (INT I = 1; I <= 9; I ++) // if there is no leading 0, put one digit first. {If (used [I]) continue; q. push (I); Mark [I]. F = 1; Mark [I]. pre = 0; Mark [I]. now = I;} while (! Q. empty () {cur = Q. front (); q. pop (); For (INT I = 0; I <10; I ++) {If (used [I]) continue; next = (cur * 10 + I) % N; If (MARK [next]. f) continue; Mark [next]. F = 1; Mark [next]. now = I; Mark [next]. pre = cur; If (next = 0) // satisfies the multiple {DFS (next); return;} Q. push (next) ;}} printf ("% d",-1) ;}int main () {int CAS = 1; while (scanf ("% d ", & N, & M )! =-1) {memset (used, 0, sizeof (used); For (INT I = 0; I <m; I ++) {int X; scanf ("% d", & X); used [x] = 1;} printf ("case % d:", CAS ++ ); if (n <10 &&! Used [N]) // special sentence {printf ("% d \ n", n); continue;} memset (mark, 0, sizeof (Mark )); BFS (); puts ("");} return 0 ;}


[BFS + remainder judgment + path Record] HDU 4474 yet another multiple problem

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.