This question is at first glance quite water, direct $ Ploya $ on it, but look at the range of data:
$ N \ good 1e9 $
1e9 species that have replaced, this prodigiously than the.
So consider optimizing formulas.
First proof, each transfer cycle junction size is replaced with the i-th $ gcd (i, n) $
prove:
First, the x-th element set position p, is the substitution type i, the k-th cycle back to the origin, k is the number of cycles junction.
$ I + p \ equiv p (mod n) $
$ I \ equiv 0 (mod n) $
$ N | i $
$ I | $ ik
We want the smallest k, then:
$ I = lcm (i, n) $
$ I = \ frac {a} {gcd (i, n)} $
$ k= \frac{n}{gcd(i,n)} $
Each end of the cycle are the same size, so the loop number of nodes is:
$ num= \frac{n}{\frac{n}{gcd(i,n)}} =gcd(i,n) $
QED.
Subsequently pushing polya the formula:
[S] is a unit function, s the establishment returns 1, otherwise it returns 0.
$ ans=\frac{1}{n} \sum \limits_{i=1}^n n^{gcd(i,n)} $
$ =\sum \limits_{i=1}^n n^{gcd(i,n)-1} $
$ =\sum \limits_{i=1}^n \sum \limits_{d|n} n^{d-1} $
$ =\sum \limits_{d|n} n^{d-1} \sum \limits_{i=1}^{n}[gcd(i,n)==d)] $
$ =\sum \limits_{d|n} n^{d-1} \sum \limits_{i=1}^{\frac{n}{d}}[gcd(i,\frac{i,n})==d] $
$ =\sum \limits_{d|n} n^{d-1} \phi{frac{n}{d}} $
Factorising dfs can then traverse all the factors, the way to determine the Euler function.
问题解决。