POJ 1591 M*A*S*H

M*A*S*H
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 888 Accepted: 470

Description

Corporal Klinger is a member of the 4077th Mobile Army Surgical Hospital in the Korean War; and he will do just about anything to get out. The U.S. Army has made an offer for a lottery that will choose some number of lucky people (X) to return to the states for a recruiting tour. Klinger needs your help getting out. 
The lottery is run by lining up all the members of the unit at attention and eliminating members by counting off the members from 1 to N where N is a number chosen by pulling cards off of the top of a deck. Every time N is reached, that person falls out of the line, and counting begins again at 1 with the next person in line. When the end of the line has been reached (with whatever number that may be), the next card on the top of the deck will be taken, and counting starts again at 1 with the first person in the remaining line. The last X people in line get to go home. 

Klinger has found a way to trade a stacked deck with the real deck just before the selection process begins. However, he will not know how many people will show up for the selection until the last minute. Your job is to write a program that will use the deck Klinger supplies and the number of people in line that he counts just before the selection process begins and tell him what position(s) in the line to get in to assure himself of a trip home. You are assured that Klinger's deck will get the job done by the time the 20th card is used. 

A simple example with 10 people, 2 lucky spots, and the numbers from cards 3, 5, 4, 3, 2 would show that Klinger should get in positions 1 or 8 to go home. 

Input

For each selection, you will be given a line of 22 integers. The first integer (1 <= N <= 50) tells how many people will participate in the lottery. The second integer (1 <= X <= N) is how many lucky "home" positions will be selected. The next 20 integers are the values of the first 20 cards in the deck. Card values are interpretted to integer values between 1 and 11 inclusive. 

Output

For each input line, you are to print the message ``Selection #A" on a line by itself where A is the number of the selection starting with 1 at the top of the input file. The next line will contain a list of ``lucky" positions that Klinger should attempt to get into. The list of ``lucky" positions is then followed by a blank line. 

Sample Input

10 2 3 5 4 3 2 9 6 10 10 6 2 6 7 3 4 7 4 5 3 2
47 6 11 2 7 3 4 8 5 10 7 8 3 7 4 2 3 9 10 2 5 3

Sample Output

Selection #1 
1 8 

Selection #2 
1 3 16 23 31 47

Source


步长会变化的约瑟夫问题,,,

几天没刷题,简单的题写的也很蛋疼


#include <iostream>
#include <cstring>
using namespace std;

int i,j,n,m=0,x,a[20];
bool queue[51];

int main()
{
	while (cin>>n>>x){
		for (i=1;i<=20;++i)
			cin>>a[i];
		memset(queue,0,sizeof(queue));
		int flaga=1,flag=n;
		while (flag!=x){
			j=0;
			for (i=1;i<=n;++i){
				if (!queue[i]) j++;
				if (flag!=x && j==a[flaga]){
					j=0; flag--;
					queue[i]=true;
				}
			}
			flaga++;
		}
		cout<<"Selection #"<<++m<<endl;
		for (i=1;i<=n;++i)
			if (!queue[i]) cout<<i<<' '; 
		cout<<endl<<endl;
	}
	return 0;
} 

kdwycz的网站:  http://kdwycz.com/

kdwyz的刷题空间:http://blog.youkuaiyun.com/kdwycz


