当前位置:网站首页>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代码:
边栏推荐
- rider内Mono脚本找不到引用资源
- 态路小课堂丨如何为CXP光模块选择光纤跳线?
- 2023版揽胜运动曝光,安全、舒适一个不落
- 快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
- Clicking Exercise - 64 Longest Harmonic Subsequences
- 不止跑路,拯救误操作rm -rf /*的小伙儿
- 负载均衡原理分析与源码解读
- Licking Exercise - 63 Find all anagrams in a string
- Centos7环境使用Mysql离线安装包安装Mysql5.7
- Get started quickly and conquer three different distributed architecture calling schemes
猜你喜欢
【勇敢饭饭,不怕刷题之链表】链表倒数节点问题
即时零售业态下如何实现自动做账?
基于UiAutomator2+PageObject模式开展APP自动化测试实战
Weilai-software development engineer side record
振弦传感器及核心VM系列振弦采集模块
常量及数据类型你还记得多少?
VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
Redis设计与实现
gpu-admission 源码分析
皕杰报表在传参乱码
随机推荐
石墨文档打开文档时快速定位到上次写的位置
1-IMU参数解析以及选择
力扣练习——59 从二叉搜索树到更大和树
Programmers pursue technology to consolidate basic learning route suggestions
AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
A case of violent parameter tuning in machine learning
为什么Redis很快
学长告诉我,大厂MySQL都是通过SSH连接的
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
力扣练习——62 有效的数独
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
[E-commerce operation] Do you really understand social media marketing (SMM)?
From the product dimension, why can't we fully trust Layer2?
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
力扣练习——63 找到字符串中所有字母异位词
不止跑路,拯救误操作rm -rf /*的小伙儿
Redis设计与实现
建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
Introduction to Software Architecture
英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