zoukankan      html  css  js  c++  java
  • codeforces 630KIndivisibility(容斥原理)

    K. Indivisibility
    time limit per test
    0.5 seconds
    memory limit per test
    64 megabytes
    input
    standard input
    output
    standard output

    IT City company developing computer games decided to upgrade its way to reward its employees. Now it looks the following way. After a new game release users start buying it actively, and the company tracks the number of sales with precision to each transaction. Every time when the next number of sales is not divisible by any number from 2 to 10 every developer of this game gets a small bonus.

    A game designer Petya knows that the company is just about to release a new game that was partly developed by him. On the basis of his experience he predicts that n people will buy the game during the first month. Now Petya wants to determine how many times he will get the bonus. Help him to know it.

    Input

    The only line of the input contains one integer n (1 ≤ n ≤ 1018) — the prediction on the number of people who will buy the game.

    Output

    Output one integer showing how many numbers from 1 to n are not divisible by any number from 2 to 10.

    Examples
    input
    12
    output
    2

    题意:求出1到n中不能被2到10中任意一个数整除的数的个数
    题解:因为4 6 8 10都是2的倍数如果不能被2整除则一定不能被这四个数整除同理3和9,则可以推出只要不能被2 3 5 7中任一个数整除即可,这时我们发现答案就是1到n之间素数的个数,如果直接遍历求素数的话一定超时,我们可以用总数减去合数的个数,所有的合数分别为2 3 5 7 的倍数,但是我们又发现这些倍数中有重复例如6既是2的倍数又是3的倍数,直接减的话肯定减多,这时就用上了容斥原理 用2 3 5 7 倍数的和减去两两之间重复的,再加上任意三个的交集(因为减去两两交集时多减了三个之间重复的)再减去四个相交的
    #include<stdio.h>
    #include<string.h>
    #include<string>
    #include<math.h>
    #include<algorithm>
    #define LL long long
    #define PI atan(1.0)*4
    #define DD double
    #define MAX 100100
    #define mod 100
    #define dian 1.000000011
    #define INF 0x3f3f3f
    using namespace std;
    int main()
    {
    	LL n,m;
    	while(scanf("%lld",&n)!=EOF)
    	{
    		
    		m=n/2+n/3+n/5+n/7-n/6-n/10-n/14-n/15-n/21-n/35+n/30+n/70+n/42+n/105-n/210;
    		m=n-m;
    		printf("%lld
    ",m);
    	}
    	return 0;
    } 
    

      

  • 相关阅读:
    vim对光标所在的数字进行增减
    fedora找开ftpd服务器并以root登陆
    wxwidget自定义消息处理步骤
    c++的检测的确比C++更严格
    php常用字符串操作函数
    如何判断一个数组是一维数组或者是二维数组?用什么函数?
    php 面试 题汇总
    php 数组 常用函数
    php 会话控制
    用tp实现中文验证码
  • 原文地址:https://www.cnblogs.com/tonghao/p/5241427.html
Copyright © 2011-2022 走看看