首页 > 其他分享 >CF1750D Count GCD

CF1750D Count GCD

时间:2023-05-16 11:35:36浏览次数:36  
标签:Count le GCD int dfrac 容斥 long CF1750D include

Problem

多组数据。

每组数据给定两个整数 \(n\),\(m\) 和一个数列 \(b\),问有多少种方案构造一个长度为 \(n\) 的序列 \(a\),满足 \(1 \le a_i \le m\) 求 \(gcd(a_1,a_2,\cdots,a_i) = b_i\),答案对 \(998244353\) 取模。

Input

第一行输入 \(t\),表示数据组数。

每组数据第一行是两个整数 \(n\),\(m\)。

每组数据第二行输入 \(n\) 个整数 \(b_1,b_2,\cdots,b_n\)

Output

输出 \(t\) 行,每行输出一个整数表示答案。

Sample

Input 1

5
3 5
4 2 1
2 1
1 1
5 50
2 3 5 2 3
4 1000000000
60 30 1 1
2 1000000000
1000000000 2

Output 1

3
1
0
595458194
200000000

Solution

首先对于 \(\forall 2 \le i \le n\),如果 \(b_{i-1}\) 不是 \(b_i\) 的倍数,显然无解。

因为 \(b_i=gcd(b_{i-1},a_i)\),所以 \(gcd(\dfrac{b_{i-1}}{b_i},\dfrac{a_i}{b_i})=1\)。

而 \(a_i\) 的取值在 \([1,m]\) 之间,那我们就是需要在 \([1,\dfrac{m}{b_i}]\) 中统计出与 \(\dfrac{b_{i-1}}{b_i}\) 互质的数的个数。

考虑容斥,我们令 \(g\) 来表示 \(\dfrac{b_{i-1}}{b_i}\),每次暴力求出 \(g\) 的质因子,然后枚举选取质因子的状态,统计答案即可。

由于 \(b_{i-1}\) 是 \(b_i\) 的倍数,\(1 \le b_i \le m \le 10^9\),那么如果 \(b_{i-1} \neq b_i\),\(b_{i-1}\) 至少是 \(b_i\) 的 \(2\) 倍,所以说 \(b_{i-1} \neq b_i\) 的情况最多只有 \(log\) 次,容斥的次数大幅减少。

而对于 \(b_{i-1}=b_i\) 的情况,\(有g=1\),没有质因子,自然就不会容斥来统计答案。

代码:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int kmax = 2e5 + 3;
const int Mod = 998244353;

int t, n, m;
int b[kmax];
int p[kmax], pc;
long long res;

long long Calc(int x, int y) { // 容斥
  pc = 0;
  int j = y;
  for (int i = 2; i * i <= y; i++) { // 筛质因子
    if (j % i) continue;
    p[++pc] = i;
    for (; j % i == 0; j /= i) {
    }
  }
  if (j > 1) p[++pc] = j;
  long long res = 0;
  for (int i = 0; i < 1 << pc; i++) { // 枚举状态
    int op = 1;
    long long v = 1;
    for (int j = 0; j < pc; j++) {
      if (i & 1 << j) {
        op *= -1;
        v *= p[j + 1];
      }
    }
    res += op * x / v;
  }
  return res;
}

void Solve() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  res = 1;
  for (int i = 2; i <= n; i++) {
    if (b[i - 1] % b[i]) { // 无解
      cout << 0 << '\n';
      return;
    }
    res = res * Calc(m / b[i], b[i - 1] / b[i]) % Mod; // 统计答案
  }
  cout << res << '\n';
}

int main() {
  cin >> t;
  while (t--) {
    Solve();
  }
  return 0;
}

标签:Count,le,GCD,int,dfrac,容斥,long,CF1750D,include
From: https://www.cnblogs.com/ereoth/p/17404001.html

相关文章

  • Spark WordCount
    一:启动hadoop和sparkcd/usr/local/Cellar/hadoop/3.2.1/sbin./start-all.shcd/usr/local/Cellar/spark-3.0.0-preview2/sbin./start-all.sh二:JavaWordCount1.引入依赖依赖的版本号要与安装程序的版本号保持一致。<dependency><groupId>org.apache.spark</groupId><a......
  • 15、MapReduce介绍及wordcount
    文章目录Hadoop系列文章目录一、mapreduce编程模型1、MapReduce介绍2、MapReduce编程规范3、序列化4、hadoop数据类型5、示例二、wordcount实现1、pom.xml2、Mapper3、Reducer4、Driver5、完整的代码(WordCount)6、Driver推荐写法7、运行结果1)、运行日志2)、运行结果三、运行环境介绍......
  • A User Account Restriction Is Preventing You From Logging On
    AUserAccountRestrictionIsPreventingYouFromLoggingOn Theerrormessage"Youcannotloginduetoaccountrestrictions"isdisplayedwhenyoulogintothesystemusingtheremotedesktopfunction.Thisisbecau......
  • PowerShell-get-counter-算机上找不到任何性能计数器集: 错误 800007d0
    #已经解决了,感谢国外大神的解答:https://techcommunity.microsoft.com/t5/windows-powershell/get-counter-could-not-find-any-performance-counter-sets-on-the/m-p/3811330/thread-id/6430#M6433 获取计数器:在192.168.50.101计算机上找不到任何性能计数器集:错误80000 ......
  • mysql用户表root用户被锁定,无法登陆(Account is locked)
    今天看到mysql的user表就打开了看看,看到root还有些权限是N,然后顺手就改成了Y,结果保存之后就凉凉了,数据库就打不开了,报“Accountislocked”这个错误,上网排查了好半天才解决,解决方法记录一下。解决思路:1、使用skip-grant-tables跳过密码验证,此时可以打开MySQL服务并登录2、......
  • CF1608F MEX counting
    题意给定\(n,k\)和序列\(b_{1\dotsn}\),计数序列\(a_{1\dotsn}\)使得\(\foralli\in[1,n],\operatorname{mex}\limits_{j=1}^i\{a_j\}\in[b_i-k,b_i+k]\)。数据范围:\(1\leb_i\len\le2000,0\lek\le50\)。题解永远做不出简单题。我是弱智。考虑递推......
  • How to ensure all the deposits to exchange accounts are reflected properly?
    Accounts,includingexchangeaccounts,canreceivefundsintwoways:an“external”,or“top-level”transfer(eg.iff1XXXsendsamessagetof1ZZZthattransfers1FIL),and“internal”transfers”thatresultfromasubinvocation.Anexampleof“inter......
  • Eclipse won't launch because `reload maven project has encountered a proble m`
    关于eclipse因为maven无法启动的问题,参考以下两个网页http://www.91linux.com/html/2016/eclipse_1018/15540.htmlhttp://stackoverflow.com/questions/31080665/eclipse-wont-launch-because-reload-maven-project-has-encountered-a-proble-m不需要删除整个.metadata如果删除......
  • count(*)、count(1)、count(列名)有什么区别
    转载:https://juejin.cn/post/6854573219089907720https://juejin.cn/post/7152086171244298254......
  • 【工具类】可重用的CountDownLatch
    欢迎review代码,指出错误importjava.util.concurrent.CountDownLatch;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.atomic.AtomicLong;importjava.util.concurrent.locks.AbstractQueuedSynchronizer;/***可重用的CountDownLatch*增加reset......