HDU - 1796 How many integers can you find && HDU 4135 Co-prime (容斥)

3 篇文章 0 订阅


How many integers can you find

Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7953    Accepted Submission(s): 2364


Problem Description
  Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N=12, and M-integer set is {2,3}, so there is another set {2,3,4,6,8,9,10}, all the integers of the set can be divided exactly by 2 or 3. As a result, you just output the number 7.
 

Input
  There are a lot of cases. For each case, the first line contains two integers N and M. The follow line contains the M integers, and all of them are different from each other. 0<N<2^31,0<M<=10, and the M integer are non-negative and won’t exceed 20.
 

Output
  For each case, output the number.
 

Sample Input
  
  
12 2 2 3
 

Sample Output
  
  
7
 

Author
wangye
 

Source
 

Recommend
wangye
 

题目大意:给定n和一个大小为m的集合,集合元素为非负整数。为1...n内能被集合里至少一个数整除的数字个数。n<=2^31,m<=10

trick:给的集合里面可能有0,所有取模,gcd,lcm都要注意有没有0,有0要提出来。。。 

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
ll a[15];
int main()
{
    ll n, m;
    while(~scanf("%lld%lld", &n, &m))
    {
        int cnt = 0, x;
        for(int i = 1; i <= m; i++)
        {
            scanf("%d", &x);
            if(x!=0) a[cnt++] = x;
        }
        ll ans = 0;
        for(int i = 1; i < (1<<cnt); i++)  //容斥代码, 奇加偶减。。
        {
            int sum = 0;
            for(int j = i; j > 0; j >>= 1)
                sum += j&1;
            ll lcm = 1;
            for(int j = 0; j < cnt; j++)
            {
                if((1<<j)&i)
                    lcm = lcm/__gcd(a[j], lcm)*a[j];
            }
            if(sum%2) ans += (n-1)/lcm;
            else ans -= (n-1)/lcm;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

Co-prime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4683    Accepted Submission(s): 1867


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
  
  
2 1 10 2 3 15 5
 

Sample Output
  
  
Case #1: 5 Case #2: 10
Hint
In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.
 

Source
 

Recommend
lcy
 

思路i转自:http://www.cnblogs.com/jiangjing/archive/2013/06/03/3115470.html

题意:就是让你求(a,b)区间于n互质的数的个数.

分析:我们可以先转化下:用(1,b)区间与n互质的数的个数减去(1,a-1)区间与n互质的数的个数,那么现在就转化成求(1,m)区间于n互质的数的个数,如果要求的是(1,n)区间与n互质的数的个数的话,我们直接求出n的欧拉函数值即可,可是这里是行不通的!我们不妨换一种思路:就是求出(1,m)区间与n不互质的数的个数,假设为num,那么我们的答案就是:m-num!现在的关键就是:怎样用一种最快的方法求出(1,m)区间与n不互质的数的个数?方法实现:我们先求出n的质因子(因为任何一个数都可以分解成若干个质数相乘的),如何尽快地求出n的质因子呢?我们这里又涉及两个好的算法了!第一个:用于每次只能求出一个数的质因子,适用于题目中给的n的个数不是很多,但是n又特别大的;(http://www.cnblogs.com/jiangjing/archive/2013/06/03/3115399.html)第二个:一次求出1~n的所有数的质因子,适用于题目中给的n个数比较多的,但是n不是很大的。(http://www.cnblogs.com/jiangjing/archive/2013/06/01/3112035.html)本题适用第一个算法!举一组实例吧:假设m=12,n=30.

第一步:求出n的质因子:2,3,5;

第二步:(1,m)中是n的因子的倍数当然就不互质了(2,4,6,8,10)->n/2  6个,(3,6,9,12)->n/3  4个,(5,10)->n/5  2个。

如果是粗心的同学就把它们全部加起来就是:6+4+2=12个了,那你就大错特错了,里面明显出现了重复的,我们现在要处理的就是如何去掉那些重复的了!

第三步:这里就需要用到容斥原理了,公式就是:n/2+n/3+n/5-n/(2*3)-n/(2*5)-n/(3*5)+n/(2*3*5).

第四步:我们该如何实现呢?我在网上看到有几种实现方法:dfs(深搜),队列数组,位运算三种方法都可以!上述公式有一个特点:n除以奇数个数相乘的时候是加,n除以偶数个数相乘的时候是减。我这里就写下用队列数组如何实现吧:我们可以把第一个元素设为-1然后具体看代码如何实现吧!

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn = 1e3;
ll prime[maxn], l, r, n;
int cnt;
void get_prime(ll num)
{
    cnt = 0;
    for(ll i = 2; i*i <= num; i++)
    {
        if(!num) break;
        if(num%i == 0)
        {
            prime[cnt++] = i;
            while(num%i == 0) num /= i;
        }
    }
    if(num > 1) prime[cnt++] = num;
}
ll rep(ll num)
{
    ll ans = 0;
    for(int i = 1; i < (1<<cnt); i++)
    {
        int sum = 0;
        ll val = 1;
        for(int j = 0; j < cnt; j++)
        {
            if(i & (1<<j))
                val *= prime[j], sum++;
        }
        if(sum&1) ans += num/val;
        else ans -= num/val;
    }
    return num-ans;
}
int main()
{
    int t, ca = 1;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%lld%lld%lld", &l, &r, &n);
        memset(prime, 0, sizeof(prime));
        get_prime(n);
        printf("Case #%d: %lld\n",ca++, rep(r)-rep(l-1));
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值