ZOJ 2836 Number Puzzle 题解

题面

 

 

 lcm(x,y)=xy/gcd(x,y)
 lcm(x1,x2,···,xn)=lcm(lcm(x1,x2,···,xn-1),xn)

#include <bits/stdc++.h>
using namespace std;
long long n,m;
long long a[1010];
long long gcd(long long a,long long b)
{
    if(!b){
        return a;
    }
    return gcd(b,a%b);
}
long long lcm(long long a,long long b)
{
    return a*b/gcd(a,b);
}
int vis[1010];
long long ans=0;
long long pre[110];
void dfs(register int dep,long long goal,long long now)
{
    if(dep>goal){
        if(goal%2==0){
            ans-=(m/now);
        }
        else{
            ans+=(m/now);
        }
        return;
    }
    for(register int i=pre[dep-1]+1;i<=n;i++){
        if(!vis[i]){
            pre[dep]=i;
            vis[i]=1;
            dfs(dep+1,goal,lcm(now,a[i]));
            vis[i]=0;
        }
    }
}
int main()
{
    freopen("div.in","r",stdin);
    freopen("div.out","w",stdout);
    cin>>n>>m;    
    for(register int i=1;i<=n;i++){
        cin>>a[i];
        ans+=(m/a[i]);
    }
    for(register int k=2;k<=n;k++){    
        dfs(1,k,1);
    }
    cout<<ans<<endl;
}

 

posted @ 2019-08-31 13:35  神之右大臣  阅读(298)  评论(0编辑  收藏  举报