Timus 1547. Password Search "Test instructions idea + large number template"

Source: Internet
Author: User

Topic Address Portal: URAL 1547

This problem needs to use a large number of many templates, recommend everyone to brush!

The main idea: Vova forgotten in Timus OJ above the password, the password is composed of lowercase letters (a~z), he only know that the password length is not greater than n bit, now he needs to use M data processor to retrieve the password, where the order of retrieval needs to satisfy the dictionary order. For example, his password length is not greater than 2, then you need to retrieve a, b,.........., y,z,aa,ab,.........., zy,zz. The retrieval interval of each data retriever is output, so that the total retrieval efficiency can be reached the highest.

The total number of known passwords may be no less than the number of data processors.

For this topic, it was a bit confusing at first. Think that only once the 26 binary can represent all the numbers (A to zzzzz..........z), and later found that it is not possible

And then we find that there's this kind of password, and there's a feature:

When the password length is one bit (a, b,.........., z): The number is 26, that is, the password length is not greater than 1 total is 26

The length is two bits (Aa,ab,.........., zz): The number is 26*26, that is, the password length is not greater than 2 total is 26+26^2

..........

The number of length n bits is: 26^n, that is, the password length is not greater than n total is 26+26^2+..........+26^n

In order to maximize the efficiency of each data processor, it is necessary to require the number of intervals, where the remainder and quotient are obtained by the large number of divisions, thus determining the number of intervals and the interval number of the beginning and ending of each data processor.

Converts the interval number to the corresponding string of the corresponding password length digits (converts decimal to 26 binary), outputting the corresponding result.

This problem needs to use a lot of large number of templates, so I wrote a little, ready to use

1. Compare the size of a string (does not contain 0 prefixes, and the elements in the string are only ' 0 ' ~ ' 9 ' constitute

/*  S1 greater than S2, return 1  s1 less than S2, return 1  s1 equals s2, return 0*/int cmp (char *s1,char *s2) {int Len1=strlen (S1), Len2=strlen (S2), I if (LEN1>LEN2) return 1;else if (len1<len2) return-1;for (i=0;i<len1;i++) if (S1[i]>s2[i]) return 1;else if ( S1[i]<s2[i]) Return-1;return 0;}

2. Multiply by a large number , where only the results of shaping and string multiplication are rendered

/* The result of   string s2 after executing the function is the result of string S1 and shaping X (x>=0) */void multi (char *s2,char *s1,int x)  {int A[330],len1,i,cnt=0;memset (A,0,sizeof (a)); Len1=strlen (S1); for (i=0;i<len1;i++) a[i]=s1[len1-i-1]-' 0 "; for (i=0;i<len1;i++) a[i]*=x;for ( i=0;i<len1+5;i++) {a[i+1]+=a[i]/10;a[i]%=10;} for (i=len1+5;i>0;i--)//If the result of the multiplication may be 0, then you cannot place I in the For loop as I>=0             if (A[i]) break;strcpy (S2, ""); for (; i>=0;i-- ) s2[cnt++]=a[i]+ ' 0 '; s2[cnt]= ';}
3. Sum of large numbers
   /* After the function is executed, the string S3 returns the large number of string representations S1 and S2 and */void Add (char *s3,char *s2,char *s1) {int A1[330],a2[330],len1=strlen (S1), Len2 =strlen (S2), Len,i,cnt=0;memset (a1,0,sizeof (A1)), Memset (a2,0,sizeof (A2)); len= (len1>len2?len1:len2); for (i=0;i <len1;i++) a1[i]=s1[len1-i-1]-' 0 '; for (i=0;i<len2;i++) a2[i]=s2[len2-i-1]-' 0 "; for (i=0;i<len+2;i++) a1[i]= A1[i]+a2[i];for (i=0;i<len+2;i++) {a1[i+1]+=a1[i]/10;a1[i]%=10;} for (i=len+2;i>0;i--)  //If considering that the add result may be 0, you cannot set I to i>=0 in the for loop condition, other conditions can generally be reserved if (A1[i]) break;strcpy (S3, ""); for (; i>=0;i--) s3[cnt++]=a1[i]+ ' 0 '; s3[cnt]= ';}

4. Large number of self-added

/*   Returns the effect of the large number of S1 since add 1 */void addpp (char *s1) {int I,len=strlen (S1), A[330],cnt=0;memset (A,0,sizeof (a)); for (i=0;i< len;i++) a[i]=s1[len-1-i]-' 0 '; a[0]++;for (i=0;i<len;i++) {a[i+1]+=a[i]/10;a[i]=a[i]%10;} for (i=len+2;i>=0;i--) if (A[i]) break;for (; i>=0;i--) s1[cnt++]=a[i]+ ' 0 '; s1[cnt]= ';}
5. Large number Subtraction
     /* After executing the function S3 represents the result of S2 minus S1, where S2 is greater than or equal to S1*/void minus (char *s3,char *s2,char *s1)  {int A1[330],a2[330],i,len1=strlen (S1), Len2=strlen (S2), Cnt=0;memset (a1,0,sizeof (A1)); Memset (a2,0,sizeof (A2)); for (i=0;i<len1;i++) a1[i]=s1[ len1-i-1]-' 0 '; for (i=0;i<len2;i++) a2[i]=s2[len2-i-1]-' 0 "; for (i=0;i<len2;i++) {if (A1[i]>a2[i]) {a2[i+1]-- ; a1[i]=a2[i]+10-a1[i];} Else{a1[i]=a2[i]-a1[i];}} for (i=len2;i>0;i--)//warning            if (A1[i]) break;for (; i>=0;i--) s3[cnt++]=a1[i]+ ' 0 '; s3[cnt]= ';}
6. Large number self-reduction

/* After executing the function S1 Returns the result of S1 1, where S1 is greater than 0void minuspp (char *s1) {int I,len=strlen (S1), A[330],cnt=0;memset (A,0,sizeof (a)); for (i= 0;i<len;i++) a[i]=s1[len-1-i]-' 0 '; a[0]--;for (i=0;i<len;i++) {if (a[i]==-1) {a[i+1]--;a[i]=9;} else break;} for (i=len+2;i>0;i--)//warning              if (A[i]) break;for (; i>=0;i--) s1[cnt++]=a[i]+ ' 0 '; s1[cnt]= ';}
7. Large Number Division(String type in addition to shaping)

     /* Here num represents a global variable, returning a value of S1mod (mm), that is, the remainder, S2 returns the value of s1/mm */int num;void divi (char *s2,char *s1,int mm) {char S3[330];int Len=strlen (S1), i,cnt=0;num=0;strcpy (S3, ""); for (i=0;i<len;i++) {num=num*10+s1[i]-' 0 '; s3[cnt++]=num/mm+ ' 0 '; num =num%mm;} s3[cnt]= ' + '; for (i=0;i<len-1;i++)  //When considering that the S1 may be less than mm, I will need to be i<len-1if (s3[i]!= ' 0 ') in the For Loop break;cnt=0;for (; i<len;i++) s2[cnt++]=s3[i];s2[cnt]= ';}
Template on the first to tidy up these, 1547 this problem are used, and perhaps also has the expression of inaccurate places, but also hope that we point out a lot!


Timus 1547. Password Search "Test instructions idea + large number template"

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.