HDU 5690 all x multiple algorithms (2016 "Baidu Star"-Preliminary (Astar round2a) 1001)

Source: Internet
Author: User
Tags cas

Transmission Door
All X

Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 965 Accepted Submission (s): 462

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≤1010

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".

The first is on this blog on the (brute force algorithm of the Exhaustion sieve method) Click here
I'm not going to repeat it.
The second is the fast power:
Let's take a look at this thing first, make ans = xxx.xxx (m), then we can write
aNs=x?( ten 0 + ten 1 +...+< Span class= "mn" id= "mathjax-span-6001" style= "Font-family:mathjax_main;" >10 m 1 )
So it's a geometric series, so we can ask for the formula.
ten 0 + ten 1 +...+< Span class= "mn" id= "mathjax-span-6023" style= "Font-family:mathjax_main;" >10 m 1 = < Span class= "mn" id= "mathjax-span-6033" style= "Font-family:mathjax_main;" >10 m Span style= "Display:inline-block; width:0px; Height:2.509em; " > ? 1 9
Then we are asking x* (10^m-1)/9 MOD K is not ==c so, here is a denominator we how to deal with, there must be someone in the thinking of the inverse, but GCD (9,k) is not necessarily equal to 1 ah, so the method can not be used to find the inverse, then how to do it, We can expand by 9 times times the x * (10^m-1) MOD 9k is not equal to 9 * C, the rest is
Fast power.
On the code:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>Using namespace Std;typedef long long LL; ll Quick_mod (ll A,ll B, ll c) {ll ans =1; while(b) {if(b&1) ans = (ans*a)%c; b>>=1; A = (a*a)%c; }returnAns;}intMain () {LLx,m, C, K;intT scanf"%d", &t); for(intcas=1; cas<=t; cas++) {cin>>x>>m>>k>>c; K*= 9; C*= 9; LL ans = quick_mod (Ten,m, k); Ans = (ans%k+K)%k; Ans = (ans-1%k+K)%k; Ans = (ans*x%k+K)%k;printf("Case #%d: \ n", CAs);if(ans = = c) puts ("Yes");ElsePuts"No"); }return 0;} /**_ooooo_O8888888o the" . " the(| -_- |) O\ =/ o ____/' ---' \____. '     \\| |//  `.  /  \\||| :  |||///_||||| -:- |||||   -             | | \\\  -/// |           | |  \_| "\---/"'|           | \  .-\__  `-`___/-. /         ___' ... ' /--.--\  `. . __."" ' < '. ___\_<|>_/___. '>' "". | | :  `- \`.; `\ _ /`;.     `/ - ` : | |   \  \ `-. \_ __\/__ _/.-'//====== '-.____ '-.___\_____/___.-' ____.-'======' =---= ' ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ the ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^ Buddha bless each ac**/

The Third kind: is to seek the circulation festival, this should not be difficult to put, I say a bit on the line, note that the loop is not necessarily from the beginning of the first, pay attention to this on the line, the specific operation of their own implementation.

HDU 5690 all x multiple algorithms (2016 "Baidu Star"-Preliminary (Astar round2a) 1001)

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.