HDU 4474 yet another multiple problem (BFS + mod operation)

Source: Internet
Author: User

Give you a number N and find the smallest number that is a multiple of N. However, a number cannot be selected.

Solution:
BFS solves the problem by saving all the MOD files and not accessing the same MOD files that have already been accessed.
The new Mod = (mod * 10 + I) % N value is continuously added later.

Address: yet another multiple problem

# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; const int n = 10005; int A [10], n, m, FF, TT, Q [N], pre [N], Val [N]; void print (int x) {If (pre [x]) print (pre [x]); printf ("% d", Val [x]);} void solve () {FF = TT = 0; For (INT I = 1; I <10; I ++) if (I % N = 0 &&! A [I]) {printf ("% d \ n", I); return ;}// for (INT I = 1; I <10; I ++) if (! A [I] & Pre [I % N] =-1) {q [TT ++] = I % N; // mod pre [I % N] Saved by the root node = 0; // pre points to the front-end, and no front-end Val [I % N] = I; // save I, for output} while (FF <TT) {int u = Q [FF ++]; for (INT I = 0; I <10; I ++) if (! A [I]) {int v = (u * 10 + I) % N; If (V = 0) {print (U ); printf ("% d \ n", I); return;} If (pre [v] =-1) // It indicates that no v MOD has been accessed, then add it to the queue Q [TT ++] = V, pre [v] = u, Val [v] = I ;}} puts ("-1 ");} int main () {int CAS = 0, T; while (~ Scanf ("% d", & N) {memset (PRE,-1, sizeof (pre); memset (A, 0, sizeof ()); scanf ("% d", & M); For (INT I = 0; I <m; I ++) {scanf ("% d", & T ); A [T] = 1;} printf ("case % d:", ++ CAS); solve ();} 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.