codeforses The Lottery(容斥定理)

题意:

给n,m,和m个数(k1~km)。求1~n中有多少个数不是(k1~km)中任意一数的倍数。

题解:

容斥模板题。a的倍数有n/a个;既是a,也是b的倍数,即为lcm(a,b)的倍数。是a,b,c的倍数,即为lcm(a,b,c)的倍数。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
LL a[20],m;
LL n,ans;

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

void dfs(int c,int cur,int i,LL ans1)      //dfs(c,1,i,0,1);
{
    if(cur==c+1)
    {
        if(c&1)
            ans-=n/ans1;
        else
            ans+=n/ans1;
        return;
    }
    for(;i<m;i++)
    {
        dfs(c,cur+1,i+1,lcm(ans1,a[i]));
    }
}

int main()
{
    while(cin>>n>>m)
    {
        for(int i=0;i<m;i++)
            scanf("%lld",&a[i]);
        ans=n;
        for(int c=1;c<=m;c++)
            dfs(c,1,0,1);
        printf("%lld\n",ans);
    }
}

The Lottery

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status

The Sports Association of Bangladesh is in great problem with their latest lottery ‘Jodi laiga Jai’. There are so many participants this time that they cannot manage all the numbers. In an urgent meeting they have decided that they will ignore some numbers. But how they will choose those unlucky numbers!!! Mr. NondoDulal who is very interested about historic problems proposed a scheme to get free from this problem. You may be interested to know how he has got this scheme. Recently he has read the Joseph’s problem. There are N tickets which are numbered from 1 to N. Mr. Nondo will choose M random numbers and then he will select those numbers which is divisible by at least one of those M numbers. The numbers which are not divisible by any of those M numbers will be considered for the lottery. As you know each number is divisible by 1. So Mr. Nondo will never select 1 as one of those M numbers. Now given N, M and M random numbers, you have to find out the number of tickets which will be considered for the lottery.

Input

Each input set starts with two Integers N (10 ≤ N <2^31) and M (1 ≤ M ≤ 15). The next line will contain M positive integers each of which is not greater than N. Input is terminated by EOF.

Output

Just print in a line out of N tickets how many will be considered for the lottery.

Sample Input

10 2

2 3

20 2

2 4

Sample Output

3

10

时间: 2024-10-21 10:50:15

codeforses The Lottery(容斥定理)的相关文章

容斥定理 hdu2204 Eddy&#39;s爱好

传送门:点击打开链接 很明显会有大量重复的被计算,所以很容易就想到容斥定理. 我们设dp[i]表示能表示成M^i(i>1)且i是这个数字能表示出来的最大的情况时的总类数 比如,27拆成M^K时的K最大能表示成3,所以27这个数字分在dp[3]这一类 1我们暂时不考虑,不把它放在任何一类 因为K>1,所以K至少是2,最大是2^K=N的时候,所以K最大等于log2(N),所以K非常的小 首先,求出K最大大概的位置 然后开始求dp[i].求法如下: 首先,1~N中有哪些是能拆分成M^i的,利用pow

cf451E Devu and Flowers 卢卡斯定理+容斥定理

题目:http://codeforces.com/problemset/problem/451/E 题意:有n个盒子(n<=20),每个盒子中有10^12个小球,现从每个盒子中取出若干球(可为0),求共取出s个小球(s<=10^14)的方案数. 组合数学问题,求C(n,m).但n,m过大时,可用卢卡斯定理. 卢卡斯定理:C(n,m) %p = C(n/p,m/p) * C(n%p,m%p) 从n个盒子中取出s个球的方案数,相当于插板,即 C(s+n-1,n-1).注意这是没有限制条件的情况.

HDU1796 How many integers can you find【容斥定理】

题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1796 题目大意: 给你一个整数N.和M个整数的集合{A1.A2.-.Am}.集合内元素为非负数(包括零),求小于N的 正整数(1~N-1)中,能被M个整数的集合中随意一个元素整除的正整数个数. 比如N = 12.M = {2,3},在1~N-1中,能被2整除的数为{2,4,6.8.10},能被3整除的数为 {3.6,9}.则所求集合为{2,3,4.6,8,9,10},共7个,则答案为7. 思路:

HDU 1796 How many integers can you find (容斥定理 + 二进制)

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5493    Accepted Submission(s): 1567 Problem Description Now you get a number N, and a M-integers set, you should

HDU 1695 GCD(容斥定理)

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7529    Accepted Submission(s): 2773 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y

GCD SUM 强大的数论,容斥定理

GCD SUM Time Limit: 8000/4000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description 给出N,M执行如下程序:long long  ans = 0,ansx = 0,ansy = 0;for(int i = 1; i <= N; i ++)   for(int j = 1; j <= M; j ++)       if(gcd(i,j)

HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)

When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortunately fall into the sea. While it was dying, seven dolphins arched its body and sent it back to the shore. It is said that ?? used to surrounded by 7

容斥定理,皮克公式

(来源:哈工大算法培训) 容斥定理:在计算集合的并集的时候,我们经常需要减去重复的部分.但是怎么减比较麻烦.容斥定理就是解决这类问题. 内容: 比如: 皮克公式: 1.如何求多边形面积: 例: 思路:按顺序两点求叉积. S=abs(1/2*((x1*y2-x2*y1)+.......+(xk*yk+1-xk+1*yk)+.........+(xn*y1-x1*yn))). 由于计算的时候可能因为顺逆时针的不同而产生正负.所以加上一个abs. 2.求多边形边上的整数点. 3. 适用于格点图. 原文

题解报告:hdu 4135 Co-prime(容斥定理入门)

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

poj1091跳蚤(容斥定理)

题目:Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长.节目主持人会给该跳蚤发一张卡片.卡片上写有N+1个自然数.其中最后一个是M,而前N个数都不超过M,卡片上允许有相同的数字.跳蚤每次可以从卡片上任意选择一个自然数S,然后向左,或向右跳S个单位长度.而他最终的任务是跳到距离他左边一个单位长度的地方,并捡起位于那里的礼物.比如当N=2,M=18时,持有卡片(10, 15, 18)的跳蚤,就可以完成任务:他可以先向左跳10