POJ 1426 Find The multiple idea, linear congruence, search difficulty: 2

Source: Internet
Author: User

http://poj.org/problem?id=1426

Tested, all values from 1-200 have a long long solution, so can be directly stored with a long long

Starting from 1, each transfer to 10*s and 10*s+1, only the remainder can be stored,

For the remainder I, only the first remainder of I is the most useful, only record this value can be

#include <cstdio> #include <cstring> #include <queue>using namespace Std;const int Maxn=222;typedef                Long Long ll;ll dp[maxn];int n;queue<int >que;int main () {while (scanf ("%d", &n) ==1&&n) {                if (n==1) {puts ("1"); continue;}               Memset (Dp,-1,sizeof (DP));               while (!que.empty ()) Que.pop ();               Dp[1]=1;               Que.push (1);               BOOL Fl=false;                        while (!que.empty ()) {int S=que.front (); Que.pop ();                                if (s==0) {printf ("%i64d\n", Dp[s]);                                Fl=true;                        Break                        } int t=s*10%n;                                if (dp[t]==-1) {dp[t]=10*dp[s];                        Que.push (t);                        } t= (s*10+1)%n;                   if (dp[t]==-1) {             dp[t]=10*dp[s]+1;                        Que.push (t);        }} if (!FL) puts ("-1"); } return 0;}

  

POJ 1426 Find The multiple idea, linear congruence, search difficulty: 2

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.