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. ZooKeeper编程指导

    简介 对于想要利用ZooKeeper的协调服务来创建一个分布式应用的开发人员来说,这篇文章提供了指导.包含了一些概念和实际性操作的信息. 这篇文章的前四个章节介绍了各种ZooKeeper的概念,这对理 ...

  2. UIView添加支持代码块的手势

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(a ...

  3. SQL 使用经验

    1. 写存储过程,Update数据库表,一定要根据idRow,也就是主键唯一键来更新. 更新操作,如果根据其他条件更新,之后就有的忙活了. 2. Query语句要加NOLOCK

  4. 算法导论——lec 11 动态规划及应用

    和分治法一样,动态规划也是通过组合子问题的解而解决整个问题的.分治法是指将问题划分为一个一个独立的子问题,递归地求解各个子问题然后合并子问题的解而得到原问题的解.与此不同,动态规划适用于子问题不是相互 ...

  5. jquery,js常用特效名称

  6. “文件XXX正由另一进程使用,因此该进程无法访问此文件”

    文件xxx正在由另一进城使用,这种问题出现有一种原因: 就是同一个线程重打开文件,但是没有关闭的情况下,再次读取的时候抛出异常. 如下的代码为错误代码:

  7. 无法显示TabHost的setIndicator设置的图片的问题解决办法

    本想做一个 带有图片的 分页显示的demo 但是 自己的代码写的也木有问题 ,所以只有mainifest 有问题了 主要是用到了  Tabhost <TabHost xmlns:android= ...

  8. Objective-C 字典、可变字典

    字典相当于c++ stl中的map 字典NSDictionary #import <UIKit/UIKit.h> #import "AppDelegate.h" int ...

  9. ubuntu终端命令

    整个电脑都划成ubuntu用. 装软件时的一个明显感觉就是很多事情,用终端的命令行去做很容易,用图形界面往往很复杂,而且很多时候还会出现权限的问题,对于ubuntu的用户权限,现在的唯一感觉就是权限在 ...

  10. C/C++中的空类及抽象类大小

    代码: #include <iostream> using namespace std; struct A{ }; struct B{ int i; }; class C:B{ ; }; ...