题意:给出[a,b]区间内与n互质的个数

思路:如果n比较小,我们可以用欧拉函数解决,但是n有1e9。要求区间内互质,我们可以先求前缀内互质个数,即[1,b]内与n互质,求互质,可以转化为求不互质,也就是有除1的公因数。那么我们把n质因数分解,就能算出含某些公因数的不互质的个数。因为会重复,所以容斥解决。因为因数个数可能很多(随便算了一个20!> 2e18,所以质因数分解个数不会超过20个),我们可以用二进制来遍历解决。

#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = + ;
const int seed = ;
const int MOD = + ;
const int INF = 0x3f3f3f3f;
ll y[], cnt;
ll a, b, n;
ll solve(ll num){
ll sum = , val, tot;
for(ll i = ; i < ( << cnt); i++){ //这里是小于最多只能cnt位
val = ;
tot = ;
for(ll j = ; j < cnt; j++){
if(i & ( << j)){ //第j个因子被用到
val *= y[j];
tot++;
}
}
if(tot & ){
sum += num / val;
}
else{
sum -= num / val;
}
}
return num - sum;
}
int main(){
int T, Case = ;
scanf("%d", &T);
while(T--){
scanf("%lld%lld%lld", &a, &b, &n);
cnt = ;
ll x = n;
for(ll i = ; i * i <= x; i++){
if(x % i == ){
y[cnt++] = i;
while(x % i == ){
x /= i;
}
}
}
if(x > ){
y[cnt++] = x;
}
printf("Case #%d: %lld\n", Case++, solve(b) - solve(a - ));
}
return ;
}

HDU 4135 Co-prime(容斥:二进制解法)题解的更多相关文章

  1. 题解报告:hdu 4135 Co-prime(容斥定理入门)

    Problem Description Given a number N, you are asked to count the number of integers between A and B ...

  2. HDU 4135 Co-prime(容斥+数论)

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. HDU 4135:Co-prime(容斥+二进制拆分)

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  4. C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥

    C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...

  5. HDU 5297 Y sequence 容斥 迭代

    Y sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5297 Description Yellowstar likes integer ...

  6. hdu 6053 trick gcd 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意:给定一个数组,我们定义一个新的数组b满足bi<ai 求满足gcd(b1,b2....bn)&g ...

  7. HDU 4336 Card Collector(容斥)

    题意:要收集n种卡片,每种卡片能收集到的概率位pi,求收集完这n种卡片的期望.其中sigma{pi} <=1; 思路:容斥原理.就是一加一减,那么如何算期望呢.如果用二进制表示,0表示未收集到, ...

  8. HDU 3970 Harmonious Set 容斥欧拉函数

    pid=3970">链接 题解:www.cygmasot.com/index.php/2015/08/17/hdu_3970 给定n  求连续整数[0,n), 中随意选一些数使得选出的 ...

  9. [HDU4135]CO Prime(容斥)

    也许更好的阅读体验 \(\mathcal{Description}\) \(t\)组询问,每次询问\(l,r,k\),问\([l,r]\)内有多少数与\(k\)互质 \(0<l<=r< ...

随机推荐

  1. 《javascript算法--对象的比较》

    方法一:JSON.stringify() 此方法简单,适用于当两个对象的属性顺序相同的时候. var user1 = {name : "nerd", org: "dev& ...

  2. isScroll的滚动组件的用法

    <div class="wrapper">  <ul>     <li>1</li>     <li>2</li& ...

  3. [sh]sh最佳实战(含grep)

    sh虐我千百遍,我待sh如初恋. sh复习资料 http://www.cnblogs.com/iiiiher/p/5385108.html http://blog.csdn.net/iiiiher/a ...

  4. [LeetCode] 176. Second Highest Salary_Easy tag: SQL

    Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...

  5. jenkins maven testng selenium自动化持续集成

    准备环境 首先我们新建一个maven的工程,并且在pom.xml中配置好我们依赖的一些jar包 <dependencies> <dependency> <groupId& ...

  6. Maven的常用功能

    一.jar包管理(仓库) 最佳实践: 1:排除依赖 pom中依赖了某个第三方API,此API又依赖其它API,有可能出问题.(必须申明排除该第三方依赖) Spring-core又隐式的依赖了commo ...

  7. Servlet—生命周期

    首次访问 重复访问 换浏览器访问(说明在一次项目运行期间,servlet实例只会创建一个)

  8. ASP.NET控件--DropDownList

      设置默认值:DropDownList1.Items[i].Selected=true;绑定:DropDownList1.DataSource = dataSet.Tables["Tabl ...

  9. Python: ord()函数

    ch() , unichr() , ord() ①chr()函数用一个范围在range(256)内的整数作参数,返回一个对应的字符. >>>chr(65) 'A' ②unichr() ...

  10. 数据仓库基础(十三)Informatica workflow

    本文转载自:http://www.cnblogs.com/evencao/p/3154715.html 看了几天的Informatica ,关于infor的资料也比较少,主要的<商业智能深入浅出 ...