Poj1845:sumdiv (factor and + inverse + mass factor decomposition) good problem

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=1845

Definition: The K value satisfying a*k≡1 (mod p) is a multiplicative inverse of p. Why do we have to multiply the inverse element? When we ask for (A/b) mod P's value, and a is large, and cannot directly obtain a A/b value, we will use the multiplication inverse. We can use the B to multiply the inverse k of p, multiply a by the K-mode p, ie (a*k) mod p. The result is equivalent to (A/b) mod p. Problem analysis: Let a^b factor and MODK, because it is large number can not directly seek, because the factor and function is multiplicative function, so first of all the quality factor decomposition, intoN=p1^a1*p2^a2*p3^a3****ps^as, then

S (n) =[(p1^a1+1-1)/(P1-1)]*[(p2^a2+1-1)/(p2-1)]*[(p3^a3+1-1)/(p3-1)]***[(ps^as+1-1)/(Ps-1)]; (Factor and)

And because S (n)%mod equals each part of the modulo, so can be gradually solved, such as (p1^a1+1-1)/(p1-1)%mod, here to use the division to take the model, so to use the concept of multiplicative inverse,

i.e. ( A/b)%p= (a *b^ ( -1)%p) , and because (a^b)% P = ((a% p) ^b)% P ,

So (p1^a1+1-1)/(P1-1)%mod== (((p1%mod) ^a1+1-1)%mod* (p1-1) ^-1)%mod;

Of course there is the premise of inverse element is gcd (a,p) ==1;

#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>#defineN 500010#defineMoD 9901typedef __int64 LL;using namespacestd;ll a,b,x,y;ll ans[n],num[n],top;ll Pow (ll x,ll k) {ll T=1;  while(k) {if(k&1) t= ((t%mod) * (x%mod))%MoD; K>>=1; X= ((x%mod) * (x%mod))%MoD; }    returnt;}voidExtend (__int64 a,__int64 b,__int64 &x1, __int64 &y1) {    if(b==0) {x1=1; Y1=0; return ; } Extend (B,a%b,x1,y1); ll T=X1; X1=Y1; Y1=t-(A/b) *Y1; return ;}voidsolve () {ll sum=1, a,xx;  for(intI=0; i<top; i++)    {        if(ans[i]%mod==0)Continue;//Key two judgments, related to seeking inverse element. If ans[i]%mod=0, then there is a hierarchical formula can be seen, the original is less than 0, so can only use the original, the result is 1if(ans[i]%mod==1)//i.e. mod|                                  (ans[i]-1), because ans[i]>=2, so ans[i] can not be equal to 1, this is gcd (ans[i]-1,mod) ==mod, there is no inverse, can not be used to expand Euclid to seek the inverse of the yuan {            At this time for (1+ans[i]^1+ans[i]^2+.....+ans[i]^num[i])%mod= (num[i]+1)%mod; Sum= (sum* (num[i]+1))%MoD; Continue; } A=pow (ans[i],num[i]+1); A= (A-1)%MoD; Extend (Ans[i]-1, mod,x,y);//because Ans[i] is a prime number, ans[i]-1 is even, so ans[i]-1 and 9901 coprimexx= (x%mod+mod)%MoD; A= ((a%mod) * (xx%mod))%MoD; Sum= (sum*a)%MoD; } printf ("%i64d\n", sum);}intMain () { while(SCANF ("%i64d%i64d", &a,&b)! =EOF) {        if(a==0) {printf ("0\n"); Continue; }        Else if(a==1|| b==0) {printf ("1\n"); Continue; } ll T=A; Top=0; memset (num,0,sizeof(num));  for(intI=2; i*i<=a; i++)        {            if(t%i==0) {Num[top]++; Ans[top]=i; T/=i;  while(t%i==0) {Num[top]++; T/=i; } Top++; }        }        if(t>1) {Num[top]++; Ans[top++]=T; }         for(intI=0; i<top; i++) {Num[i]*=b;    } solve (); }    return 0;}

Poj1845:sumdiv (factor and + inverse + mass factor decomposition) good question

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.