[ZOJ 2836] Number Puzzle

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

The input contains several test cases.

For each test case, there are two lines. The first line contains N (1 <= N <= 10) and M (1 <= M <= 200000000), and the second line contains A1, A2, ..., An(1 <= Ai <= 10, for i = 1, 2, ..., N).

Output

For each test case in the input, output the result in a single line.

Sample Input

3 2 2 3 7 3 6 2 3 7

Sample Output

1 4

入门容斥原理,见代码

复制代码
#include <iostream>
#include <cstdio>
using namespace std;
#define ll long long
#define N 10

int n,m;
int a[N];
int gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
int lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}
////写法1/////
int solve()
{
    int res=0;
    for(int i=1;i<(1<<n);i++)
    {
        int cnt=0;
        ll LCM=1;
        for(int j=0;j<n;j++)
        {
            if(i&(1<<j)) cnt++,LCM=lcm(LCM,a[j]);
        }
        if(cnt&1) res+=m/LCM;
        else res-=m/LCM;
    }
    return res;
}
////写法2/////
int ans;
void dfs(int pos,int LCM,int cnt)
{
    if(cnt)
    {
        if(cnt&1) ans+=m/LCM;
        else ans-=m/LCM;
    }
    for(int i=pos+1;i<n;i++)
        dfs(i,lcm(LCM,a[i]),cnt+1);
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        //printf("%d\n",solve());
        ans=0;
        dfs(-1,1,0);
        printf("%d\n",ans);
    }
    return 0;
}
复制代码

 

posted @   哈特13  阅读(191)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
阅读排行:
· 从零开始开发一个 MCP Server!
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
点击右上角即可分享
微信分享提示