Uoj #221 The beauty of "NOI2016" cycle

Source: Internet
Author: User
Tags gcd

Topic Link: The Beauty of circulation

This question feels very beautiful ... It's not easy to have such a beautiful face and a higher level of thinking ...

For the sake of convenience, let me first speak about two symbols. $[a]$ indicates that if $a$ is true, return $1$, otherwise return $0$; $a \perp b$ represents $a$ and $b$ coprime.

First, we need to consider what conditions a score needs to be met to become pure repeating decimal.

Let's think about how we use division to determine whether a fractional $\frac{x}{y}$ is pure repeating decimal. Obviously we are all the way down, when there is the same remainder, then this number is a cyclic decimal. If the remainder of the first repetition is $x$, then this number is the pure loop decimal. This approach is actually the existence of a number $l$, which makes: $ $xk ^l \equiv x (\bmod y) $$

And because the title demand value is not repeated, then we can get $x \perp y$. So we can launch $k^l \equiv 1 (\bmod y) $. So we just $k \perp y$.

So we're actually asking for this: \begin{aligned}&\sum_{x=1}^{n} \sum_{y=1}^{m}[x\perp y][y \perp k]\\=&\sum_{y=1}^{m}[y\ Perp k]\sum_{x=1}^{n}[x\perp y]\end{aligned}

With a Möbius inversion, you can get: \begin{aligned}&\sum_{y=1}^{m}[y\perp K] \sum_{x=1}^{n}\sum_{d|x,d|y}\mu (d) \\=&\sum_{y=1} ^{m}[y\perp K]\sum_{d|y}^{n}\mu (d) \lfloor \frac{n}{d}\rfloor \end{aligned}

By changing the enumeration order, you can get: \begin{aligned}&\sum_{d=1}^{\min (n,m)}\mu (d) \lfloor \frac{n}{d} \rfloor \sum_{y=1}^{m}[d\ |\ y][y \perp k]\\=&\sum_{d=1}^{\min (n,m)}\mu (d) \lfloor \frac{n}{d}\rfloor \sum_{i=1}^{\lfloor \frac{m}{d} \rfloor}[id\ Perp k]\\=& \sum_{d=1}^{\min (n,m)}[d\perp K]\mu (d) \lfloor \frac{n}{d}\rfloor \sum_{i=1}^{\lfloor \frac{m}{d} \ Rfloor}[i\perp k]\end{aligned}

The last step used a property of coprime, that is $[ab\perp c]=[a\perp c][b\perp c]$. This should be well understood ...

We do not set $f (n,k) =\sum_{i=1}^n[i\perp k]$, then we just in $o (1) $ time to find out $f$, the complexity is $o (n) $, you can get $84$ points. In fact, we can get a formula: $ $f (n,k) =\lfloor \frac{n}{k} \rfloor F (k,k) +f (n\bmod k,k) $$

This should not be difficult to understand, because $[a\perp B]=[a\bmod b \perp b]$

So we can preprocess the value of $n\leqslant k$ for $f (n,k) $, each time the evaluation becomes $O (1) $. So the $84$.

Let's consider how to optimize it next. Since $\lfloor \frac{m}{x} \rfloor$ only $\sqrt{m}$, $\lfloor \frac{n}{x} \rfloor$ only $\sqrt{n}$ species, we can clearly fragment sum. Then, we need to quickly find out the value of $\sum_{i=1}^n[i\perp k]\mu (i) $.

$g (n,k) =\sum_{i=1}^n[i\perp k]\mu (i) $, let's consider how quickly this function can be obtained. We first consider a $k$ factor $p$, then $k$ obviously can be written in $p^cq$ form. Since only the $k$ coprime is the valid value in the range of $[1,n]$, if $x\perp k$, we can get $x\perp p$ and $x\perp q$. Therefore, we can consider subtracting the $x$ from the $p$ coprime from the $x\perp q$, and we can get the $x\perp k$ part. If you do not understand here, you can draw a $x\perp q$, $x \perp p$, $x \perp k$ the diagram to understand.

Since all the numbers with $q$ coprime must be written in the form of $p^xy (Y\perp Q) $. Then we need to subtract the number that must satisfy $x>0$. Also because when $x>1$ $\mu (p^xy) =0$, so we only need to consider $x=1$ situation can. In this case, the number we need to consider is the form of $py (Y\perp Q) $, so we can get the following formula: \begin{aligned} g (n,k) &=\sum_{i=1}^n[i\perp q]\mu (i)-\sum_{y= 1}^{\lfloor\frac{n}{p}\rfloor}[py\perp q]\mu (PY) \\&=g (n,q)-\sum_{y=1}^{\lfloor\frac{n}{p}\rfloor}[y\perp q]\ Mu (py) \end{aligned}

