当前位置:网站首页>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代码:
边栏推荐
猜你喜欢

孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感

关于振弦采集模块及采集仪振弦频率值准确率的问题

Flutter气泡框实现

rider内Mono脚本找不到引用资源

蔚来-软件开发工程师一面记录

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

从产品维度来看 我们为什么不能完全信任Layer2?

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

学长告诉我,大厂MySQL都是通过SSH连接的

LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
随机推荐
杭电多校-Loop-(不确定性贪心+线段树)
POJ 3101 Astronomy (数学)
蔚来-软件开发工程师一面记录
blocking non-blocking poll mechanism asynchronous
快速上手,征服三种不同分布式架构调用方案
力扣练习——58 验证二叉搜索树
Licking Exercise - 58 Verifying Binary Search Trees
LeetCode_443_压缩字符串
做自媒体月入几万?博主们都在用的几个自媒体工具
LeetCode50天刷题计划(Day 17—— 下一个序列(14.50-16.30)
Chapter 22 Source Code File REST API Reference (4)
怎么加入自媒体,了解这5种变现模式,让账号快速变现
Interviewer: How are Dao, Service, Controller, Util, and Model divided in the project?
Since the media hot style title how to write?Taught you how to write the title
Double.doubleToLongBits()方法使用
十年架构五年生活-09 五年之约如期而至
Emulate stm32 directly with proteus - the programmer can be completely discarded
Memory problems difficult to locate, it is because you do not use ASAN
[E-commerce operation] Do you really understand social media marketing (SMM)?
网络基础(第一节)