Xtreme8.0 - Play with GCD dp
Play with GCD
题目连接:
https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/play-with-gcd
Description
Minka is very smart kid who recently started learning computer programming.
He learned how to calculate the Greatest Common Divisor (GCD) of given numbers. The GCD http://en.wikipedia.org/wiki/Greatest_common_divisor of k numbers say [n1,n2,n3… nk] is the largest positive integer that divides all these numbers without any remainder. You may find out more about the GCD and the way it is calculated on the Wikipedia website.
Minka has N (1 <= N <= 10^5) balls and there is a number V (1 <= V <= 10^4) written on every ball. Now Minka has to perform Q queries, and in each query he wants to know the number of possible ways he can choose balls out of the N balls, so that GCD of the numbers written on the chosen balls equals to the number X of each query. Although he already knows the answer for each query, he would still like you to check if you can also find answer to his queries.
Since number of ways can be very large, your program should output the number of ways modulus 10^9+7.
Notes:
- There can be at most 100 distinct numbers written on N balls.
- By definition, the GCD is only defined for 2 or more numbers. For this problem, however, we will consider that the GCD of a single number may also defined and in such case the GCD of a single number will be equal to the number itself (i.e. the GCD of 2 is 2. Please refer to the explanation of Sample Input 1 for more details).
Input
The first line of each test file contains an integer N (1 <= N <= 10^5) denoting the number of balls.
The next line contains N space separated integer numbers, each one representing the number written on each of the N balls. The ith number (Vi) corresponds to the number written on the ith ball (1 <= Vi <= 10^4).
The third line contains an integer Q (1 <= Q <= 10^4) representing the number of GCD queries that will have to be performed.
Finally, Q lines follow, each one containing an integer X (1 <= X <= 10^4) corresponding to the GCD of each query.
Output
Your program should output the number of ways modulus 10^9+7 that balls can be drawn from the set, so that their GCD equals the number X corresponding to each query.
Note: There is a newline character at the end of the last line of the output.
Sample Input
5
2 3 5 6 6
2
2
5
Sample Output
4
1
Hint
We have 5 balls in the set, labeled with numbers [2, 3, 5, 6, 6] respectively. For the first query (X=2), there are in total 4 (distinct) ways by which we may choose balls so that their GCD equals 2, meaning:
a) {1, 4} (i.e. ball 1 labeled with number 2 and ball 4 labeled with number 6)
b) {1, 5} (i.e. ball 1 labeled with number 2 and ball 5 labeled with number 6)
c) {1, 4, 5} (i.e. ball 1 labeled with number 2, ball 4 labeled with number 6 and ball 5 labeled with number 6)
d) {1} (i.e. ball 1 labeled with number 2 since according to our definition of GCD, the GCD of 2 would equal 2)
Regarding the second query (X=5), there is only one way to choose balls so that their GCD equals 5, which is to choose only ball 3 (labeled with number 5).
题意
给你n个数,问你里面有多少个集合的gcd为x。
最多有100个不同的数。
题解
离散化之后乱dp一波就好了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const int mod = 1e9+7;
int p[maxn],dp[10005],n;
int add(int x,int y){
x+=y;
if(x>=mod)x-=mod;
return x;
}
int gcd(int x,int y)
{
if(y==0)return x;
return gcd(y,x%y);
}
vector<int>V;
map<int,int> H;
int a[maxn];
long long two[maxn];
int main()
{
scanf("%d",&n);
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
scanf("%d",&p[i]);
V.push_back(p[i]);
}
sort(V.begin(),V.end());
V.erase(unique(V.begin(),V.end()),V.end());
for(int i=0;i<V.size();i++)
H[V[i]]=i;
for(int i=1;i<=n;i++)
a[H[p[i]]]++;
two[0]=1;
for(int i=1;i<=n;i++)
two[i]=two[i-1]*2LL%mod;
for(int i=0;i<V.size();i++)
{
for(int j=1;j<=10000;j++)
dp[gcd(j,V[i])]=add(dp[gcd(j,V[i])],1LL*(two[a[i]]-1)*dp[j]%mod);
dp[V[i]]=add(dp[V[i]],(two[a[i]]-1));
}
int m;scanf("%d",&m);
for(int i=1;i<=m;i++)
{
int x;
scanf("%d",&x);
printf("%d\n",dp[x]);
}
}
Xtreme8.0 - Play with GCD dp的更多相关文章
- Xtreme8.0 - Kabloom dp
Xtreme8.0 - Kabloom 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/kablo ...
- Xtreme8.0 - Kabloom 动态规划
Xtreme8.0 - Kabloom 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/kablo ...
- Xtreme8.0 - Magic Square 水题
Xtreme8.0 - Magic Square 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/ ...
- LightOJ - 1140 统计0的数位 数位DP
注意以下几点: 搜索维度非约束条件的都要记录,否则大概率出错,比如_0 st参数传递和_0的互相影响要分辨清楚 num==-1就要返回0而不是1 #include<iostream> #i ...
- [原]携程预选赛A题-聪明的猴子-GCD+DP
题目: 聪明的猴子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- AIM Tech Round (Div. 2) D. Array GCD dp
D. Array GCD 题目连接: http://codeforces.com/contest/624/problem/D Description You are given array ai of ...
- HDU 5656 CA Loves GCD dp
CA Loves GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5656 Description CA is a fine comrade w ...
- D - 稳住GCD DP
http://acm.uestc.edu.cn/#/problem/show/923 给定一堆数字,求其所有数字的gcd. 现在要删除最多的数字,使得剩下的数字的gcd和原来的一样. 设dp[i][v ...
- UESTC 923 稳住GCD DP + GCD
定义:dp[i][j] 表示 在前i个数中,使整个gcd值为j时最少取的数个数. 则有方程: gg = gcd(a[i],j) gg == j : 添加这个数gcd不变,不添加, dp[i][j] ...
随机推荐
- Sublime Text 之运行 js 方法[2015-5-6更新mac下执行js]
昨天说完<Sublime Text 2 绿化与汉化 [Windows篇]>,今天我们来说说怎么用st直接运行 js 吧.群里的小伙伴一直对我的 ST 能直接运行js感到非常好奇,今天我就公 ...
- 如何使用 grunt
1>. 首先要有nodejs环境, 至少0.8.0版本: 2>. 转到 项目文件夹下: >npm install –g grunt-cli >npm init #生成一个基本的 ...
- HDU 1262 寻找素数对 模拟题
题目描述:输入一个偶数,判断这个偶数可以由哪两个差值最小的素数相加,输出这两个素数. 题目分析:模拟题,注意的是为了提高效率,在逐个进行判断时,只要从2判断到n/2就可以了,并且最好用打表法判断素数. ...
- 字符串格式化(百分号&format)
字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号方式: %[(name)][flags][width].[precision]typecode [ ]:表示 ...
- Python_oldboy_自动化运维之路_面向对象2(十)
本节内容: 面向对象程序设计的由来 什么是面向对象的程序设计及为什么要有它 类和对象 继承与派生 多的态与多态性 封装 静态方法和类方法 面向对象的软件开发 反射 类的特殊成员方法 异常处理 1.面向 ...
- tensorflow-训练(train)/测试(test)
一个TFRecords 文件为一个字符串序列.这种格式并非随机获取,它比较适合大规模的数据流,而不太适合需要快速分区或其他非序列获取方式. 操作组 操作 Training Optimizers,Gra ...
- EntityFrameWork 图解
- 2.选择元素 - 自定义过滤器《jquery实战》
2.5.6 自定义过滤器 jQuery 中有两种方法创建自定义的过滤器.第一种比较简单,但是不鼓励,从 jQuery 1.8 开始已经被第二种方法取代.记住,使用新方法时,你自定义的过滤器在 jQue ...
- MVC:分页改进URL
http://localhost/?page=2 可以根据"可组合URL"创建一种更具吸引力的URL方案: http://localhost/page2 publi ...
- Action的模型绑定
- 你真的会用Action的模型绑定吗? 在QQ群或者一些程序的交流平台,经常会有人问:我怎么传一个数组在Action中接收.我传的数组为什么Action的model中接收不到.或者我在ajax的 ...