51nod1237 of greatest common divisor and V3

Source: Internet
Author: User
Tags gcd greatest common divisor

Test instructions:\ (\SUM_{I=1}^N\SUM_{J=1}^NGCD (i,j) \)
Puzzle: First enumerate gcd,\ (\sum_{d=1}^n\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor}\sum_{j=1}^{\lfloor \frac{n}{d} \rfloor}[(I,j) =1]\)
Considering that the second half is the most common Möbius inversion, there \ (\sum_{d=1}^n\sum_{x=1}^{\lfloor \frac{n}{d} \rfloor}\mu (x) *{\lfloor \frac{n}{d*x} \ rfloor}^2\)
Enumeration dx=t,\ (=sum_{t=1}^n{\lfloor \frac{n}{t} \rfloor}^2\sum_{d|n}d*\mu (\frac{t}{d}) \)
Since \ (id=i*\phi\), inversion after \ (\phi=id*\mu\),\ (\sum_{t=1}^n{\lfloor \frac{n}{t} \rfloor}^2*\phi (t) \)
And finally, the sub-block + du screening phi

#pragma gcc optimize (2)//#pragma gcc optimize (3)//#pragma gcc optimize (4)//#pragma gcc optimize ("unroll-loops")//# pragma comment (linker, "/stack:200000000")//#pragma gcc optimize ("ofast,no-stack-protector")//#pragma gcc target (" Sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ") #include <bits/stdc++.h> #define FI first#define SE Second#define DB Double#define mp make_pair#define pb push_back#define Pi ACOs ( -1.0) #define LL long Long#define VI vector& lt;int> #define MOD 1000000007#define ld long double#define C 0.5772156649#define ls l,m,rt<<1#define rs M+1,r,rt <<1|1#define PLL pair<ll,ll> #define PIL pair<int,ll> #define PLI pair<ll,int> #define PII pair <int,int>//#define CD complex<double> #define ULL unsigned long long#define base 1000000000000000000#define Max (a) (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) #define FIN freopen ("A.txt", "R", stdin) #define Fout freopen ("A.txt", "w", stdout) #define FIO Ios::sync_with_ Stdio (FALSE); Cin.tie (0) template<typename t>inline t const& MAX (T const &a,t const &AMP;B) {return a>b?a:b;} Template<typename t>inline T const& MIN (T const &a,t const &AMP;B) {return a<b?a:b;} inline void Add (ll &a,ll b) {a+=b;if (a>=mod) A-=mod;} inline void Sub (ll &a,ll b) {a-=b;if (a<0) A+=mod;} inline ll gcd (ll A,ll b) {return B?GCD (b,a%b): A;} inline ll QP (ll A,ll b) {ll ans=1;while (b) {if (b&1) ans=ans*a%mod;a=a*a%mod,b>>=1;} return ans;} inline ll QP (ll a,ll b,ll c) {ll ans=1;while (b) {if (b&1) ans=ans*a%c;a=a*a%c,b>>=1;} return ans;} Using namespace Std;const double eps=1e-8;const ll Inf=0x3f3f3f3f3f3f3f3f;const int n=5000000+10,maxn=3000000+10,inf=    0x3f3f3f3f;int prime[n],cnt;ll phi[n],inv2=qp (2,mod-2); bool Mark[n];map<ll,ll>phii;void init () {phi[1]=1;        for (int i=2;i<n;i++) {if (!mark[i]) prime[++cnt]=i,phi[i]=i-1;           for (int j=1;j<=cnt&&i*prime[j]<n;j++) {mark[i*prime[j]]=1; if (i%prime[j]==0) {phi[i*prime[j]]=phi[i]*prime[j];            Break        } phi[i*prime[j]]=phi[i]* (prime[j]-1);        }} for (int i=1;i<n;i++) {phi[i]%=mod;    Add (Phi[i],phi[i-1]);    }}ll Getphi (ll N) {if (n<n) return phi[n];    if (Phii.find (n)!=phii.end ()) return phii[n];    ll ans=n%mod* (n%mod+1)%mod*inv2%mod;        for (ll i=2,j;i<=n;i=j+1) {j=n/(n/i);    Sub (ans,1ll* (j-i+1)%mod*getphi (n/i)%mod); } return Phii[n]=ans;}    int main () {init ();    ll n,ans=0;scanf ("%lld", &n);        for (ll i=1,j;i<=n;i=j+1) {j=n/(n/i);        ll te= (n/i)%mod;te=te*te%mod;        ll P=getphi (j)-getphi (i-1);p = (p%mod+mod)%mod;    Add (Ans,te*p%mod);    } printf ("%lld\n", ans); return 0;} /****************************************/

51nod1237 Greatest common divisor and V3

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.