zoukankan      html  css  js  c++  java
  • hdu 1796 How many integers can you find 容斥原理

    将不同因子组合求区间内合数个数比较有技巧

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;
    
    int n,cnt,a[20],ans;
    int gcd(int a,int b)
    {
        return b==0?a:gcd(b,a%b);
    }
    
    void dfs(int cur,int lcm,int num)
    {
        lcm=a[cur]/gcd(a[cur],lcm)*lcm;
        if(num%2==1)
            ans+=(n-1)/lcm;
        else
            ans-=(n-1)/lcm;
        for(int i=cur+1;i<cnt;i++)
            dfs(i,lcm,num+1);
    }
    int main()
    {
        int m,i,t;
        while(~scanf("%d%d",&n,&m))
        {
            for(i=0,cnt=0;i<m;i++)
            {
                scanf("%d",&t);
                if(t) a[cnt++]=t;
            }
            ans=0;
            for(i=0;i<cnt;i++)
                dfs(i,a[i],1);
            printf("%d
    ",ans);
        }
        return 0;
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

  • 相关阅读:
    Kvm virsh
    lvs tunl
    django表单使用
    django上传图片
    django框架admin使用
    django模板的使用
    django数据库操作
    django数据库交互
    django数据库中
    django之类视图
  • 原文地址:https://www.cnblogs.com/xryz/p/4847816.html
Copyright © 2011-2022 走看看