Contest1194

来源:互联网 发布:江彬和钱宁 知乎 编辑:程序博客网 时间:2024/05/17 08:19

1776: Press the switch

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 328  Solved: 82
[Submit][Status][Web Board]

Description

达达的家里有一串长度为n的灯泡,编号1,2,3,4.....n-1,n。每一个灯泡都有一个开关,达达每次选一个数a,把编号为a的倍数的灯泡的开关都按一遍。
假定灯刚开始都开着,他做了m次这样的事,问他爸妈打他没有?偶,不不不,问最后有几个灯开着。

Input

多组测试数据,第一行输入一个n(1<=n<=1e12)

第二行数输入一个数m(0<=m<=2)

接下来输入m个数ai(1<=ai<=min(1000,n))

Output

输出最后结果

Sample Input

100
1
2
5
2
2 3
100000000000
1
1

Sample Output

50
2
0

AC代码:
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<cmath>
int gcd(int a,int b)
{
        while(b)
        {
                int temp=a%b;
                a=b;
                b=temp;
        }
        return a;
}
using namespace std;;
int main()
{
        long long n;
        while(scanf("%lld",&n)!=EOF)
        {
                int m;
                scanf("%d",&m);
                int x1,x2;
                if(m==0)
                        printf("%lld\n",n);
                else if(m==1)
                {
                        scanf("%d",&x1);
                        printf("%lld\n",n-n/x1);
                }
                else if(m==2)
                {
                        scanf("%d%d",&x1,&x2);
                        //int x=gcd(x1,x2);
                        //printf("%d\n",x);
                        long long lcm=(x1*x2)/gcd(x1,x2);
                        n=n-(n/x1+n/x2)+n/lcm*2;
                        printf("%lld\n",n);
                }
        }
        return 0;
}