The last step above is due to the $p\perp q$, so $py\perp q$ only need to guarantee $y\perp q$.

Let's take a look at the latter equation. Obviously when $p\perp y$ $\mu (py) =\mu (P) \mu (y) $, otherwise $\mu (py) =0$. So we got: \begin{aligned} g (n,k) &=g (n,q)-\sum_{y=1}^{\lfloor\frac{n}{p}\rfloor}[y\perp p][y\perp Q]\mu (p) \mu ( Y) \\&=g (n,q)-\mu (p) \sum_{y=1}^{\lfloor\frac{n}{p}\rfloor}[y\perp k]\mu (y) \\&=g (n,q) +g (\lfloor\frac{n}{p }\RFLOOR,K) \end{aligned}

So we can solve it recursively. It is easy to find the boundary condition is $n=0$ or $k=1$. $n =0$ when the direct return to $0$, $k =1$ is the Möbius function of the prefix and, with the Duchi sieve to find out. Because each recursion or $n$ will become $\lfloor \frac{n}{p} \rfloor$, there is $o (\sqrt{n}) $ seed value, or $p$ a qualitative factor, there is $\omega (k) $ seed value, so there is a total of $o (\omega (k) \ Sqrt{n}) $ type value, memory search can be. where $\omega (k) $ represents the number of different qualitative factors for $k$. So the final total complexity is $o (\omega (k) \sqrt{n}+n^{\frac{2}{3}}) $, which can be passed by this question.

Two details:

Can be in the recursive $g$ when not directly remember $k$, but record $k$ still how many factorization, convenient storage;

Memory can open hash list storage, with $map$ can also, or simply sub-$n,m$ and less than, Naghugen number of cases can be stored.

The first time to write this problem, it seems to write too detailed ... Please don't spray the gods ...

Paste the following code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #define FILE (s) freopen (S ".", "R", stdin), Freopen (S ". Out", "w", stdout) #define MAXN 5000010#define MoD 100007 #define MAXK 2010using namespace Std;typedef long long llg;int n,m,k,pr_k[maxk],lp;int s[maxn],ls,f[maxk];int Head[mod], Next[maxn],to[maxn],tt;int H1[11][mod],n1[maxn],t1[maxn],t2;bool Vis[maxn],huzhi[maxk];llg ANS,ZTO[MAXN],ZT1[MAXN ],mu[maxn];int gcd (int a,int b) {int r=a%b;while (r) A=b,b=r,r=a%b;return b;} void Pre () {mu[1]=1;for (int i=2;i<maxn;i++) {if (!vis[i]) s[++ls]=i,mu[i]=-1;for (int j=1;j<=ls && s[j]*i <maxn;j++) {vis[s[j]*i]=1;if (i%s[j]) Mu[i*s[j]]=-mu[i];else{mu[i*s[j]]=0;break;}}} for (int i=2;i<maxn;i++) mu[i]+=mu[i-1];for (int i=1;i<=k;i++) {huzhi[i]= (gcd (i,k) ==1); f[i]=f[i-1]+huzhi[i];}} LLG Suan (int x) {return (x/k) *f[k]+f[x%k];} LLG solveu (int x) {if (X&LT;MAXN) return mu[x];for (int i=head[x%mod];i;i=next[i]) if (to[i]==x) return Zto[i];int now=++tt; To[tt]=x;next[tt]=head[x%mod];head[x%mod]=tt; zto[now]=1;for (int i=2,nt=0;nt<x;i=nt+1) nt=x/(x/i), zto[now]-= (nt-i+1) *solveu (x/i); return zto[now];} LLG g (int x,int y) {if (!x) return Solveu (y), if (y<=1) return y;for (int i=h1[x][y%mod];i;i=n1[i]) if (t1[i]==y) return zt1 [I];int now=++t2; T1[t2]=y;n1[t2]=h1[x][y%mod];h1[x][y%mod]=t2;return zt1[now]=g (x-1,y) +g (X,y/pr_k[x]);} int main () {File ("a"), scanf ("%d%d%d", &n,&m,&k);p Re (); for (int i=1;s[i]<=k;i++) if (k%s[i]==0) pr_k[++ lp]=s[i];for (int d=1,l=min (n,m), nt;d<=l;d=nt+1) {nt=min (n/(n/d), m/(m/d)), ans+= (g (lp,nt)-G (lp,d-1)) * (LLG) (N/D) *suan (M/D);} printf ("%lld", ans); return 0;}

Uoj #221 The beauty of "NOI2016" loops

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.