HDU4135(容斥原理)
Co-prime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4090 Accepted Submission(s): 1619
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.
#include <cstdio>
#include <vector>
using namespace std;
typedef long long LL;
LL sieve(LL m,LL n)
{
vector<LL> divisor;
for(LL i=;i*i<=n;i++)
{
if(n%i==)
{
divisor.push_back(i);
while(n%i==) n/=i;
}
}
if(n>) divisor.push_back(n);
LL ans=;
for(LL mark=;mark<(<<divisor.size());mark++)
{
LL odd=;
LL mul=;
for(LL i=;i<divisor.size();i++)
{
if(mark&(<<i))
{
mul*=divisor[i];
odd++;
}
}
LL cnt=m/mul;
if(odd&) ans+=cnt;
else ans-=cnt;
}
return m-ans;
}
LL a,b,n;
int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
scanf("%lld%lld%lld",&a,&b,&n);
LL res=sieve(b,n)-sieve(a-,n);
printf("Case #%d: ",cas);
printf("%lld\n",res);
}
return ;
}
HDU4135(容斥原理)的更多相关文章
- hdu4135容斥原理 组合遍历
容斥原理实现的关键在于:组合遍历,即如何遍历2^n种组合. 容斥原理的三种写法: DFS 队列数组 位数组 #include<stdio.h> #include<iostream&g ...
- HDU4135容斥原理
#include <cstdio> #include <string.h> #include <cmath> using namespace std; #defin ...
- HDU4135 Co-prime(容斥原理)
题目求[A,B]区间内与N互质数的个数. 可以通过求出区间内与N互质数的个数的前缀和,即[1,X],来得出[A,B]. 那么现在问题是求出[1,X]区间内与N互质数的个数,考虑这个问题的逆问题:[1, ...
- hdu4135 Co-prime 容斥原理
Given a number N, you are asked to count the number of integers between A and B inclusive which are ...
- 容斥原理的(二进制思想和质因子分解+模板)hdu4135+ecf81.D
题:http://acm.hdu.edu.cn/showproblem.php?pid=4135 题意:求[A,B]与N互质的数的个数 #include<iostream> #includ ...
- hdu4135 Co-prime【容斥原理】
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)
hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...
- 容斥原理应用(求1~r中有多少个数与n互素)
问题:求1~r中有多少个数与n互素. 对于这个问题由容斥原理,我们有3种写法,其实效率差不多.分别是:dfs,队列数组,位运算. 先说说位运算吧: 用二进制1,0来表示第几个素因子是否被用到,如m=3 ...
- hdu4059 The Boss on Mars(差分+容斥原理)
题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设 则 为一阶差分. 二阶差分: n阶差分: 且可推出 性质: 1. ...
随机推荐
- linux 无密码登录
环境:Linux 脚本:Python 功能:批量IP,远程执行命令.拷贝文件 运行:./ssh_scp.py iplist.txt 脚本内容: #!/usr/bin/env python# -*- c ...
- MySQL数据库基本操作(四)
在进行查询之前,我们要先建好关系表,并往数据表中插入些数据.为查询操作做好准备. 五张关系表的创建: #创建并进入数据库: mysql> CREATE DATABASE `info`; Quer ...
- Logger日志级别说明及设置方法、说明
日志记录器(Logger)是日志处理的核心组件.log4j具有5种正常级别(Level).日志记录器(Logger)的可用级别Level (不包括自定义级别 Level), 以下内容就是摘自log4j ...
- sqoop学习3(数据导入乱码问题)
sqoop将mysql数据库中数据导入hdfs或hive中后中文乱码问题解决办法 [root@spark1 ~]# vi /etc/my.cnf 修改配置文件 在文件内的[mysqld]和client ...
- HBase-存储-HFile格式
HBase-存储-HFile格式 实际的存储文件功能是由HFile类实现的,它被专门创建以达到一个目的:有效地存储HBase的数据.它们基于Hadoop的TFile类,并模仿Google的BigTab ...
- 区间dp的感悟
学区间dp似乎也很久了...对区间dp的通用模型都了解了一些 但是做题还是很坑 上了一点难度的题基本想不出什么思路.. 目前的做题方式就是看题 想一会发现自己不会做 看题解 好巧妙啊 理解后写一发.. ...
- svn更新时忽略指定文件或文件夹
选择一个收SVN控制的文件夹->右击->选择TortoiseSVN->更新至版本,就会出现 选择更新深度为工作副本,再选择项目,出现如图中所示的界面,把不想更新的文件或者文件夹前 ...
- 【NOIP2014 普及组】螺旋矩阵
[NOIP2014 普及组]螺旋矩阵 一.题目 [NOIP2014 普及组]螺旋矩阵 时间限制: 1 Sec 内存限制: 128 MB 提交: 18 解决: 0 [提交][状态][讨论版] 题目描 ...
- Advanced SQL: Relational division in jOOQ
i Rate This Relational algebra has its treats. One of the most academic features is the ...
- unity监测按下键的键值并输出+unity键值
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using U ...