HDU5780 gcd Euler function

Source: Internet
Author: User
Tags greatest common divisor

http://acm.hdu.edu.cn/showproblem.php?pid=5780

BC #85 1005

Ideas:

First primitive Simplification:x^gCD(a,b)?? −1

That is, in N, (the number of conventions is the logarithm of i) *x^i-1, where I is 22 greatest common divisor in N. Then the problem can be transformed into pre-processing I, and then summing, note o (n*300) = 1, normal card constants. must also be optimized

Because Ans=∑s[D]∗(x^? D?? −1), s[d]= greatest common divisor is the logarithm of D

< Span class= "Mopen" > < Span class= "Mclose" > we notice s[d] or (the number of conventions is the logarithm of i), that is, the logarithm of Inma within n/i, apparently using Euler to do

< Span class= "Mopen" > < Span class= "Mclose" >s[d]=2* (phi[1]+phi[2]+...+phi[n/d])-1

< Span class= "Mopen" > < Span class= "Mclose" > Note: D is different, but n/d, that is, s[d] may have many of the same, such as 10/6 10/7 10/8 10/9 10/10, so for S[d] the same items, we can use equal formula summation (fast Power + inverse new knowledge)

So we just have to find each paragraph s[d] can be j=n/(n/i), J for the last of the same s[d] subscript

1 //#pragma COMMENT (linker, "/stack:102c000000,102c000000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <sstream>6#include <string>7#include <algorithm>8#include <list>9#include <map>Ten#include <vector> One#include <queue> A#include <stack> -#include <cmath> -#include <cstdlib> the //#include <conio.h> - using namespacestd; - #definePi ACOs (-1.0) - Const intN = 1e6+Ten; + Const intMOD = 1e9+7; - #defineINF 0x7fffffff +typedefLong LongLL; A  at voidFre () {freopen ("In.txt","R", stdin);} -InlineintRead () {intx=0, f=1;CharCh=getchar (); while(ch>'9'|| ch<'0') {if(ch=='-') f=-1; Ch=getchar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; Ch=getchar ();}returnx*F;} -  - ll Pow_m (ll x,ll N) - { -LL res=1; in      while(n>0) -     { to         if(N &1) +res= (res*x)%MOD; -x= (x*x)%MOD; theN >>=1; *     } $     returnRes;Panax Notoginseng } -  the intPrime[n],sphi[n]; + intInv[n]; A voidE_fun () { thesphi[1]=1; +      for(intI=2; i<=n;i++){ -         if(!Sphi[i]) { $prime[++prime[0]]=i; $sphi[i]=i-1; -         } -          for(intj=1; j<=prime[0]&&i*prime[j]<=n;j++){ the             if(I%prime[j]) sphi[i*prime[j]]=sphi[i]* (prime[j]-1); -             Elsesphi[i*prime[j]]=sphi[i]*Prime[j];Wuyi         } the     } -      for(intI=1; i<=n;i++) sphi[i]= (sphi[i-1]+sphi[i])%MOD; Wu      -     //make a table to find the inverse element About     //inv[1] = inv[0] = 1; $     //for (int i = 2;i < n;i++) -     //Inv[i] = inv[mod%i]* (mod-mod/i)%mod; - } -  A  + voidEX_GCD (LL a,ll b,ll &d,ll &x,ll &y) { the     if(!b) { -D =A; $x =1; they =0; the     } the     Else { theEX_GCD (b, a%B, D, y, x); -Y-= x* (A/b); in     } the     //return x; the } About  the ll Sn (ll q,ll N) { the     if(q==1)returnN; the LL x,y,d; +EX_GCD (q1, mod,d,x,y); -     return(Pow_m (Q,n)-1) * ((x+mod)%mod)%MOD; the }Bayi  the intMain () { the E_fun (); -     intT; -t=read (); the      while(t--){ the         intX,n; theX=read (), n=read (); theLL ans=0; -          for(intI=1, j;i<=n;i=j+1){ thej=n/(n/i); theLL sd=2*sphi[n/i]-1; theans= (ans + sd* (pow_m (x,i) *sn (x,j-i+1)%mod-(j-i+1))%mod)%MOD;94         } theprintf"%i64d\n", ans); the     } the     return 0;98}

HDU5780 gcd Euler function

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.