luogu1587 [NOI2016] cycle of the United States

Topic links: luogu1587

The first is the title of "pure decimals" feels very odd clear

For a fraction \ (\ FRAC {X} {Y} \) , to satisfy the condition first of all that the \ (GCD (X, Y) =. 1 \) , and secondly because the pure cycle there must be a length of a loop section \ (L \ ) such that the decimal point to the right \ (L \) the same two numbers after the decimal part of bits is written like this
\ [\ frac {x} { y} - \ lfloor \ frac {x} {y} \ rfloor = \ frac {xk ^ l} { y} - \ lfloor \ frac {xk ^ l} {y} \ rfloor \]

Sides to the denominator available
\ [xy \ lfloor \ frac {
x} {y} \ rfloor = xk ^ ly \ lfloor \ frac {xk ^ l} {y} \ rfloor \] transposing
\ [xk ^ lx = y ( \ lfloor \ frac {xk ^ l
} {y} \ rfloor- \ lfloor \ frac {x} {y} \ rfloor) \] so that the final \ (XK ^ L \ equiv X (MOD \ Y) \) , since the \ (GCD (X, Y) =. 1 \) , so that both sides while divided by \ (X \) to give \ (K ^ L \ equiv. 1 (MOD \ Y) \) , it is clear that there are \ (gcd (k, y ) = 1 \)
so that the last subject seeking is
\ [\ sum_ {i = 1 } ^ n \ sum_ {j = 1} ^ m [gcd (i, j) = 1] [gcd (j, k) = 1 ] \]
If both are simultaneous inversion, then obviously difficult to handle, so consider first a
\ [\ Begin {aligned} & \ sum_ {i = 1} ^ n \ sum_ {j = 1} ^ m [gcd (i, j) = 1] [gcd (j, k) = 1] \\ = & \ sum_ {i = 1} ^ n \ sum_ {j = 1} ^ m [gcd (i, j) = 1] \ sum_ {d | gcd (j, k)} \ mu (d) \\ = & \ sum_ {d | k} \ mu (d) \ sum_ {i = 1} ^ n \ sum_ {j = 1} ^ {\ lfloor \ frac {m} {d} \ rfloor} [gcd (i, jd) = 1] \\ = & \ sum_ { d | k} \ mu (d) \ sum_ {i = 1} ^ n \ sum_ {j = 1} ^ {\ lfloor \ frac {m} {d} \ rfloor} [ gcd (i, j) = 1
] [gcd (i, d) = 1] \ end {aligned} \] this is what is it? Noting the form of equation two longitudinal substantially uniform, may then be referred to \ (f (n, m, k) = \ sum_ {i = 1} ^ n \ sum_ {j = 1} ^ m [gcd (i, j) =. 1] [GCD (J, K) =. 1] \) , then \ (f (n, m, k) = \ sum_ {d | k} \ mu (d) f (\ lfloor \ frac {m} { d} \ rfloor, n, d
) \) boundary conditions, if the first \ (f (0, m,
k) = f (n, 0, k) = 0 \) Next \ (f (n, m, 1) = \ sum_. 1} ^ {n-I = \ sum_. 1} = {J ^ m [GCD (I, J) =. 1] \) , this just inversion will have a look \ (\ sum_ {d = 1 } ^ n \ MU (I) \ lfloor \ FRAC {n-} {I} \ rfloor \ lfloor \ FRAC {m} {I} \ rfloor \) , the direct number theory block, but be careful \ (n \ leq 10 ^ 9 \), So consider Du teach sieve seeking \ (\ mu \) and
a little trick is that we will only go computing \ (\ neq 0 \ \ mu (d)) value, the other directly to pruning

#include<iostream>
#include<string>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define fir first
#define sec second
#define maxd 1000000007
#define eps 1e-8
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
const int N=10000000;
struct node{
    int n,m,k;
};
bool operator <(node p,node q)
{
    if (p.n!=q.n) return p.n<q.n;
    else if (p.m!=q.m) return p.m<q.m;
    else return p.k<q.k;
}
map<node,ll> ans;
map<int,ll> sumu;
int n,m,k,tot=0,mu[N+10],pri[1001000],len;
bool nopri[N+10];
ll sum[N+10];
vector<int> fac;

int read()
{
    int x=0,f=1;char ch=getchar();
    while ((ch<'0') && (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
    while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
    return x*f;
}

void sieve()
{
    mu[1]=1;
    rep(i,2,N)
    {
        if (!nopri[i]) {pri[++tot]=i;mu[i]=-1;}
        int j;
        for (j=1;j<=tot && i*pri[j]<=N;j++)
        {
            nopri[i*pri[j]]=1;
            if (i%pri[j]==0) break;
            else mu[i*pri[j]]-=mu[i];
        }
    }
    rep(i,1,N) sum[i]=sum[i-1]+mu[i];
}

ll query(int x)
{
    if (x<=N) return sum[x];
    if (sumu[x]) return sumu[x];
    ll ans=1;int l,r;
    for (l=2;l<=x;l=r+1)
    {
        r=x/(x/l);
        ans-=1ll*(r-l+1)*query(x/l);
    }
    sumu[x]=ans;
    return ans;
}

ll calc(int n,int m)
{
    if (n>m) swap(n,m);
    //cout << n << " " << m << " ";
    int l,r;ll ans=0;
    for (l=1;l<=n;l=r+1)
    {
        r=min(n/(n/l),m/(m/l));
        ans+=1ll*(n/l)*(m/l)*(query(r)-query(l-1));
    }
    //cout << ans << endl;
    return ans;
}

ll solve(int n,int m,int k)
{
    if ((!n) || (!m)) return 0;
    node now=(node){n,m,k};
    if (ans[now]) return ans[now];
    if (k==1) ans[now]=calc(n,m);
    else
    {
        int i;
        for (i=0;i<len && fac[i]<=k;i++)
        {
            if ((k%fac[i]==0) && (mu[fac[i]]))
                ans[now]+=solve(m/fac[i],n,fac[i])*mu[fac[i]];
        }
    }
    return ans[now];
}

int main()
{
    n=read();m=read();k=read();
    sieve();
    rep(i,1,k)
        if (k%i==0) fac.pb(i);len=fac.size();
    printf("%lld",solve(n,m,k));
    return 0;
}

Guess you like

Origin www.cnblogs.com/encodetalker/p/11129977.html