<think>嗯,用户想了解如何在C语言中实现容斥原理的应用,并且需要示例代码。首先,我需要回忆一下容斥原理的基本概念。容斥原理通常用于计算多个集合的并集大小,通过交替加减不同集合的交集来避免重复计算。这在组合数学和算法问题中很常见,比如计算满足某些条件的数的个数。 接下来,我得想想如何在C语言中具体实现这个原理。通常,容斥原理的实现会涉及到遍历所有可能的子集组合,然后根据子集的大小来决定是加还是减。例如,对于m个集合,需要遍历从1到2^m-1的所有子集,然后计算每个子集的交集大小,并根据子集元素的奇偶性来决定符号。 然后,用户提供的引用中有几个代码示例,比如引用[1]中的代码,看起来像是使用位运算来生成子集,并且通过递归的gcd函数来计算最小公倍数。这可能是一个应用容斥原理的例子,用于计算在n范围内能被多个数整除的数的数量。例如,计算1到n中能被数组l中的至少一个数整除的数的个数,这可以通过容斥原理来避免重复计算。 我需要将这些思路整理成一个清晰的步骤,解释如何用C语言实现容斥原理。首先,可能需要一个函数来计算最大公约数(gcd),然后计算最小公倍数(lcm),因为交集的大小通常涉及到lcm。例如,两个数的lcm是它们的乘积除以gcd。然后,遍历所有子集,对于每个子集,计算这些数的lcm,并根据子集的大小决定加减。 然后,考虑示例代码的结构。引用[1]中的代码似乎是这样做的:输入n和m,然后输入m个数,遍历所有可能的子集(通过位掩码k从0到2^m-1),计算每个子集的lcm,并根据子集的元素个数奇偶性来调整总和s的值。例如,如果子集有奇数个元素,就减去n/lcm,否则加上。这是因为容斥原理中,奇数次交集加,偶数次减,但这里可能是相反,需要仔细看代码逻辑。 此外,用户提到的引用[2]和[3]可能涉及其他应用场景或优化方法,比如使用前缀和或其他数据结构来简化计算。不过当前问题主要是容斥原理的实现,所以可能需要集中在位运算遍历子集和lcm的计算上。 在解释代码时,需要分步骤说明,比如如何生成子集,如何计算交集大小,如何处理符号。同时,需要给出具体的例子,比如计算1到n中能被至少一个数整除的个数,并解释代码的运行流程。 可能还需要提到代码中的关键点,比如如何用位掩码k来枚举子集,如何通过位与操作检查哪些元素被包含在当前子集中,如何维护当前子集的lcm,以及如何处理溢出问题(比如r可能会超过n,这时候可以提前终止)。 最后,应用场景部分需要举几个实际的例子,比如数学问题中的计数,或者几何问题中的区域计算,引用中的几何工具应用可能相关,比如poj题目中的几何问题可能用到容斥原理来计算覆盖区域。 总结来说,回答需要包括容斥原理的C语言实现步骤,示例代码的解释,以及应用场景的举例。同时,根据用户提供的引用,特别是引用[1]中的代码,可能需要将其作为示例进行详细说明,并引用相关部分。</think>### C语言中容斥原理的实现与应用 #### 一、容斥原理基础 容斥原理用于计算多个集合的并集大小,公式为: $$|A_1 \cup A_2 \cup \dots \cup A_m| = \sum |A_i| - \sum |A_i \cap A_j| + \sum |A_i \cap A_j \cap A_k| - \dots + (-1)^{m+1}|A_1 \cap \dots \cap A_m|$$ #### 二、实现步骤 1. **位运算枚举子集** 通过二进制位掩码遍历所有子集组合(共$2^m-1$种),例如: ```c for (int k = 1; k < (1 << m); k++) ``` 2. **计算集合交集** 使用最大公约数(GCD)和最小公倍数(LCM)处理交集: ```c int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } ``` 3. **符号控制** 根据子集元素个数的奇偶性决定加减符号: ```c if (__builtin_popcount(k) % 2) sum += n / current_lcm; else sum -= n / current_lcm; ``` #### 三、完整示例代码 以下代码实现**计算1~n中能被数组l中任意数整除的数的个数**: ```c #include <stdio.h> int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { int n, m, l[15]; while (scanf("%d%d", &n, &m) != EOF) { for (int i = 0; i < m; i++) scanf("%d", &l[i]); int result = 0; for (int k = 1; k < (1 << m); k++) { long long current_lcm = 1; int cnt = 0; for (int i = 0; i < m; i++) { if (k & (1 << i)) { current_lcm = current_lcm / gcd(current_lcm, l[i]) * l[i]; if (current_lcm > n) break; // 防止溢出 cnt++; } } if (cnt % 2) result += n / current_lcm; else result -= n / current_lcm; } printf("%d\n", result); } return 0; } ``` #### 四、关键代码解析 1. **位掩码遍历** `k`从1到$2^m-1$遍历所有非空子集,每位表示是否选中对应元素[^1]。 2. **动态计算LCM** 通过不断更新`current_lcm`处理多个数的LCM,同时防止数值溢出[^3]。 3. **奇偶性判断** `__builtin_popcount(k)`统计二进制中1的个数,决定加减符号[^2]。 #### 五、应用场景 1. **数学问题** 计算区间内满足多个条件的整数数量(如同时被2/3/5整除的数)。 2. **几何覆盖** 求多个几何图形覆盖区域的总面积(如圆、矩形的交集计算)[^4]。 3. **组合计数** 解决排列组合中的包含排除问题,如错位排列数的计算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值