HDU 5109 (number of constructs + degree of understanding of modulo)

Source: Internet
Author: User

Alexandra and A*b problem

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 802 Accepted Submission (s): 211


Problem Descriptionalexandra has a little brother. He is new to programming. One day he is solving the following problem:given the positive integers A and B, output a*b.
This problem was even easier than the last one. Alexandra can ' t wait to give him another task:given a positive integer A and a string s (s only contains numbers.), find T He minimum positive integer B, such that S is a substring of T, where T is the decimal notation of a*b.
See the sample for better understanding.
Note:s can contain leading zeros, but T can ' t.

Inputthere is multiple test cases (no more than 500). Each case contains a positive integer A and a string S.
A≤10,000,≤ |s| 8 .

Outputfor each case, output the required B. It is guaranteed this such B always exists.
to C + + programmers:if you want to output 64-bit integers and use "%i64d" specifier or cout.

Sample INPUT6 896 192 00861 1

Sample Output3250431 Test Instructions: Give a number A, and a string s (lens<=8) to find a minimum number B, so that s is a continuous sub-sequence of a*b = T. Problem Solving: Very ingenious topic. Suppose t = xsy, then we can write the expression T = (X*10^lens+s) *10^len+y, and because t%a = = 0 So for each part of the smallest T-equation, we can take a modulo, so we can know x<a, because if x >= A, we can make the x smaller by the modulo operation, then if s[0]==0, then the lower limit of X is 1, otherwise the lower limit of x is 0, then for 10^len we can also know 10^len<= a <= 10^4, and then we turn out: set k = (x* 10^lens+s) *10^len SO (k+y)%a=0y = (-k%a+a)%a (Y<10^len) So the final answer is obtained by enumerating the X,10^len.
#include <cstdio>#include<cstring>#include<vector>#include<algorithm>using namespaceStd;typedefLong LongLL;intMain () {LL A; Chars[ -];  while(SCANF ("%lld%s", &a,s)! =EOF) {        intLen =strlen (s); if(strcmp (s),"0")==0) {printf ("0\n"); Continue; } LL LS=1, mid=0;  for(intI=0; i<len;i++) ls*=Ten;  for(intI=0; i<len;i++) {Mid= mid*Ten+s[i]-'0'; } LL ans= -1;  for(LL i=1; i<=10000; i*=Ten){             for(LL j= (s[0]=='0'); j<a;j++) {LL T= (j*ls+mid) *i; LL y= (a-t%a)%A; if(y>=i)Continue; if(ans<0) ans = t+y; Elseans = min (t+Y,ans); }} printf ("%i64d\n", ans/a); }    return 0;}

HDU 5109 (number of constructs + degree of understanding of modulo)

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.