HDU 1852 (Fast power mode + formula for modulo when there is division)

Source: Internet
Author: User

Beijing 2008

Time limit:1000/1000 MS (java/others) Memory limit:32768/65535 K (java/others)
Total submission (s): 741 Accepted Submission (s): 291


Problem Descriptionas We all know, the next Olympic games would be held in Beijing in 2008. So the year seems a little special somehow. You're looking forward to it, too, aren ' t? Unfortunately there still is months to go. Take it easy. Luckily you meet me. I have a problem for you to solve. Enjoy your time.

Now given a positive an integer N, get the sum S of all positive integer divisors of 2008N. Oh No, the result is much larger than you can think. But it's OK to determine the rest of the division of S by K. The result is kept as M.

Pay attention! M is not the answer we want. If you can get the 2008M, that'll be wonderful. If it is larger than k, leave it modulo k to the output. See the example for N = 1,k = 10000:the positive integer divisors of 20081 is 1, 2, 4, 8, 251, 502, 1004, 2008,s = 3780, M = 37 2008M% K = 5776.

Inputthe input consists of several test cases. Each test case is contains a line with the integers N and K (1≤n≤10000000, 500≤k≤10000). N = K = 0 ends the input file and should not be processed.

Outputfor each test case, in a separate line, please output the result.

Sample INPUT1 100000 0

Sample Output5776 Harvest is very big!. Previously for the division modulo operation only know with the inverse of the element can be counted, but when the two number of non-reciprocity, do not know how to get. Today, we got two formulas. The first formula is done by itself when thought might be possible, then really AC, and then go to verify that there are really: 1. (A/b)%mod=a% (b*mod)/b%mod (get this formula good excitement)

2. (A/b)%mod=a*b^ (mod-2)%mod,mod is a prime number (can be proved by inverse) (the formula words feel if the mod is a prime number, directly with the inverse of the same, you can refer to my blog hdu1452)

Then the problem is not difficult, the 2008 decomposition into 251*2^3 and then the factor and the first formula to remove the denominator 250, and then you can get M, in the fast power calculation is good.

#include <stdio.h>#include<iostream>using namespaceStd;typedefLong LongLL; ll Pow_mod (ll a,ll n,ll MoD) {ll ans=1;  while(n) {if(n&1) ans = a*ans%MoD; A=a*a%MoD; N=n>>1; }    returnans;}intMain () {LL n,k;  while(SCANF ("%lld%lld", &n,&k)!=eof,n&&k) {k= -*J; LL M= ((Pow_mod (251, n+1, K)-1) * (Pow_mod (2,3*n+1, K)-1))%K; M= m/ -; K/= -; LL ans=pow_mod ( -, m,k); printf ("%lld\n", ans); }    return 0;}

HDU 1852 (Fast power mode + formula for modulo when there is division)

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.