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. Word转换为markdown

    Word转换为markdown 首先你的电脑要有office word 1   安装pandoc https://github.com/jgm/pandoc/releases,可以找到最新的pando ...

  2. kernel下nand flash的文件系统总结

    1.FLASH转换层(FTL) EXt2/EXT3/EXT4文件系统可以通过FTL实现对flash的支持,因为FTL可以将闪存flash模拟成磁盘结构. 在ext2文件系统的基础上上,为了保证数据的一 ...

  3. lua的文件管理

    lua没有自己的文件管理 只有读取和写入文件,但是可以通过调用lfs(LuaFileSystem),lfs是一个 用于lua进行文件访问的库,支持lua5.1和lua5.2,并且跨平台 lfs的使用: ...

  4. CentOS忘记普通用户密码解决办法

    普通用户忘记密码 1.使用root用户登录系统,找到/etc/shadow文件. 2.找到用户名开头的那一行,例如我的用户名为pds,,以冒号为分割符,红色部分是密码加密部分 pds:$1$Civop ...

  5. PAT 1026 Table Tennis[比较难]

    1026 Table Tennis (30)(30 分) A table tennis club has N tables available to the public. The tables ar ...

  6. angular $scope.$watch

    在$scope内置的所有函数中,用得最多的可能就是$watch 函数了.当你的数据模型中某一部分发生变化时,$watch函数可以向你发出通知. 你可以监控单个对象的属性,也可以监控需要经过计算的结果( ...

  7. isKindOfClass isMemeberOfClass 的区分

    isKindOfClass If you use such constructs in your code, you might think it is alright to modify an ob ...

  8. Summary: Java Inheritance

    In this tutorial we will discuss about the inheritance in Java. The most fundamental element of Java ...

  9. Leetcode: Construct Binary Tree from Preorder and Inorder Transversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...

  10. zw版【转发·台湾nvp系列Delphi例程】HALCON TestRegionPoint1

    zw版[转发·台湾nvp系列Delphi例程]HALCON TestRegionPoint1 unit Unit1;interfaceuses Windows, Messages, SysUtils, ...