ZOJ2836-Number Puzzle-容斥原理

依次考虑一个数的倍数,两个数的倍数(lcm),三个数的倍数(lcm)。。。

会发现有这么一个规律,奇数个数时要加上情况数,偶数个数时要减去情况数。

一种只有10个数,用二进制枚举所有情况即可。

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

int N,M,save[100];
long long ans;

int main()
{
    while(~scanf("%d%d",&N,&M))
    {
        for(int i=0;i<N;i++)
        {
            scanf("%d",&save[i]);
        }
        ans = 0;

        for(int i=1;i<(1<<N);i++)
        {
            int num = 0,lcm = 1;
            for(int j=0;j<N;j++)
            {
                if((1<<j) & i)
                {
                    lcm = (lcm*save[j])/__gcd(lcm,save[j]);
                    num++;
                }
            }

            if(num & 1)
                ans += M/lcm;
            else
                ans -= M/lcm;
        }

        printf("%lld\n",ans);
    }
}
时间: 2024-08-02 06:43:42

ZOJ2836-Number Puzzle-容斥原理的相关文章

ZOJ 2836 Number Puzzle ( 容斥原理 )

ZOJ 2836 Number Puzzle( 容斥原理 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define CLR( a, b ) memset( a, b, sizeof(a) ) int m, n, A[11]; LL gcd( LL a, LL b ) { return b == 0 ? a :

[容斥原理] zoj 2836 Number Puzzle

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1836 Number Puzzle Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a list of integers (A1, A2, ..., An), and a positive integer M, please find the number of positive integers th

ZOJ 2836 Number Puzzle

Number Puzzle Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 283664-bit integer IO format: %lld      Java class name: Main Given a list of integers ($A_1, A_2, ..., A_n$), and a positive integer M, please fi

ACM HDU 1755 -- A Number Puzzle

A Number Puzzle Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 938    Accepted Submission(s): 276 Problem Description Lele 最近上课的时候都很无聊,所以他发明了一个数字游戏来打发时间.这个游戏是这样的,首先,他拿出几张纸片,分别写上0到9之间的任意数字(可重复写

ZOJ 2836 Number Puzzle(容斥原理啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1836 Given a list of integers (A1, A2, ..., An), and a positive integer M, please find the number of positive integers that are not greater than M and dividable by any integer from the g

hdu 4390 Number Sequence (容斥原理)

Number Sequence Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 790    Accepted Submission(s): 331 Problem Description Given a number sequence b1,b2-bn. Please count how many number sequences

Number Puzzle

Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a list of integers (A1, A2, ..., An), and a positive integer M, please find the number of positive integers that are not greater than M and dividable by any integer from the given list. Input Th

[全排列]--A Number Puzzle

Lele 最近上课的时候都很无聊,所以他发明了一个数字游戏来打发时间. 这个游戏是这样的,首先,他拿出几张纸片,分别写上0到9之间的任意数字(可重复写某个数字),然后,他叫同学随便写两个数字X和K.Lele要做的事情就是重新拼这些纸牌,组成数字 T ,并且 T + X 是 K 的正整数倍. 有时候,当纸片很多的时候,Lele经常不能在一节课之内拼出来,但是他又想知道答案,所以,他想请你帮忙写一个程序来计算答案. Input 本题目包含多组测试数据,请处理到文件结束. 每组数据第一行包含两个整数

E - Puzzle( UVA-227)

 Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the frame, t

Puzzle UVA - 227

A children's puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the frame, the frame