当前位置:网站首页>HDU 4135:Co-prime (容斥原理)
HDU 4135:Co-prime (容斥原理)
2022-08-10 11:08:00 【51CTO】
Co-prime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4289 Accepted Submission(s): 1698
Problem Description
Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
Input
The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 10 15) and (1 <=N <= 10 9).
Output
For each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow the output format below.
Sample Input
Sample Output
思路:通常我们求1-n中与n互质的数的个数都是用欧拉函数! 但如果n比较大或者是求1~m中与n互质的数的个数等等问题,要想时间效率高的话还是用容斥原理!
容斥:先对n分解质因数,分别记录每个质因数, 那么所求区间内与某个质因数不互质的个数就是n / r(i),假设r(i)是r的某个质因子 假设只有三个质因子, 总的不互质的个数应该为p1+p2+p3-p1*p2-p1*p3-p2*p3+p1*p2*p3, 及容斥原理,可以转向百度百科查看相关内容 pi代表n/r(i),即与某个质因子不互质的数的个数 ,当有更多个质因子的时候, 可以用状态压缩解决,二进制位上是1表示这个质因子被取进去了。 如果有奇数个1,就相加,反之则相减。
AC代码:
边栏推荐
- Short video software development - how to break the platform homogenization
- LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
- 第二十二章 源代码文件 REST API 参考(四)
- Codeforces 814 C. An impassioned circulation of affection (dp)
- Pulling drills - 56 Finding the right interval
- Unsafe的一些使用技巧
- 1-IMU参数解析以及选择
- 使用.NET简单实现一个Redis的高性能克隆版(六)
- 【勇敢饭饭,不怕刷题之链表】链表倒数节点问题
- Hangdian Multi-School-Loop-(uncertainty greedy + line segment tree)
猜你喜欢

MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细

什么是幂等性?四种接口幂等性方案详解!

How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize

【勇敢饭饭,不怕刷题之链表】链表倒数节点问题

Do self-media monthly income tens of thousands?Several self-media tools that bloggers are using

使用哈工大LTP测试分词并且增加自定义字典

From the product dimension, why can't we fully trust Layer2?

Where can I view the version record of WeChat applet submission review history?

Redis设计与实现

LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
随机推荐
【机器学习】浅谈正规方程法&梯度下降
POJ 2891 Strange Way to Express Integers (Extended Euclidean)
std::move()
ISO9001在讲什么?过程方法和风险思维
A little self-deprecating deconstruction about farmers "code"
石墨文档打开文档时快速定位到上次写的位置
从脚本到剪辑,影像大师亲授的后期制作秘籍
力扣练习——62 有效的数独
Introduction to Software Architecture
从产品角度看 L2 应用:为什么说这是一个游乐场?
中小规模网站架构
实现内网穿透的最佳解决方案(无实名认证,完全免费)
AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
阻塞 非阻塞 poll机制 异步
OSSCore 开源解决方案介绍
YTU 2894: G--我要去内蒙古大草原
【TypeScript】接口类型与类型别名:这两者的用法与区别分别是什么?
Where can I view the version record of WeChat applet submission review history?
关于“码农”的一点自嘲解构
The brave rice rice, does not fear the brush list of 】 list has a ring