ACM-ICPC 2018 徐州赛区网络预赛 D. EasyMath

ACM-ICPC 2018 徐州赛区网络预赛 D. EasyMath



做法:

\[f(m,n) = \sum _{i=1}^{m} \mu(in) = \sum_{i=1}^{m}[gcd(i,n)=1]\mu(i)\mu(n) = \mu(n)\sum_{d|n}\mu(d)f(\frac{m}{d},d)\]

边界: n=1,杜教筛求\(\sum_{i=1}^{m}\mu(i)\),m = 1, 返回\(\mu(n)\),预处理尽可能把空间卡满。

2个小时的时候就推出来了这个式子,不会算复杂度,本校没人过。。。于是成功放弃了。。。

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
#define mp make_pair
#define PII pair<int,int>
#define sc second
typedef long long ll;
const int N = 1e7 + 13000000 + 1;
const int LM = 13000000;
using namespace std;
ll n,m;
bool notp[N];
int p[N], smiu[N];
short miu[N];
void init() {
    notp[1] = 1;
    miu[1] = 1;
    for(int i=2;i<=1e7+LM;++i) {
        if(!notp[i]) p[++p[0]] = i, miu[i] = -1;
        for(int j=1;j<=p[0]&&p[j]*i<=1e7+LM;++j) {
            notp[i*p[j]] = 1;
            if(i%p[j] == 0) {
                miu[i*p[j]] = 0;
                break;
            }
            miu[i*p[j]] = miu[i]*miu[p[j]];
        }
    }
    for(int i=1;i<=1e7+LM;++i) smiu[i] = smiu[i-1] + miu[i];
}
ll g(ll n) {
    if(n<=1e7+LM) return smiu[n];
    if(n == 1) return 1;
    ll ans = 1;
    for(ll i=2,r;i<=n;i=r+1) {
        r = (n/(n/i));
        ans -= (r-i+1LL)*g(n/i);
    }
    return ans;
}
void chai(ll x,vector<ll> &v,ll &mu) {
    v.clear();
    mu = 1;
    for(int i=1;i<=p[0]&&1LL*p[i]*p[i]<=x;++i) {
        if(x%p[i]==0) {
            int cnt = 0;
            v.pb(p[i]);
            mu = -mu;
            while(x%p[i]==0) x/=p[i], cnt++;
            if(cnt>1) mu = 0;
        }
    }
    if(x!=1) mu=-mu, v.pb(x);
}
ll f(ll m,ll n) {
    if(m == 0 || n == 0) return 0;
    ll mu_n, ans = 0;
    if(m == 1 && n <= 1e7+LM) return miu[n];
    vector<ll> v;
    chai(n,v,mu_n);
    if(m == 1) return mu_n;
    if(n == 1) return g(m);
    if(mu_n == 0) return 0;
    int cnt = v.size();
    for(int s=0;s<(1<<cnt);++s) {
        ll d = 1, mu_d = 1;
        for(int i=0;i<cnt;++i) if(s&(1<<i)){
            d = d*v[i];
            mu_d = -mu_d;
        }
        if(m >= d) ans += mu_d*f(m/d,d);
    }
    ans *= mu_n;
    return ans;
}

int main() {
    init();
    scanf("%lld%lld",&m,&n);
    printf("%lld\n",f(m,n));
    return 0;
}

原文地址:https://www.cnblogs.com/RRRR-wys/p/9615109.html

时间: 2024-11-05 11:50:46

ACM-ICPC 2018 徐州赛区网络预赛 D. EasyMath的相关文章

ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn&#39;t want to study

262144K Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. That means, if he reads books from ll to rr, he wi

ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn&#39;t want to study (线段树)

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. That means, if he reads books from ll to rr, he will get a

ACM-ICPC 2018 徐州赛区网络预赛 B. BE, GE or NE

In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl named "Sena" are playing a video game. The game system of this video game is quite unique: in the process of playing this game, you need to constantly fac

ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer (最大生成树+LCA求节点距离)

ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer J. Maze Designer After the long vacation, the maze designer master has to do his job. A tour company gives him a map which is a rectangle. The map consists of N \times MN×M little squares. That is to say, the h

ICPC 2018 徐州赛区网络赛

ACM-ICPC 2018 徐州赛区网络赛 ?去年博客记录过这场比赛经历:该死的水题 ?一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进. ? ? D. Easy Math 题意: ? 给定 \(n\), \(m\) ,求 \(\sum _{i=1}^{m} \mu(in)\) .其中 $ 1 \le n \le 1e12$ , $ 1 \le m \le 2e9$ ,\(\mu(n)\) 为莫比乌斯函数. ? 思路: ? 容易知道,\(i\) 与 \(n\) 不互质时, \(\m

ACM-ICPC 2018 徐州赛区网络预赛

Rank Solved A B C D E F G H I J K 157/1526 7/11 O O ? ? . O O O O O ? O: 当场通过 ?: 赛后通过 .: 尚未通过 A Hard to prepare solved by chelly chelly's solution B BE, GE or NE solved by ch ch's solution C Cacti Lottery upsolved by chelly chelly's solution 考虑枚举*代表的

ACM-ICPC 2018 徐州赛区网络预赛 A. Hard to prepare

传送门:https://nanti.jisuanke.com/t/31453 本题是一个组合数学(DP,滑稽)题. 一个环上有N个位置,标号为1~N.设第i(1≤i≤N)个位置上的数为x[i],限制条件为:0≤x[i]<2k.另有限制条件:当位置i和位置j相邻时,x[i]⊕x[j]≠2k-1.求满足限制条件的环的状态数. 可以考虑将环切割成链,以分析问题.设: ①a[n]:长度为n,且首尾相同的满足上述条件的链的状态数: ②b[n]:长度为n,且首尾相反的满足上述条件的链的状态数: ③c[n]:

【ACM-ICPC 2018 徐州赛区网络预赛】E. End Fantasy VIX 血辣 (矩阵运算的推广)

Morgana is playing a game called End Fantasy VIX. In this game, characters have nn skills, every skill has its damage. And using skill has special condition. Briefly speaking, if this time you use skill "x", then next time you can use skill &quo

ACM-ICPC 2018 徐州赛区网络预赛(9.9)

#include<bits/stdc++.h> #define int long long using namespace std; const int maxn=1e6+10; const int mod=1e9+7; int dp[maxn][3]; int quick(int a,int n) { int ans=1; int x=a; while(n!=0) { if(n%2==1) {ans=ans%mod*x%mod; n--; } else { x=x%mod*x%mod;n=n