UVa10325 - The Lottery(容斥原理)

来源:互联网 发布:锦尚中国源码 编辑:程序博客网 时间:2024/05/29 11:44

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 22 320 22 4

Sample Output

310

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();}}}



0 0