UVa10325 - The Lottery(容斥原理)

The Lottery 

The Sports Association of Bangladesh is in great problem with their latest lottery 'Jodi laiga Jai'. There areso many participants this time that they cannot manage all the numbers. In an urgentmeeting they have decided that they will ignore some numbers. But how theywill choose those unlucky numbers!! Mr. NondoDulal who is very interested about historic problems proposed a scheme to get free fromthis problem.

You may be interested to know how he has got this scheme. Recently he has read theJoseph's problem.

The Problem

There are N tickets which are numbered from 1 to N. Mr. Nondo will choose Mrandom numbers and then he will select those numbers which is divisibleby at least one of those M numbers. The numbers which are not divisible by anyof those M numbers will be considered for the lottery.

As you know each number is divisible by 1. So Mr. Nondo will never select1 as one of those M numbers. Now given N,M and M random numbers, you have tofind out the number of tickets which will be considered for the lottery.

The Input

Each input set starts with two Integers N (10<=N<2^31) and M (1<=M<=15).The next line will contain M positive integers each of which is not greater than N.Input is terminated by EOF.

The Output

Just print in a line out of N tickets how many will be considered for the lottery.

Sample Input

10 2
2 3
20 2
2 4

Sample Output

3
10
 
 
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

public class Main 
{
	public static final boolean DEBUG = false;
	public StreamTokenizer tokenizer;
	public BufferedReader cin;
	public PrintWriter cout;
	public long n, m;
	public long[] arr;
	
	public void init() 
	{
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(
						new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
			}
			cout = new PrintWriter(new OutputStreamWriter(System.out));
			tokenizer = new StreamTokenizer(cin);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String next()
	{
		try {
			tokenizer.nextToken();
			if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
			else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) return String.valueOf((int)tokenizer.nval);
			else if (tokenizer.ttype == StreamTokenizer.TT_WORD) return tokenizer.sval;
			else return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public long gcd(long a, long b)
	{
		return b == 0? a : gcd(b, a % b);
	}
	
	public long lcm(long a, long b)
	{
		return a * b / gcd(a, b);
	}
	
	public boolean input() 
	{
		String s = next();
		if (s == null) return false;
		
		n = Long.parseLong(s);
		m = Long.parseLong(next());
		
		arr = new long[(int)m];
		
		for (int i = 0; i < m; i++) {
			arr[i] = Integer.parseInt(next());
		}
		return true;
	}

	
	public void solve() 
	{
		long ans = 0;
		
		for (int i = 1; i < (1 << m); i++) {
			long mult = 1, cnt = 0;
			for (int j = 0; j < m; j++) {
				if ((i & (1 << j)) != 0) {
					mult = lcm(mult, arr[j]);
					if (mult > n) break;
					cnt++;
				}
			}
			
			if (mult > n) continue;
			if (cnt % 2 != 0) ans += n / mult;
			else ans -= n / mult;
		}
		
		cout.println(n - ans);
		cout.flush();
	}

	public static void main(String[] args) 
	{
		Main solver = new Main();
		solver.init();
		
		while (solver.input()) {
			solver.solve();
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值