HDU ACM 4474 Yet another multiple problem-> number theory (digital BFS)

Source: Internet
Author: User

Test instructions: N and m,m decimal Unit Number (0-9), the smallest number of ans, which causes ans to be a multiple of n (except 0), and ans does not include the input decimal number. There is no output-1.

Using BFS Search, small to large enumeration can ensure that the number of construction is incremental, if you do not judge the direct search, the complexity is very high. If a number%n==0, this number is a multiple of N. In the absence of the time, if a%n==b%n, and a<b, then actually can take a without taking B, because if the end of a C can make ac%n==0, then bc%n is equal to 0, if a and B after the addition of%n==0, the optimal conditions will be the same number of additions. So just maintain the combination of the number of%n value can, if the search on the way to the same%n value, it can be ignored, certainly not the previous better.

#include <iostream> #include <queue>using namespace std; #define N 10010int p[n];      PRE[V] Record reaches V of the previous node uint num[n];    NUM[V] records the last digit of the form v bool a[10];    Dropped data int n;void Init () {memset (p,-1,sizeof (P)); memset (num,-1,sizeof (num)); memset (A,false,sizeof (a));} void Show (int u)        //Recursive print result {if (p[u]!=-1) Show (P[u]);p rintf ("%d", Num[u]);} void BFs () {int i,t,u;queue<int> q;for (i=1;i<10;i++)  //Note 0 does not count, to remove if (!a[i]) {t=i%n;if (t==0)             // The result is only a single digit case {printf ("%d", i); return;} Q.push (t); num[t]=i;} while (!q.empty ()) {U=q.front (), Q.pop (), for (i=0;i<10;i++) if (!a[i]) {t= (10*u+i)%n;if (num[t]==-1)//////not seen before {Q.push (t);p [T]=u;num[t]=i;} if (t==0) {Show (t); return;}}} printf ("-1");} int main () {int k=0,m,x;while (scanf ("%d%d", &n,&m) ==2) {Init (); while (m--) {scanf ("%d", &x); a[x]=true;} printf ("Case%d:", ++k), BFS ();p utchar (' \ n ');} return 0;}


HDU ACM 4474 Yet another multiple problem-> number theory (digital BFS)

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.