POJ 1845 Sumdiv (factor and)

Source: Internet
Author: User

POJ 1845 Sumdiv (factor and)
Sumdiv

Time Limit:1000 MS   Memory Limit:30000 K
Total Submissions:15404   Accepted:3800

Description

Consider two natural numbers A and B. Let S be the sum of all natural divisors of A ^ B. Determine S modulo 9901 (the rest of the division of S by 9901 ).

Input

The only line contains the two natural numbers A and B, (0 <= A, B <= 50000000) separated by blanks.

Output

The only line of the output will contain in S modulo 9901.

Sample Input

2 3

Sample Output

15
This question is basically the same as HDU 1452, but it is the promotion of HDU.
Let's talk about the steps for solving this question first: ① break A Into A = (p1 ^ a1) * (p2 ^ a2 )*..... * (pk ^ ak), then A ^ B is A ^ B = [p1 ^ (a1 * B)] * [p2 ^ (a2 * B)] *..... * [pk ^ (ak * B)] ② sum S based on the formula of the approximate number and mod9901. There are some details below:
At the beginning, I first decomposed the question and calculated it according to the formula of the sum. However, I have been WA and I have changed many places. If a number n = (p1 ^ a1) * (p2 ^ a2 )*..... * (pk ^ ak) approx. and s = (p1 ^ (a1 + 1)-1)/(p1-1) * (p2 ^ (a2 + 1)-1) /(p2-1 )*..... * (pk ^ (ak + 1)-1)/(pk-1) I calculated according to the formula of s, but it has been wrong, it must be because this formula involves division, although division can be solved by using the reverse element in the modulo operation, however, there are restrictions on the reverse element (in the sense of m, the necessary and sufficient conditions for a to have the reverse element are a and m ), I think it must have been difficult to deal with this place, so we have been WA.
In fact, I ignored another formula. The formula s given above is after deformation. Before deformation, it is: s = (1 + p1 + p1 ^ 2 + p1 ^ 3 +... p1 ^ a1) * (1 + p2 + p2 ^ 2 + p2 ^ 3 + .... P2 ^ a2) * (1 + p3 + p3 ^ 3 +... + P3 ^ a3 )*.... * (1 + pk ^ 2 + pk ^ 3 +... pk ^ ak) This formula does not involve division and can be used comfortably. (In this formula, each bracket is an equi-ratio series, and the summation of each is the formula with division above ). Now the problem is clear.
Let's leave this question aside and look at the following Knowledge: A problem similar to this: S (k) = A ^ 0 + A ^ 1 + A ^ 2 + .... + fast sum of A ^ k. Method: Binary + recursive solution. The following is a simple example: ① k = 4 is an even number A ^ 0 + A ^ 1 + A ^ 2 + A ^ 3 + A ^ 4 = (A ^ 0 + A ^ 1) + A ^ 2 + A ^ 3 (A ^ 0 + A ^ 1) = (1 + A ^ 3) * [A ^ 0 + A ^ 1] + A ^ 2 = (1 + A ^ (k/2 + 1) * S (k/2-1) + A ^ (n/2) ② k = 5 is an odd number A ^ 0 + A ^ 1 + A ^ 2 + A ^ 3 + A ^ 4 + A ^ 5
= [A ^ 0 + A ^ 1 + A ^ 2] + A ^ 3 * [A ^ 0 + A ^ 1 + A ^ 2] = (1 + A ^ 3) * [A ^ 0 + A ^ 1 + A ^ 2] = (1 + A ^ (k/2 + 1) * S (k/2) in the preceding formula, the blue part uses the fast power, and the red part continues recursion, so that you can quickly find S (k. Recursive exit n = 0 return 1;
In the code, divi [I] [0] represents the I-th prime factor, and divi [I] [0] represents the number of times this prime number occurs.
# Include
 
  
# Include
  
   
# Include
   
    
# Include using namespace std; typedef _ int64 ll; const int MOD = 9901; ll divi [10000] [2], tot; ll quick_mod (ll a, ll B) {// a ^ B % MODll res = 1; a % = MOD; while (B) {if (B & 1) res = (res * a) % MOD; b/= 2; a = (a * a) % MOD;} return res;} ll cal (int p, int n) {if (n = 0) return 1; if (n & 1) return (1 + quick_mod (p, n/2 + 1) * cal (p, n/2) % MOD; elsereturn (1 + quick_mod (p, n/2 + 1) * cal (p, n/2-1) + quick_mod (p, n/2 )) % MOD;} void pre_solve (ll n) {ll I; tot = 0; for (I = 2 ; I * I <= n;) {if (n % I = 0) {divi [tot] [0] = I; divi [tot] [1] = 0; do {n/= I; divi [tot] [1] ++;} while (n % I = 0); tot ++;} if (I = 2) I ++; else I + = 2;} if (n> 1) {divi [tot] [0] = n; divi [tot] [1] = 1; tot ++ ;}} int main () {ll A, B, I, res; while (scanf ("% I64d % I64d", & A, & B )! = EOF) {if (A = 0) {// do not forget to specify printf ("0 \ n"); continue ;} if (A = 1 | B = 0) {printf ("1 \ n"); continue;} pre_solve (A); res = 1; for (I = 0; I
    
   
  
 

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.