UVA 10325

题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32762

题意:求1~n中不能被给定m个数中任意一个数整除的数的个数
分析:
和之前的例题非常像,我们可以 n –sum,sum表示所有能被这些数整除的数的个数。
sum = sum +/- n/某种组合的最小公倍数
在做的过程中我们需要枚举子集,枚举子集的时候我们可以用之前学过的二进制枚举的方法。
上代码:

#include <iostream>

using namespace std;
typedef long long LL;
LL gcd(LL m, LL n)
{
    if(n == 0)
    return m;
    return gcd(n, m%n);
}
LL lcm(LL m, LL n)
{
    return m/gcd(m, n)*n;
}
LL data[20];
int main()
{
    int m;
    LL n,ans;
    while(cin>>n>>m)
    {
        for(int i=0; i<m; i++)
          cin>>data[i];
        ans=0;
        for(int i=1; i<(1<<m); i++)
        {
            LL l=1,f=0;
            for(int j=0; j<m; j++)
            {
                if((1<<j)&i)
                {
                    l=lcm(l,data[j]);
                    if(l>n)
                    break;
                    f++;
                }
            }
            if(f&1)
            ans+=n/l;
            else
            ans-=n/l;
        }
        cout<<n-ans<<endl;
    }
    return 0;
}
时间: 2024-10-03 01:21:33

UVA 10325的相关文章

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.

UVa 10183 How Many Fibs? (统计斐波那契数个数&amp;amp;高精度)

10183 - How Many Fibs? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1124 Recall the definition of the Fibonacci numbers:    f1 := 1    f2 := 2    fn :

UVa 701 The Archeologists&#039; Dilemma: 数学及枚举

701 - The Archeologists' Dilemma Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=642 An archeologist seeking proof of the presence of extraterrestrials i

UVa 550 Multiplying by Rotation:模拟乘法

550 - Multiplying by Rotation Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=491 Warning: Not all numbers in this problem are decimal numbers! Multiplic

UVA 之401 - Palindromes

A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string is read from ri