POJ 1845 Sumdiv (mathematics, multiplication inverse)

Source: Internet
Author: User

Test instructions

Given the numbers a and B, all the factors that require AB (including AB and 1) and mod 9901 results.

Ideas:

Even knowing the formula has to be extrapolated for a while.

It is easy to know that the decomposition is obtained first, then the expression of all the factors is as follows:

All we have to do is calculate the results of the sum%9901.

There are two ways of doing this:

(1) The first step of the sum above is calculated directly by the fast power calculation, and the model is obtained in the process of calculation.

(2) The second step of sum above can be calculated according to the following formula:

     

Also need to use the fast power, the process is slightly more complicated. Note that MB may exceed int.

The following is the code for the second method:

1 //#include <bits/stdc++.h>2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <map>7#include <algorithm>8#include <vector>9#include <iostream>Ten #definePII pair<int,int> One #defineINF 2147483647 A #defineLL unsigned long Long - using namespacestd; - Const DoublePI = ACOs (-1.0); the Const intn=10010; - ConstLL mod=9901; - BOOLIsprime[n]; -LL P[n];//Prime List +  - intGet_all_prime (intN//Sieve method for all [0~n] primes, returns the prime table size + { Amemset (IsPrime,1,sizeof(IsPrime)); at     intCnt=0; -      for(intI=2; i<n; i++) -     { -         if(!isprime[i])Continue; -p[cnt++]=i; -          for(intJ=i*i; j<n; J+=i) isprime[j]=0; in     } -     returnCNT; to } +  -ll _mul (ll a,ll b,ll MoD)//A*b will not overflow if it is to be operated in addition form the { *a%=MoD; $LL r=0;//ResultsPanax Notoginseng      while(b) -     { the         if(b&1) r= (r+a)%MoD; +A= (a+a)%MoD; Ab>>=1; the     } +     returnR; - } $  $ll Pow (ll a,ll b,ll MoD)//fast power function modulus - { -a%=MoD; theLL r=1;//Results -      while(b)Wuyi     { the         if(b&1) r=_mul (r,a,mod); -A=_mul (a,a,mod); Wub>>=1; -     } About     returnR; $ } -  - ll Cal (ll A,ll B) - { ALL ans=1; +      for(intI=0; p[i]*p[i]<=a; i++)//all quality factors of a are first asked the     { -         if(a%p[i]==0) $         { the             intCnt=0; the              while(a%p[i]==0)//take all the light the             { thecnt++; -A/=P[i]; in             } theLL mb=mod* (p[i]-1); theans*= (Pow (p[i), cnt*b+1, MB) +mb-1)%mb/(p[i]-1) ;//to prevent negative numbers from appearing Aboutans%=MoD; the         } the     } the  +     if(A >1)//Don't miss it, a^b is the factor. -     { theLL mb=mod* (A-1);Bayians*= (Pow (A, B +1, MB) +mb-1)%mb/(A-1) ;//to prevent negative numbers from appearing theans%=MoD; the     } -  -     returnans; the } the  the intMain () the { -     //freopen ("Input.txt", "R", stdin); the Get_all_prime (N); the     intA, B; the      while(~SCANF ("%d%d",&a,&B))94printf"%lld\n", Cal (A, b)); the     return 0; the}
AC Code

POJ 1845 Sumdiv (mathematics, multiplication inverse)

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.