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)

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

分析:容斥原理,注意long long;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+10;
using namespace std;
inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
int n,m,k,t,fac[20],all;
int main()
{
    int i,j;
    while(~scanf("%d%d",&m,&n))
    {
        --m;
        all=0;
        rep(i,0,n-1)
        {
            scanf("%d",&j);
            if(j)fac[all++]=j;
        }
        ll ret=0;
        rep(i,1,(1<<all)-1)
        {
            ll now=1,cnt=0;
            rep(j,0,all-1)
            {
                if(i&(1<<j))
                {
                    cnt++;
                    now=now*fac[j]/gcd(now,fac[j]);
                }
            }
            if(cnt&1)ret+=m/now;
            else ret-=m/now;
        }
        printf("%lld\n",ret);
    }
    return 0;
}
时间: 2025-01-03 08:31:53

How many integers can you find的相关文章

TypeError: string indices must be integers, not str

1. TypeError: string indices must be integers, not str 字符串类型取第index个字符的时候,应该传入int而不是str.如 view source print? 1 a='abcdef' 2 print a[0] 3 #而不是 print a['0'] 更常见的情况是把一个string当成了字典在使用 :should_be_dict_but_string['id'] .这样的错误

[TypeScript] Create random integers in a given range

Learn how to create random integers using JavaScript / TypeScript. /** * Returns a random int between * @param start inclusive * @param before exclusive */ export function randomInt(start: number, before: number) { return start + Math.floor(Math.rand

HDU 1796How many integers can you find(容斥原理)

How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1796 Description Now you get a number N, and a M-integers set, you should find out how many integers which are smal

29. Divide Two Integers

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. (1) log int divide(int dividend, int divisor) { if(dividend == 0) return 0; if(divisor == 0) return INT_MAX; double t1 = log(fabs(dividend

E - A Simple Problem with Integers

#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; #define N 100002 struct node { int l,r; long long lz,w; }q[4*N]; void pushup(int rt) { q[rt].w=q[rt*2].w+q[rt*2+1].w; } void pushdo

371. Sum of Two Integers

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example: Given a = 1 and b = 2, return 3. Credits: Special thanks to @fujiaozhu for adding this problem and creating all test cases. 这道题让我们实现两数相加,但是不能用加号或

POJ 3468 A Simple Problem with Integers

链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 77302 Accepted: 23788 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two ki

poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 83959   Accepted: 25989 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. Yo

A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 99895   Accepted: 31162 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given

poj3468 A Simple Problem with Integers 线段树区间更新

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97722   Accepted: 30543 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of