hdu 4135 Co-prime(素数分解 容斥)

容斥做,欧拉好像比这个麻烦???

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5
 6 using namespace std;
 7 typedef long long ll;
 8
 9 ll solve(ll a,ll n){
10     vector<ll> v;
11     for(ll i = 2 ; i*i <= n ; i ++){    // 质数分解
12         if(n % i == 0){
13             v.push_back(i);
14             while(n%i == 0)    n/=i;
15         }
16     }
17     if(n > 1) v.push_back(n);
18     //有几位素因子 ,用二进制表示是否用过
19     ll sum = 0,tmp,cnt;
20     for(ll i = 1 ; i < (1<<v.size()) ; i ++){
21         cnt = 0;
22         tmp = 1;
23         for(ll j = 0 ; j < v.size() ; j ++){
24             if(i & (1<<j)){
25                 tmp *= v[j];    // 2 3;  6 12 18
26                 cnt++;
27             }
28         }
29         if(cnt & 1)  sum += a/tmp; //容斥 ,sum表示有1-a中有几个数字与n不互质
30         else sum -= a/tmp;
31     }
32     return a - sum;
33 }
34
35 int main(){
36     int T;
37     scanf("%d",&T);
38     for(int cas = 1 ; cas <= T ; cas ++){
39         ll a,b,n;
40         scanf("%lld%lld%lld",&a,&b,&n);
41         printf("Case #%d: %lld\n",cas,solve(b,n) - solve(a-1,n));
42     }
43     return 0;
44 }
时间: 2024-10-29 19:09:48

hdu 4135 Co-prime(素数分解 容斥)的相关文章

hdu 6059 Kanade&#39;s trio(trie+容斥)

题目链接:hdu 6059 Kanade's trio 题意: 给你n个数,让你找有多少个(i,j,k),使得i<j<k满足a[i]^a[j]<a[j]^a[k]. 题解: 首先考虑a[i]和a[k],将他们都转换成二进制,对于a[i]和a[k],我们用Bi[p]表示二进制下的a[i]的第p位.考虑a[i]和a[k]二进制不同的最高位,这里假设为p,如果Bi[p]=0,Bk[p]=1,那么Bj[p]要为0,才能使得a[i]^a[j]<a[j]^a[k].(因为p前面的位相同,只有亦

HDU 5072 Coprime (莫比乌斯反演+容斥+同色三角形)

Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1469    Accepted Submission(s): 579 Problem Description There are n people standing in a line. Each of them has a unique id number. Now

hdu 1695 GCD 欧拉函数+容斥

题意:给定a,b,c,d,k x属于[1 , c],y属于[1 , d],求满足gcd(x,y)=k的对数.其中<x,y>和<y,x>算相同. 思路:不妨设c<d,x<=y.问题可以转化为x属于[1,c / k ],y属于[1,d/k ],x和y互质的对数. 那么假如y<=c/k,那么对数就是y从1到c/k欧拉函数的和.如果y>c/k,就只能从[ c/k+1 , d ]枚举,然后利用容斥.详见代码: /****************************

HDU 4632 Palindrome subsequence (区间dp 容斥定理)

Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/Others) Total Submission(s): 2610    Accepted Submission(s): 1050 Problem Description In mathematics, a subsequence is a sequence that can be derived

hdu 5471(状压DP or 容斥)

想了最复杂的思路,用了最纠结的方法,花了最长的时间,蒙了一种规律然后莫名其妙的过了. MD 我也太淼了. 后面想了下用状压好像还是挺好写的,而且复杂度也不高.推出的这个容斥的规律也没完全想透我就CAO. Count the Grid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 400    Accepted Submission(s)

HDU 5838 (状压DP+容斥)

Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的点.   n<=5 , m<=5. 解题分析 考虑从1~n*m,从小到大依次填数,则如果某个位置编号为X且该位置还未填数,那么其周围的点均不能填数. 令dp[i][j]表示填到第i个数,状态为j . 令X的个数为cnt,那么 j ∈[ 0 , 1<<cnt). 一种情况为第i个数填在

Lucky HDU - 5213 (莫队,容斥)

WLD is always very lucky.His secret is a lucky number . is a fixed odd number. Now he meets a stranger with numbers:.The stranger asks him questions.Each question is like this:Given two ranges and ,you can choose two numbers and to make .The you can

HDU 4790:Just Random(容斥)

Just Random Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3932 Accepted Submission(s): 1276 Problem Description Coach Pang and Uncle Yang both love numbers. Every morning they play a game with n

hdu 4059 The Boss on Mars(容斥)

http://acm.hdu.edu.cn/showproblem.php?pid=4059 定义S = 1^4 + 2^4 + 3^4+.....+n^4,现在减去与n互质的数的4次方,问共减少了多少. 容斥原理,可以先把与n不互质的数的4次方求出来.那就先对n进行质因子分解,对质因子的组合运用容斥原理,质因子个数为奇数就加,偶数就减.其实与求[1,n]内与n互质的数的个数类似,该题重点是计算,防止乘法溢出. 对于求解1^4 + 2^4 + 3^4+.....+n^4,可以先类比1^2+2^2