链接

D - Only one of two

题面

题意

求第\(k\)个只能被\(N\)或\(M\)整除的数

题解

\([1,x]\)中的能被\(n\)整除的数有\(\lfloor \frac{x}{n} \rfloor\)个

\([1,x]\)中的能被\(m\)整除的数有\(\lfloor \frac{x}{m} \rfloor\)个

\([1,x]\)中的能被\(n\)或\(m\)整除的数有\(\lfloor \frac{x}{n*m} \rfloor\)个

\([1,x]\)中的只能被\(n\)或\(m\)中一个数整除的数有\(\lfloor \frac{x}{n} \rfloor + \lfloor \frac{x}{m} \rfloor-2*\lfloor \frac{x}{n*m} \rfloor\)个

然后可以观察到答案具有单调性,能被\(n 、 m\)整除的数的个数随答案的增大增大。

代码

  1. #include <bits/stdc++.h>
  2. #define int long long
  3. #define rep(i,a,b) for(int i = (a); i <= (b); ++i)
  4. #define fep(i,a,b) for(int i = (a); i >= (b); --i)
  5. #define pii pair<int, int>
  6. #define pdd pair<double,double>
  7. #define ll long long
  8. #define db double
  9. #define endl '\n'
  10. #define x first
  11. #define y second
  12. #define pb push_back
  13. #define vi vector<int>
  14. using namespace std;
  15. const int maxn=1e5+10;
  16. void solve() {
  17. int n,m,k;
  18. cin>>n>>m>>k;
  19. int lcm=n*m/__gcd(n,m);
  20. int l=0,r=2e18;
  21. auto check=[&](int tar){
  22. if(tar/n+tar/m-2*(tar/lcm)>=k) return true;
  23. return false;
  24. };
  25. while(l<r){
  26. int mid=(l+r)>>1;
  27. if(check(mid)) r=mid;
  28. else l=mid+1;
  29. }
  30. cout<<l<<endl;
  31. }
  32. signed main() {
  33. ios::sync_with_stdio(false);
  34. cin.tie(0);
  35. cout.tie(0);
  36. // freopen("1.in", "r", stdin);
  37. int _;
  38. // cin>>_;
  39. // while(_--)
  40. solve();
  41. return 0;
  42. }

总结

\([1,x]\)中的能被\(n\)整除的数有\(\lfloor \frac{x}{n} \rfloor\)个

\([1,y]\)中的能被\(n\)整除的数有\(\lfloor \frac{y}{n} \rfloor\)个

\([x,y]\)中的能被\(n\)整除的数有\(\lfloor \frac{y}{n} \rfloor - \lfloor \frac{x}{n} \rfloor\)个

Toyota Programming Contest 2024#2(AtCoder Beginner Contest 341)D - Only one of two(数论、二分)的更多相关文章

  1. KYOCERA Programming Contest 2021(AtCoder Beginner Contest 200) 题解

    KYOCERA Programming Contest 2021(AtCoder Beginner Contest 200) 题解 哦淦我已经菜到被ABC吊打了. A - Century 首先把当前年 ...

  2. HHKB Programming Contest 2022 Winter(AtCoder Beginner Contest 282)

    前言 好久没有打 AtCoder 了.有点手生.只拿到了 \(\operatorname{rk}1510\),应该上不了多少分. 只切了 \(\texttt{A,B,C,D}\) 四题. A - Ge ...

  3. AtCoder Beginner Contest 255(E-F)

    Aising Programming Contest 2022(AtCoder Beginner Contest 255) - AtCoder E - Lucky Numbers 题意: 给两个数组a ...

  4. AtCoder Beginner Contest 076

    A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...

  5. AtCoder Beginner Contest 184 题解

    AtCoder Beginner Contest 184 题解 目录 AtCoder Beginner Contest 184 题解 A - Determinant B - Quizzes C - S ...

  6. atcoder beginner contest 251(D-E)

    Tasks - Panasonic Programming Contest 2022(AtCoder Beginner Contest 251)\ D - At Most 3 (Contestant ...

  7. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  8. AtCoder Beginner Contest 052

    没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...

  9. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  10. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

随机推荐

  1. 微信小程序-Storage

    官方文档地址:https://developers.weixin.qq.com/miniprogram/dev/api/storage/wx.setStorageSync.html ?> Sto ...

  2. C/C++ 类与构造析构等知识

    简单定义类 #include <iostream> #include <string> using namespace std; class Student { public: ...

  3. 监控Celery不一定非要使用Flower

    运维平台中有许多的周期/定时/异步任务,例如证书扫描.数据备份.日志清理.线上作业等等,这些任务的执行都是借助于Celery来完成的.任务多了之后就会遇到一系列的问题,例如我之前写过的将任务分多队列来 ...

  4. 使用s3fs-fuse挂载minio文件时无法删除问题排查过程

    使用s3fs-fuse挂载minio文件时无法删除问题排查过程 结论:部分场景无法满足,具体问题详见正文 1. 部署minio docker run    -p 9000:9100    -p 909 ...

  5. (python)做题记录||2024.2.4||题目是codewars的【 All Balanced Parentheses】

    题目链接:https://www.codewars.com/kata/5426d7a2c2c7784365000783/python 我的解决方案: def balanced_parens(n): # ...

  6. 域名解析迟迟不生效,刷新本地DNS的方法

    ipconfig /flushdns 刷新后,再ping  发生域名解析 的指向就对了.!

  7. Android里使用AspectJ实现AOP

     前言 Aspectj提供一种在字符串里编程的模式,即在字符串里写函数,然后程序启动的时候会动态的把字符串里的函数给执行了. 例如: "execution(* *(..))" 这里 ...

  8. win32--GetFileAttributes

    DWORD d = GetFileAttributes(path.c_str()); 根据返回的十进制,对比文件属性,来检索指定文件或目录的文件系统属性. 也可以使用 if ((d & FIL ...

  9. win32 - WaitForMultipleObjects的使用

    创建5个线程,并无限期地打印某些内容 #include <Windows.h> #include <stdio.h> DWORD IDs[5]; DWORD WINAPI Th ...

  10. 【Android 逆向】【攻防世界】android2.0

    这是一道纯算法还原题 1. apk安装到手机,提示输入flag,看来输入就是flag 2. jadx 打开apk查看 this.button.setOnClickListener(new View.O ...