HDU 1796 How many integers can you find 解题报告(数论)

96 篇文章 0 订阅

How many integers can you find

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


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
 

    解题报告:0 - n 间能被集合中整除的数有多少个。暴力肯定不行,最大的n是2^31。

    思考一下,0 - n 间能被4整除的数有多少个?n / 4 个。

    那么 0 - n 间能被6整除的数有多少个? n / 6 个。

    而 0 -  n 间能被{4, 6}整除的数应该是 n / 4 + n / 6 - n / lcm(4, 6) 个。因为有重复。

    重复此过程,集合中数字最多有10个,可以用数组存储每次lcm的值。

    AC代码如下:

#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long LL;

int array[21];
int last[10000];
int top;

int gcd(int a, int b)
{
	return b == 0 ? a : gcd(b, a%b);
}

LL lcm(int a, int b)
{
	return (LL)a / gcd(a, b) * b;
}

int main()
{
#ifdef ACM
	freopen("in.txt", "r", stdin);
#endif

	int n, m;
	while (~scanf("%d%d", &n, &m))
	{
		n--;
		top = 0;

		for (int i = 0; i < m; i++)
		{
			scanf("%d", array + i);
			if (array[i] == 0) m--, i--;
		}
		sort(array, array + m);

		int newM = 0;
		for (int i = 0; i < m; i++)
		{
			bool ok = true;
			for (int j = 0; j < i; j++)
			{
				if (array[i] % array[j] == 0)
				{
					ok = false;
					break;
				}
			}
			if (ok)
			{
				array[newM++] = array[i];
			}
		}
		m = newM;

		int res = 0;
		for (int i = 0; i < m; i++)
		{
			int a = array[i];
			res += n / a;

			int newTop = top;
			last[newTop++] = a;

			for (int j = 0; j < top; j++)
			{
				LL t;
				if (last[j] < 0)
					t = lcm(a, -last[j]);
				else
					t = -lcm(a, last[j]);
				if (t <= n)
				{
					last[newTop++] = (int)t;
					res += n / (int)t;
				}
			}

			top = newTop;
		}

		printf("%d\n", res);
	}
}

    上面这段不是太好看,但是偶然间发现这段代码是杭电上最快的= =,0MS

    标准点的容斥原理应该这么写:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int num[22];

int gcd(int a, int b)
{
    return b==0?a:gcd(b, a%b);
}

int lcm(int a, int b)
{
    return a/gcd(a,b)*b;
}

int main()
{
    int n, m;
    while(~scanf("%d%d", &n, &m))
    {
        n--;

        for(int i=0; i<m; i++)
        {
            scanf("%d", num+i);
            if(num[i]==0)
                m--,i--;
        }

        int ans = 0;
        for(int i=1;i<(1<<m);i++)
        {
            long long tmp = 1;
            bool flag = false;
            for(int j=0;j<m;j++) if(i&(1<<j))
                tmp = lcm(tmp, num[j]), flag = !flag;
            if(flag)
                ans += n/tmp;
            else
                ans -= n/tmp;
        }

        printf("%d\n", ans);
    }
}

    因为多次重复lcm,所有代码的效率很慢。时效640MS。

    另外,本题和ZOJ 2836 近乎相同,将scanf m,n 的顺序交换即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值