Problem F: 我是好人4

Description

众所周知,我是好人!所以不会出太难的题,题意很简单

给你n个数,问你1000000000(含1e9)以内有多少个正整数不是这n个数任意一个的倍数

最后友情提供解题代码(我真是太好人了)

void solve(int p[], int n)

{

int ans = 0;

for (int i = 1; i <= 1e9; i++)

{

int fl = 0;

for (int j = 0; j < n; j++)

{

if (i % p[j] == 0)

{

fl = 1;

break;

}

}

if (fl == 0)ans++;

}

printf("%d\n", ans);

}

Input

第1行是一个整数T,表示共T组数据。 接下来是T组数据,每组数据第1行是正整数n(n<=50),接下来是n个正整数(小于等于1000),任意两数用1个空格隔开,最前数前面与最后数后面无空格

Output

输出T行,对应T组数据。(T<=10) 每行输出这样的正整数有多少个

Sample Input

3 4 2 3 5 7 1 2 13 854 101 143 282 538 922 946 286 681 977 892 656 907

Sample Output

228571428 500000000 968701719

HINT

提示:数据是随机生成的,尔等尽可随意乱搞

思路:根据容斥原理;偶数就减,奇数就加;

   dfs需要优化:lcm>1e9;return;

   把0删掉,如果有整除的删掉;

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
ll a[];
ll ans;
int y,len;
ll maxx=1e9;
ll gcd(ll x,ll y)
{
if(x%y==)
return y;
else
return gcd(y,x%y);
}
void dfs(ll lcm,int pos,int step)
{
if(lcm>maxx)
return;
if(pos==len)
{
if(step==)
return;
if(step&)
ans+=(maxx/lcm);
else
ans-=(maxx/lcm);
return;
}
dfs(lcm*a[pos]/gcd(lcm,a[pos]),pos+,step+);
dfs(lcm,pos+,step);
}
int main()
{
int x,i;
scanf("%d",&x);
while(x--)
{
scanf("%d",&y);
int lenn=;
for(i=;i<y;i++)
{
scanf("%lld",&a[i]);
if(a[i])
a[lenn++]=a[i];
}
sort(a,a+lenn);
len=;
for(int i=;i<lenn;i++) {
int f = ;
for(int j=;j<len;j++) {
if(a[i]%a[j]==) f=;
}
if(!f) a[len++] = a[i];
}
ans=;
dfs(,,);
printf("%lld\n",maxx-ans);
}
return ;
}

广州工业大学2016校赛 F 我是好人4 dfs+容斥的更多相关文章

  1. 广东工业大学2016校赛决赛-网络赛 1174 Problem F 我是好人4 容斥

    Problem F: 我是好人4 Description 众所周知,我是好人!所以不会出太难的题,题意很简单 给你n个数,问你1000000000(含1e9)以内有多少个正整数不是这n个数任意一个的倍 ...

  2. ACM学习历程—广东工业大学2016校赛决赛-网络赛F 我是好人4(数论)

    题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=5 这个题目一看就是一道数论题,应该考虑使用容斥原理,这里对lcm进行容斥. ...

  3. 广东工业大学2016校赛决赛-网络赛 1169 Problem A: Krito的讨伐 优先队列

    Problem A: Krito的讨伐 Description Krito终于干掉了99层的boss,来到了第100层.第100层可以表示成一颗树,这棵树有n个节点(编号从0到n-1),树上每一个节点 ...

  4. ACM学习历程—广东工业大学2016校赛决赛-网络赛E 积木积水(最值问题 || 动态规划)

    题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=4 这个题目自然会考虑到去讨论最长或者最短的板子. 笔上大概模拟一下的话,就 ...

  5. ACM学习历程—广东工业大学2016校赛决赛-网络赛D 二叉树的中序遍历(数据结构)

    题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=3 这算是一个胡搞类型的题目.当然肯定是有其数据结构支撑的. 唯一的限制就是 ...

  6. ACM学习历程—广东工业大学2016校赛决赛-网络赛C wintermelon的魔界寻路之旅(最短路 && 递推)

    题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=2 题目由于要找对称的路径,那么狠明显可以把右下角的每一块加到左上角对应的每 ...

  7. 广东工业大学2016校赛决赛重现——E积木积水(方法据说很多)

    Problem E: 积木积水 Description 现有一堆边长为1的已经放置好的积木,小明(对的,你没看错,的确是陪伴我们成长的那个小明)想知道当下雨天来时会有多少积水.小明又是如此地喜欢二次元 ...

  8. ACM-ICPC 2015 沈阳赛区现场赛 F. Frogs && HDU 5514(容斥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5514 题意:有m个石子围成一圈, 有n只青蛙从跳石子, 都从0号石子开始, 每只能越过xi个石子.问所 ...

  9. 校赛F

    问题描述 例如对于数列[1 2 3 4 5 6],排序后变为[6 1 5 2 4 3].换句话说,对于一个有序递增的序列a1, a2, a3, ……, an,排序后为an, a1, an-1, a2, ...

随机推荐

  1. Scala中的数组和集合操作

    package test /* 1.在scala集合中,Iterable是共同的Trait,Iterable要求继承者实现一些共同的方法,例如元素的遍历 * 2.Array是scala基础的数据结构, ...

  2. Summary: rand5构造rand7

    给一个方法,比如 rand5(), 它能够等概率生成 1-5 之间的整数. 所谓等概率就是1,2,3,4,5 生产的概率均为 0.2 .现在利用rand5(), 构造一个能够等概率生成 1- 7 的方 ...

  3. Twitter OA prepare: Rational Sum

    In mathematics, a rational number is any number that can be expressed in the form of a fraction p/q ...

  4. Summary: Difference between null and empty String

    String s1 = ""; means that the empty String is assigned to s1. In this case, s1.length() i ...

  5. mysql进程文件

  6. cxf-webservice完整示例

    最近一段时间研究webservice,一般来说,开发java的Webservice经常使用axis2和cxf这两个比较流行的框架 先使用cxf,开发一个完整示例,方便对webservice有一个整体的 ...

  7. Detour的简单使用

    Detours的安装:下载部分:1.直接在百度搜"detour",进对应的网站下载.2.或以下链接https://www.microsoft.com/en-us/research/ ...

  8. python print 使用分隔符 或行尾符

    使用print() 函数输出数据,但是想改变默认的分隔符或者行尾符. >>> print('ACME', 50, 91.5) ACME 50 91.5 >>> pr ...

  9. python xml练习:从database.xml文件取databaselist的ip、name、passwd,写入列表

    xml: <?xml version='1.1' encoding='utf-8'?><!--this is a test about xml--><databaseli ...

  10. web前端----JavaScript的BOM

    一.引入 到目前为止,我们已经学过了JavaScript的一些简单的语法.但是这些简单的语法,并没有和浏览器有任何交互. 也就是我们还不能制作一些我们经常看到的网页的一些交互,我们需要继续学习BOM和 ...