ZOJ 2836

求不比M大的可以被集合任一个数整除的数的个数。(容斥原理)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

int set[15];
int ans;

int gcd(int a,int b){
	if(b==0) return a;
	return gcd(b,a%b);
}

void dfs(int i,int num,int p,int m,int n){
	if(i>=n){
		if(num==0)
		ans=0;
		else if(num&1)
		ans+=(m/p);
		else ans-=(m/p);
		return;
	}
	dfs(i+1,num,p,m,n);
	dfs(i+1,num+1,p/gcd(p,set[i])*set[i],m,n);
}

int main(){
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF){
		for(int i=0;i<n;i++)
		scanf("%d",&set[i]);
		dfs(0,0,1,m,n);
		printf("%d\n",ans);
	}
	return 0;
}

  

时间: 2024-10-05 04:41:07

ZOJ 2836的相关文章

ZOJ 2836 Number Puzzle ( 容斥原理 )

ZOJ 2836 Number Puzzle( 容斥原理 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define CLR( a, b ) memset( a, b, sizeof(a) ) int m, n, A[11]; LL gcd( LL a, LL b ) { return b == 0 ? a :

[容斥原理] zoj 2836 Number Puzzle

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1836 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 th

ZOJ 2836 Number Puzzle

Number Puzzle Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 283664-bit integer IO format: %lld      Java class name: Main Given a list of integers ($A_1, A_2, ..., A_n$), and a positive integer M, please fi

ZOJ 2836 Number Puzzle(容斥原理啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1836 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 g

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n