Dividing Numbers

Time Limit: 9000/3000MS (Java/Others)     Memory Limit: 262144/262144KB (Java/Others)
Submit Status

Given an integer N (1≤N≤1013) and K (1≤K≤100) co-prime numbers P1,P2,...,Pk, which are less than 1000. Please tell me how many integers in range [1,N] satisfied that none of a number in P1,P2,...,Pk can divide it.

Input

The first line contains two integers N (1≤N≤1013) and K (1≤K≤100).

The second line contains K numbers P1,P2,...,Pk. It is guaranteed that 2≤Pi≤1000.

It is guaranteed that the given K numbers are pairwise co-prime.

Output

Output an integer representing the number of integers in range [1,N] satisfied the condition.

Sample input and output

Sample Input Sample Output
20 3
2 3 5
6
50 2
15 8
41

Source

2015 UESTC ACM Summer Training Team Selection (4)
 
结题报告:
注意到题目中的条件,两两互质,这是关键,即gcd( a , b ) = 1 , a, b ∈ array[p]
那么解决这道题的关键就在于重复数字的问题,例如 N = 105 ,  P = 3 , 5 , 那么15 , 30 , 45 .... 这几个数我们就必须保证只能删掉一次
 
我们令 F( i , j ) 表示范围[ 1 , i ] , 不能被P[0] , P[1] ..... P[j] 整除的数的个数
转移方程:
 F ( i , j ) = F( i , j - 1 ) - F( i / p[j] , j - 1 )
转移方程的解释:
我们仅仅考虑P【j】这个数,能够被整数它的肯定是(1,2,3.... i / P[j] ) ,这些数很可能在以后(往前转移)经被筛过了,所以我们需要减去
反正过来想:
【1,N】之间不能被P【0】 -> P【j】 整数的数的数量 = 【1,N】之间不能被P【0】 -> P【j-1】 整除的数 - 【1,N】之间P【j】的因子数目(同时保证这些因子数目不是前面P【0】 -> P【j-1】 任何一个数的因子)
接下来的问题就是搜索了
因为N的范围很大,所以我们在小范围内使用记忆化搜索,大范围上用dfs即可
 
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin) using namespace std;
const int maxn = 5e4; long long n;
int k , p[];
long long f[maxn][]; long long dfs(long long x,int y)
{
if (y == -) return x;
if (x < maxn)
{
if (f[x][y] != -) return f[x][y];
return f[x][y] = dfs(x,y-) - dfs(x/p[y],y-);
}
else
return dfs(x,y-) - dfs(x/p[y],y-);
} int main(int argc,char *argv[])
{
scanf("%lld%d",&n,&k);
memset(f,-,sizeof(f));
for(int i = ; i < k ; ++ i) scanf("%d",&p[i]);
sort(p,p+k);
printf("%lld\n",dfs(n,k-));
return ;
}

UESTC_Dividing Numbers CDOJ 1156的更多相关文章

  1. cdoj Dividing Numbers 乱搞记忆化搜索

    //真tm是乱搞 但是(乱搞的)思想很重要 解:大概就是记忆化搜索,但是原数据范围太大,不可能记下所有的情况的答案,于是我们就在记下小范围内的答案,当dfs落入这个记忆范围后,就不进一步搜索,直接返回 ...

  2. CDOJ 1272 Final Pan's prime numbers

    有些问题,不做实践与猜测,可能一辈子也想不出答案,例如这题. #include<stdio.h> #include<math.h> long long x; int main( ...

  3. ural 1156. Two Rounds

    1156. Two Rounds Time limit: 2.0 secondMemory limit: 64 MB There are two rounds in the Urals Champio ...

  4. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  5. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  6. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  7. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  8. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  9. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

随机推荐

  1. php面向对象编程之构造方法__construct()

    php中构造方法是对象创建完成后第一个被对象自动调用的方法.在每个类中都有一个构造方法,如果没有显示地声明它,那么类中都会默认存在一个没有参数且内容为空的构造方法. 构造方法的作用 通常构造方法被用来 ...

  2. C# 约瑟夫环算法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. Open Session In View

    Open Session In View模式的主要思想是:在用户的每一次请求过程始终保持一个Session对象打开着 实现步骤: 步骤一.创建一个Web项目,创建包cn.happy.util,创建Hi ...

  4. (转载)XML Tutorial for iOS: How To Choose The Best XML Parser for Your iPhone Project

    There are a lot of options when it comes to parsing XML on the iPhone. The iPhone SDK comes with two ...

  5. HDU 2025 查找最大元素

    查找最大元素 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  6. servlet下载,解决文件名中有中文下载路径出现乱码不能正常下载问题

    方法很多种,我只试用了两种. 主页面JSP中引入下载功能所需的js文件.引入的时候设置编码格式例如 <script type="text/javascript" charse ...

  7. 【hoj】1604 cable master

    简单,二分法,可是在oj上交的时候会有精度的限制,所以仅仅能把数扩得大一点,并且在扩的时候为防止尾数会自己主动生成一些非零数,所以还是自己吧扩到的位置设置为0,还有输出时由于%.2lf会自己有4设5入 ...

  8. git的0基础使用

    1.申请一个git帐号 2.项目开发者将你增加这个项目 3.在终端随意一个目录克隆 该项目地址 git clone 该项目地址 4.进nginx配置 5.更新的时候进入项目目录 git pull

  9. 实现简单的django上传文件

    本文用django实现上传文件并保存到指定路径下,没有使用forms和models,步骤如下: 1.在模板中使用form表单,因为这个表单使用于上传文件的,所以method属性必须设置为post,而且 ...

  10. My way on Linux - 知识梳理计划

    知识梳理计划图 近期计划把自己学习的工作中用到的Linux知识梳理下,敬请期待.