当前位置:网站首页>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代码:
边栏推荐
- LCD驱动端与设备端名称匹配过程分析(Tiny4412)
- 力扣练习——59 从二叉搜索树到更大和树
- Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
- Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability
- Where can I view the version record of WeChat applet submission review history?
- 振弦传感器及核心VM系列振弦采集模块
- 什么是幂等性?四种接口幂等性方案详解!
- 【Untitled】
- ENVI 5.3软件安装包和安装教程
- 再有人问你分布式事务,把这篇扔给他
猜你喜欢
Some tips for using Unsafe
接口定义与实现
Where can I view the version record of WeChat applet submission review history?
从源码角度分析UUID的实现原理
使用哈工大LTP测试分词并且增加自定义字典
【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
振弦传感器及核心VM系列振弦采集模块
A case of violent parameter tuning in machine learning
mysql appears: ERROR 1524 (HY000): Plugin '123' is not loaded
随机推荐
力扣练习——60 二叉搜索子树的最大键值和
2022年裁员潮,失业程序员何去何从?
Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability
力扣练习——62 有效的数独
StoneDB Document Bug Hunting Season 1
LeetCode_628_三个数的最大乘积
网络套接字(UDP和TCP编程)
Article take you understand interrupt the key driver of polling mechanism
Nocalhost - 让云原生时代的开发更高效
实现内网穿透的最佳解决方案(无实名认证,完全免费)
VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决
Redis常用命令
Centos7环境使用Mysql离线安装包安装Mysql5.7
力扣练习——56 寻找右区间
关于振弦采集模块及采集仪振弦频率值准确率的问题
【机器学习】浅谈正规方程法&梯度下降
不止跑路,拯救误操作rm -rf /*的小伙儿
【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
10 个 Reduce 常用“奇技淫巧”
Licking Exercise - 58 Verifying Binary Search Trees