Nine degrees OJ topic 1085: Seeking root (N, K) Tsinghua 2010 Machine Test Topic

Source: Internet
Author: User

Title Description:

N<k, root (n,k) = N, otherwise, root (n,k) = root (N ', K). n ' is the sum of the numbers represented by the K-binary of N. Input x,y,k, output root (x^y,k) value (here ^ is a power, not XOR), 2=<k<=16,0<x,y<2000000000, half of the test point X^y will overflow the range of int (>= 2000000000)

Input:

Each set of test data includes one row, X (0<x<2000000000), Y (0<y<2000000000), K (2<=k<=16)

Output:

Input may have multiple sets of data, for each set of data, the value of root (X^y, k)

Sample input:
4 4 10
Sample output:
4

I have been pondering the problem for a long time, the most troubling to me is the scope of the data, 2000000000 of 2 billion times how big, how to calculate?
Each time the K-ary of N is required to represent the sum of the figures, then one to take the mold, two requirements division, so large number how to do. The topic test is not a large number of operations, there must be some kind of law, then what is this law?

In the process of thinking, the first consideration is 1. (a*b) mod n = (A * (b mod n)) mod n;
Second, it also takes into account
Each time the K-ary of N is required to represent the sum of the numbers, then the last one and that is 2. N mod (k-1)  (this is seen by the law)

But then their own ideas are more chaotic, do not know how to deal with the X-y side of the problem, think for a long time did not think out.

Finally can not help but look at the other people's puzzle,
They did ask for X's Y-side, and used the iterative method to seek, and in the process of seeking direct modulo operation, to avoid data overflow. The gap is here.
The code is as follows
1#include <cstdio>2#include <cstring>3 4typedefLong Longll;5 6 7 intRoot (ll x, ll y, ll K) {8ll mi =1;9      while(y) {Ten         if(Y &1) { OneMi = (mi*x)%K; A         } -x = (x * x)%K; -y = y >>1; the     } -     returnmi; - } -  + intMain () { - ll X, Y, K; +      while(SCANF ("%lld%lld%lld", &x, &y, &k)! =EOF) { All ans = root (x, Y, K1); at         if(ans = =0) { -Ans = k-1; -         } -printf"%lld\n", ans); -     } -     return 0; in}

About 2nd
n=a0+a1*k+a2*k^2+......an*k^N N'=a0+a1+a2+......+anN-N'=a1* (k-1) +a2* (k-1) ^2+a3* (k-1) ^3+......+an* (k-1) ^n(N-N')% (k-1) =0(N'- N"')% (k-1) =0     ..... (N (R-1)-N (R))% (K-1)=0Add to (N-N (r))% (K-1)=0N (R)=n% (K-1) therefore (x^y)% (K-1) is what we ask for. When (x^y)% (K-1) =0, note that the result is K-1



Nine degrees OJ topic 1085: Seeking root (N, K) Tsinghua 2010 Machine Test Topic

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.