201,600 Degree Star-Preliminary (Astar round2a) AII X

Source: Internet
Author: User

Problem Description

F (X,m) represents an M-digit number that is all composed of a number x. Please calculate whether the following formula is true:

F (x,m) mod k≡c

Input

The first line is an integer t, which represents the T group data. One row for each set of test data, containing four digits x,m,k,c

1≤x≤9

1≤m≤10^10

0≤c<k≤10,000

Output

For each set of data, output two lines: The first line of output: "Case #i:". I represents the test data for group I. The second line output "Yes" or "No", representing four numbers, whether it can satisfy the formula given in the topic.

Sample Input

3

1 3 5 2

1 3 5 1

3 5 99 69
Sample Output

Case #1:

No

Case #2:

Yes

Case #3:

Yes

Hint

For the first set of test data: 111 MoD 5 = 1, the formula is not true, so the answer is "no", and the second set of test data satisfies the above formula, so the answer is "yes".

Xxxxxxx|xx*xx%k = = C
Because of k<=10000, the remainder range is within [0,10000], so define f[10000] (for labeling purposes)
Because I can not use the M-bit x directly to the K, consider periodicity, make t=0, continue to do Operation T= (t*10+x)%k,f[t]=r (array f is initialized to 0),
to the R (up to 10001) times (this is actually equal to the result of the R-bit x for the K-remainder), there must be duplicate values,
At this point, r-f[t] is the cycle T, that is, we take the T-bit x forward, the result of the K-remainder is still t, so we just need to do it again (M-f[t])%T
(t*10+x)%k operation, which greatly reduces run time

#include <stdio.h>#definell Long LongintOK (intX,ll m,intKintc) {    intf[10000] = {0},r,t=0, left;  for(r=1; r<=m;r++) {T= (Ten*T+X)%K; if(F[t]) { left= (int) ((M-f[t])% (rf[t]));  while(left--) {T= (Ten*T+X)%K; }            returnt = =C; } F[t]=R; }    returnt = = C;//m must be less than 10001}intMain () {intT,x,k,c,i=1;    ll m; scanf ("%d",&T);  while(t--) {scanf ("%d%lld%d%d",&x,&m,&k,&c); printf ("Case #%d\n", i++); Puts (OK (x,m,k,c)?"Yes":"No"); }    return 0;}//1 5 5 1 Yes//1 3 5 2 No//1 3 5 1 Yes//3 5 Yes//8 10000000000 No//7 9999998777 Yes

201,600 Degree Star-Preliminary (Astar round2a) AII X

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.