当前位置:网站首页>HDU 4135: Co-prime (the principle of inclusion and exclusion)
HDU 4135: Co-prime (the principle of inclusion and exclusion)
2022-08-10 11:52: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
思路:Usually we ask1-n中与nThe number of coprime numbers is calculated by Euler's function! 但如果nBigger or bigger1~m中与nThe number of coprime numbers and so on,If you want to be more time efficient, you should use the principle of inclusion and exclusion!
容斥:先对n分解质因数,Record each prime factor separately, Then the number of non-coprime factors with a prime factor in the required interval is n / r(i),假设r(i)是ra prime factor of Suppose there are only three prime factors, The total number of non-coprime should be p1+p2+p3-p1*p2-p1*p3-p2*p3+p1*p2*p3, and the principle of inclusion and exclusion,You can turn to Baidu Encyclopedia to view related content pi代表n/r(i),That is, the number of numbers that are not coprime to a prime factor ,when there are more prime factors, It can be solved with state compression,二进制位上是1Indicates that the prime factor is taken in. 如果有奇数个1,就相加,Otherwise, it is subtracted.
AC代码:
边栏推荐
- A little self-deprecating deconstruction about farmers "code"
- Module 9 - Designing an e-commerce seckill system
- LeetCode50天刷题计划(Day 16—— 两两交换链表中的节点(9.10-10.30)
- SQL优化最强总结 (建议收藏~)
- 【电商运营】你真的了解社交媒体营销(SMM)吗?
- LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
- 皕杰报表在传参乱码
- blocking non-blocking poll mechanism asynchronous
- Article take you understand interrupt the key driver of polling mechanism
- 学长告诉我,大厂MySQL都是通过SSH连接的
猜你喜欢

Since the media hot style title how to write?Taught you how to write the title

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

Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇

AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)

项目部署、

APP automation testing practice based on UiAutomator2+PageObject mode

Module 9 - Designing an e-commerce seckill system

Nocalhost - Making development more efficient in the cloud-native era

Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户

From the product dimension, why can't we fully trust Layer2?
随机推荐
Introduction to Software Architecture
什么是幂等性?四种接口幂等性方案详解!
A case of violent parameter tuning in machine learning
面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
力扣练习——60 二叉搜索子树的最大键值和
SQL优化最强总结 (建议收藏~)
推荐6个自媒体领域,轻松易上手
即时零售业态下如何实现自动做账?
负载均衡原理分析与源码解读
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
L2 applications from a product perspective: why is it a playground?
Buckle exercise - rectangular area does not exceed the maximum value of K and (hard)
接口定义与实现
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
POJ 1026 Cipher (Permutation Groups)
Buckle Exercise - 61 Sort by frequency of characters
[Go WebSocket] 多房间的聊天室(一)思考篇
皕杰报表在传参乱码
Flutter气泡框实现
Redis设计与实现