[Rejection principle] HDU 1796 how many integers can you find

Source: Internet
Author: User

Question:

Give N and then the number of M. Question 1 ~ The number of N-1 can be divided by one or more of the M number.

Ideas:

First n --

Then, for each number of M actually 1 ~ N-1 can be divided by its internal is (N-1)/m [I]

But there will be duplicates. For example, in Example 6, the calculation will be repeated.

At this time, we need to refresh the principle.

Add the first number minus two numbers ..

Here we need to note that when two or more numbers are used, we need to calculate LCM instead of simply multiplying them!

Code:

#include "stdio.h"#include "string.h"#include "math.h"#include "iostream"#include "cstdlib"#include "algorithm"#include "queue"using namespace std;int a[12];int used[12],b[12];int n,m;int gcd(int a,int b){    return b==0?a:gcd(b,a%b);}int lcm(int k){    int ans=b[0];    for(int i=1;i<k;i++)    {        int tep=gcd(ans,b[i]);        ans=ans/tep*b[i];    }    return ans;}__int64 dfs(int kk,int x,int lit){    __int64 ans=0;    if(x==lit)    {        int tep;        tep=lcm(x);        return n/tep;    }    for(int i=kk+1;i<m;i++)    {        if(a[i]==0) continue;        if(used[i]) continue;        used[i]=1;        b[x]=a[i];        ans+=dfs(i,x+1,lit);        used[i]=0;    }    return ans;}int main(){    while(scanf("%d%d",&n,&m)!=-1)    {        n--;        for(int i=0;i<m;i++) scanf("%d",&a[i]);        __int64 ans=0;        for(int i=1;i<=m;i++)        {           // printf("%d\n",dfs(-1,0,i));            memset(used,0,sizeof(used));            if(i%2==0) ans-=dfs(-1,0,i);            else ans+=dfs(-1,0,i);        }        printf("%I64d\n",ans);    }    return 0;}



[Rejection principle] HDU 1796 how many integers can you find

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.