POJ 1845 Sumdiv (factor decomposition + fast Power + two-point summation)

Source: Internet
Author: User
Tags modulus two factor

Test instructions: Give you A, B, let a^b all the factors and modulo 9901

Idea: A can be split into the product of the element factor: a = p1^x1 * p2^x2 *...* PN^XN

Then a^b = p1^ (b*x1) * p2^ (B*X2) *...* pn^ (B*XN)

So a^b all the vegetarian factors and is

(p1^0 + p1^1 + p1^2 + ... + p1^ (b*x1)) * (p2^0 + p2^1 + ... + p2^ (b*x2)) * ... * (pn^0 + pn^1 + ... + pn^ (B*XN))

It can be seen that each parenthesis is geometric series, but do not use the geometric series formula, because there is division (at first I use division, then the inverse of the mold, WA to cool dead), because not necessarily meet the multiplicative inverse of the conditions required, divisor and modulus may be non-reciprocity (divisor may be modulus of how many times). Since the formula cannot be used, it is necessary to draw on the two points. For example, the following formula sums: A1+a2+a3+a4 = A1+A2+A2 (A1+A2). Through this formula found, as long as the A2 on the line, and then as long as the calculation of a a1+a2, you can dispense with half of the computational capacity. Then the same a1+a2 can continue to score.

Now spread to the general formula. A1+a2+...+an

1) n is even: A1+a2+...+an = a1+a2+ ... +a (N/2) + A (N/2) (A1+a2+...+a (N/2))

2) n is odd: A1+a2+...+an = a1+a2+ ... +a (N/2) + A (N/2) (A1+a2+...+a (N/2)) + an

The introduction of these can be recursive solution.

Note : To find a factor, hit a prime table, only to hit the sqrt (n) on the line, because it is only possible in sqrt (n), if there is a greater than sqrt (n) two factor, the product is naturally greater than n, so only need to sqrt (n) can be. Because even if there is a large prime and a small prime number multiplied, then in the case of the small prime number, only a large number of prime, which will go directly to the bad judgment.

#include <cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespaceStd;typedefLong Longll;Const intMAXN =10010;Constll mod =9901ll;ll A, B;structFactor {ll FAC; ll CNT;} FACTOR[MAXN];inttot;BOOLPRIME[MAXN + -];intPR[MAXN];//Prime Listintpr_cnt;voidInit_prime () {memset (prime,true,sizeof(prime)); prime[0] = prime[1] =false;  for(inti =2; I * I <= MAXN; i++)        if(Prime[i]) for(intj = i + i; J < Maxn; J + =i) prime[j]=false; Pr_cnt=0;  for(inti =2; I <= MAXN; i++)        if(Prime[i]) pr[pr_cnt++] =i;}voidInit ()//Find all the vegetarian factors{tot=0; Memset (Factor,0,sizeof(factor));  for(inti =0; I < pr_cnt && Pr[i] <= A; i++)    {        if(A% pr[i] = =0) {FACTOR[TOT].FAC=Pr[i];  while(A% pr[i] = =0) {factor[tot].cnt++; A/=Pr[i]; } factor[tot].cnt*=B; Tot++; }    }    if(A >1) {FACTOR[TOT].FAC=A; Factor[tot++].cnt =B; }}ll Quickpow (ll A, ll B, ll MoD) {ll ans=1LL;  while(b) {if(B &1) ans = ans * a%MoD; A= A * A%MoD; b>>=1; }    returnAns%MoD;} ll Binary_pow (ll A, ll B, ll MoD)//Calculate the geometric series and{    if(b = =0)return1LL; if(b = =1)returnA; ll ans=0; if(B &1) {ans=Quickpow (A, B, MoD); Ans= (ans + (Quickpow (A, B/2, MoD) + 1LL)% mod * BINARY_POW (A, B/2, MoD))%MoD; }    Elseans= (Quickpow (A, B/2, MoD) + 1LL)% mod * BINARY_POW (A, B/2, MoD)%MoD; returnans;}voidsolve () {if(B = =0) {puts ("1"); return; }    if(A = =0) {puts ("0"); return;    } init (); ll ans=1LL;  for(inti =0; i < tot; i++) {ans= ans * (Binary_pow (FACTOR[I].FAC, factor[i].cnt, MoD) + 1LL)%MoD; } cout<< ans <<Endl;}intMain () {init_prime ();  while(Cin >> A >>B) {solve (); }    return 0;}

POJ 1845 Sumdiv (factor decomposition + fast Power + two-point summation)

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.