首页 > 其他分享 >Educational Codeforces Round 142 (Rated for Div. 2)

Educational Codeforces Round 142 (Rated for Div. 2)

时间:2023-01-26 14:33:43浏览次数:43  
标签:Educational Rated 142 int i64 因子 mp return primes

E. Divisors and Table

\(m=m_1\cdot m_2\)

找 \(m\) 的所有因子,记为数组 \(x\)。

对于 \(x_i\),找它的最大的小于等于 \(n\) 的因子 \(y\),那么 \(x_i\) 的贡献为 \(\frac{x_i}{y}\) (当 \(\frac{x_i}{y}>n\) 时,贡献为 \(0\))

先用板子求出 \(m\) 的所有质因子和所有因子。

如果 \(x_i\) 小于 \(n\),那他的贡献肯定是 \(1\)。

接下来就是如何用 \(m\) 的质因子来求 \(x_i\) 的最大因子。

记 \(f(x)\) 为 \(x\) 的最大的小于等于 \(n\) 的因子。

样例 \(1\) 当我们遍历到 \(3\) 的时候就可以更新所有 \(3\) 的倍数且是 \(m\) 的因子的数。

#include "bits/stdc++.h"

using namespace std;
using i64 = long long;

// constexpr int M = 998244353;
// constexpr int M = 1000000007;

using i128 = __int128;

i64 POW(i64 a, i64 b, i64 mod) {
  i64 res = 1;
  while (b) {
    if (b & 1) {
      res = (i128)res * a % mod;
    }
    b >>= 1;
    a = (i128)a * a % mod;
  }
  return res;
}

bool is_prime(i64 p) {
  if (p < 2) {
    return 0;
  }
  i64 d = p - 1, r = 0;
  while (!(d & 1)) {
    r++;
    d >>= 1;
  }
  int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
  for (int k = 0; k < 9; k++) {
    if (p == prime[k]) {
      return true;
    }
    i64 x = POW(prime[k], d, p);
    if (x == 1 || x == p - 1) {
      continue;
    }
    for (int i = 0; i < r - 1; i++) {
      x = (i128)x * x % p;
      if (x == p - 1) {
        break;
      }
    }
    if (x != p - 1) {
      return false;
    }
  }
  return true;
}

mt19937
    rng((unsigned int)chrono::steady_clock::now().time_since_epoch().count());

i64 pollard_rho(i64 x) {
  i64 s = 0, t = 0;
  i64 c = (i64)rng() % (x - 1) + 1;
  i64 val = 1;
  for (int goal = 1;; goal <<= 1, s = t, val = 1) {
    for (int step = 1; step <= goal; step++) {
      t = ((i128)t * t + c) % x;
      val = (i128)val * abs(t - s) % x;
      if (step % 127 == 0) {
        i64 g = gcd(val, x);
        if (g > 1) {
          return g;
        }
      }
    }
    i64 g = __gcd(val, x);
    if (g > 1) {
      return g;
    }
  }
}

unordered_map<i64, int> primes;

void get(i64 x) {
  if (x < 2) {
    return;
  }
  if (is_prime(x)) {
    primes[x]++;
    return;
  }
  i64 mx = pollard_rho(x);
  get(x / mx);
  get(mx);
}

void getprimes(i64 x) {
  primes.clear();
  get(x);
}

vector<i64> fac;

void getfac(i64 x) {
  fac.clear();
  getprimes(x);
  vector<pair<i64, int>> tmp(primes.begin(), primes.end());
  int SIZE = tmp.size();
  function<void(int, i64)> dfs = [&](int id, i64 x) {
    if (id == SIZE) {
      fac.push_back(x);
      return;
    }
    i64 p = 1;
    for (int i = 0; i <= tmp[id].second; i++) {
      if (i != 0) {
        p *= tmp[id].first;
      }
      dfs(id + 1, x * p);
    }
  };
  dfs(0, 1);
}

void solve() {
  int n, m1, m2;
  cin >> n >> m1 >> m2;
  i64 m = 1LL * m1 * m2;
  getfac(m);
  unordered_map<i64, int> mp(1024);
  mp.max_load_factor(0.25);
  for (auto x : fac) {
    if (x <= n) {
      mp[x] = x;
    }
    for (auto [y, useless] : primes) {
      i64 t = x * y;
      if (t > m) {
        continue;
      }
      if (m % t == 0) {
        mp[t] = max(mp[t], mp[x]);
      }
    }
  }
  int cnt = 0;
  int ans = 0;
  for (auto [x, y] : mp) {
    if (x / y <= n) {
      cnt++;
      ans ^= x / y;
    }
  }
  cout << cnt << ' ' << ans << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int tt = 1;
  cin >> tt;
  while (tt--) {
    solve();
  }
  return 0;
}

标签:Educational,Rated,142,int,i64,因子,mp,return,primes
From: https://www.cnblogs.com/kiddingma/p/17067817.html

相关文章

  • Educational Codeforces Round 142 (Rated for Div. 2) A-D
    比赛链接A题解知识点:贪心。代码#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;boolsolve(){intn;cin>>n;intcnt=0;......
  • CF1792 D. Fixed Prefix Permutations : Educational Codeforces Round 142 (Rated fo
    给出n个长度为m的排列(a1,a2,a3,...,an)定义一个操作 r=ai•aj:r[k]=a[j][a[i][k]]定义一个排列(p1,p2,...,pn)的beauty为最大的k,使得p[1]=1,p[2]=2,..,p[k......
  • CF Educational Round 142 (Rated for div2) 题解
    A注意到除了血量为\(1\)的怪物,其余的怪物直接斩杀是更合理的。所以只要统计血量为\(1\)的怪物的个数即可。#include<cstdio>voidsolve(){ intn;scanf("%d",......
  • codeforce edu round 142 D. Fixed Prefix Permutations
    题目链接:https://codeforces.com/contest/1792/problem/D题意是给出n个长度为m的排列,定义排列p*q为\(r_j=q_{p_j}\),令一个排列的价值p为最大的k使得\(p_1=1,p_2=2......
  • Educational Codeforces Round 1 个人总结A-E
    EducationalCodeforcesRound1A.TrickySum数学,求\(1\dotsn\)的和减去小于等于n的二次幂乘2之和LLf[40];voidsolve(){ LLn; cin>>n; LLans=n+n*(n-1)/2;......
  • cratedb 支持游标了
    好久没太关注cratedb了,就在最近看了下发现支持游标了,还是很强大的,值得体验试用下,以前我在尝试集成cratedb与hasura的时候发现了一些问题,从目前的一些特殊,似乎是可以尝试......
  • 「解题报告」ARC142D Deterministic Placing
    好?首先我们可以发现,第一步和第三步的局面必须相等,因为第二步可以反着走回第一步,如果不相等那么下一步走的方案就不唯一了。然后我们考虑走的形式,由于是一棵树,没有环,我们......
  • Educational Codeforces Round 14
    EducationalCodeforcesRound14AFashioninBerland做法:模拟代码:voidsolve(){intn;cin>>n;intans=0;for(inti=1;i<=n;i++){......
  • LeetCode.142 环形链表II
    1.题目:给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。如果链表中有某个节点,可以通过连续跟踪next指针再次到达,则链表中存在......
  • Educational Codeforces Round 17
    EducationalCodeforcesRound17https://codeforces.com/contest/762A.k-thdivisor#include<bits/stdc++.h>#definelllonglongusingnamespacestd;lln,k;......