2021 ICPC North American Qualifier C. Common Factors(数论 + 找规律)

昨天这道题属实有点唬人

没能打成ICPC就场外观战,今年一众oier学弟真是让我开了眼,换到今年我估计无了,不过有luka,感觉我们组队的话可以9题左右

首先是问F(i) = 2-i的非互质数量,问给出一个n,找出一个k属于n,范围1e18,问max(f(k) / k)

然后经过群友的提醒打表之后发现这个分子的数目是确定的

为Q = sigma(prime[i]), 每段的数量为 Q * prime[i + 1] - Q, 分子的数量是和Q不互质的数量,那么就是phi(Q), 

所以答案为Q - phi(Q) / Q

刚开始以为这个phi开不下就没当回事儿,结果发现可以23333

然后特别注意一下又是int128的问题,出题人大概率拿py写的标程,然后cpp炸精度了,真尼玛坑爹

#include <bits/stdc++.h>

using namespace std;
#define limit (1000000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;

inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {
        if (s == '-')sign = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9') {
        x = (x << 3) + (x << 1) + s - '0';
        s = getchar();
    }
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if (x / 10) print(x / 10);
    *O++ = x % 10 + '0';
}

void write(ll x, char c = 't') {
    if (x < 0)putchar('-'), x = -x;
    print(x);
    if (!isalpha(c))*O++ = c;
    fwrite(obuf, O - obuf, 1, stdout);
    O = obuf;
}


const ll mod = 1e9 + 7;

ll quickPow(ll base, ll expo) {
    ll ans = 1;
    while (expo) {
        if (expo & 1)(ans *= base) %= mod;
        expo >>= 1;
        base = base * base;
        base %= mod;
    }
    return ans % mod;
}

ll C(ll n, ll m) {
    if (n < m)return 0;
    ll x = 1, y = 1;
    if (m > n - m)m = n - m;
    rep(i, 0, m - 1) {
        x = x * (n - i) % mod;
        y = y * (i + 1) % mod;
    }
    return x * quickPow(y, mod - 2) % mod;
}

ll lucas(ll n, ll m) {
    return !m || n == m ? 1 : C(n % mod, m % mod) * lucas(n / mod, m / mod) % mod;
}
ll fact[limit];
void calc(){
    fact[0] = 1;
    rep(i,1,1e3)fact[i] = (fact[i - 1] * i) % mod;
}
ll A(ll x, ll y){
    return (C(x,y) * fact[y]) % mod;
}
ll euler(ll x)
{
    int rs=x,a=x;
    for(int i=2; i*i<=a; i++)
        if(a%i==0) // 最开始先 a%i == 0 进去马上执行 rs 操作,是因为确保了 i 肯定是素数
        {
            rs=rs/i*(i-1); // 先进行除法是为了防止中间数据的溢出
            while(a%i==0) a/=i; // 确保下一个 i 是素数
        }
    if(a>1) rs=rs/a*(a-1);

    return rs;
}
int kase;
ll n, m, k;
int prime[limit],tot,num[limit],phi[limit],miu[limit];
void get_prime(const int &n = 1e6){
    memset(num,1,sizeof(num));
    num[1] = num[0] = 0;
    miu[1] = 1;
    rep(i,2,n){
        if(num[i])prime[++tot] = i,miu[i] = -1,phi[i] = i - 1;
        for(int j = 1; j <= tot && prime[j] * i <= n ; ++j){
            num[prime[j] * i] = 0;
            if(i % prime[j] == 0){
                miu[i * prime[j]] = 0;
                break;
            }else{
                miu[i * prime[j]] = -miu[i];//莫比乌斯函数
            }
        }
    }
}//素数筛
int a[limit];

void solve() {
    cin>>n;
    get_prime();
    ll product = 1;
    ll assigned = 1;
    ll p = -1, q= -1;
    rep(i,1,1e18){
        product *= prime[i];
        ll next = prime[i + 1] * product;
        ll gap = next - product;
        if(n - assigned <= gap ){//找到了
            q = product;
            p = 0;
            p = (q - euler(q));
            break;
        }
        assigned += gap;
    }
    ll gc = gcd(p, q);
    p /= gc;
    q /= gc;
    cout<<p<<"/"<<q<<endl;

}

int32_t main() {
#ifdef LOCAL
    FOPEN;
//    FOUT;
#endif
    FASTIO
//    cin >> kase;
//    while (kase--)
        solve();
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
    return 0;
}

 

posted @ 2022-01-24 15:47  tiany7  阅读(64)  评论(0编辑  收藏  举报