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:

1. There can be at most 100 distinct numbers written on N balls.
2. 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.

5

2 3 5 6 6

2

2

5

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).

代码

``````#include<bits/stdc++.h>
using namespace std;

const int maxn = 100005;
const int mod = 1e9+7;
int p[maxn],dp[10005],n;
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++)
}
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的更多相关文章

1. Xtreme8.0 - Kabloom dp

Xtreme8.0 - Kabloom 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/kablo ...

2. Xtreme8.0 - Kabloom 动态规划

Xtreme8.0 - Kabloom 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/kablo ...

3. Xtreme8.0 - Magic Square 水题

Xtreme8.0 - Magic Square 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/ ...

4. LightOJ - 1140 统计0的数位 数位DP

注意以下几点: 搜索维度非约束条件的都要记录,否则大概率出错,比如_0 st参数传递和_0的互相影响要分辨清楚 num==-1就要返回0而不是1 #include<iostream> #i ...

5. [原]携程预选赛A题-聪明的猴子-GCD+DP

题目: 聪明的猴子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

6. 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 ...

7. 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 ...

8. D - 稳住GCD DP

http://acm.uestc.edu.cn/#/problem/show/923 给定一堆数字,求其所有数字的gcd. 现在要删除最多的数字,使得剩下的数字的gcd和原来的一样. 设dp[i][v ...

9. UESTC 923 稳住GCD DP + GCD

定义:dp[i][j] 表示 在前i个数中,使整个gcd值为j时最少取的数个数. 则有方程: gg = gcd(a[i],j) gg == j : 添加这个数gcd不变,不添加,  dp[i][j] ...

随机推荐

1. Java接口和抽象类的区别

今天看到项目中,写了一个抽象类,里面有很多方法继承了这类,当调用这个接口时,采用的是这个抽象类去调方法的,当时一想,这个不就是我们说的Java的多态的特征: 继承:存在继承关系的子类和父类 重写:子类 ...

2. 输入m乘法表

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

3. PHP获取POST数据的几种方法汇总

一.PHP获取POST数据的几种方法 方法1.最常见的方法是:\$_POST['fieldname']; 说明:只能接收Content-Type: application/x-www-form-urle ...

4. linux 命令chmod 和chown

chmod 命令 “chmod”命令就是改变文件的模式位.chmod会根据要求的模式来改变每个所给的文件,文件夹,脚本等等的文件模式(权限). 在文件(文件夹或者其它,为了简单起见,我们就使用文件)中 ...

5. [Tutorial] Using the RasPi as a WiFi hostspot

http://www.raspberrypi.org/forums/viewtopic.php?f=36&t=19120 http://wireless.kernel.org/en/users ...

6. OSChina底层数据库操作的类(QueryHelper)源代码

OSChina 使用的是 dbutils 这个JDBC的封装类库来进行数据库操作. 而 QueryHelper 则是在 dbutils 的基础上进行一级简单的封装,提供一些经常使用的数据库操作方法和对 ...

7. react重学

知识点一:react解析中 return {__html:rawMarkup}; 这里的html前边用的是双下划线(谢谢学妹的指点)

8. Linux入门搭建可视化桌面环境小合集virtual box centOS7.10

常用命令: 关联主机与虚拟机(方便文件共享): # sudo mount -t vboxsf share(主机文件夹名) /usr/share(虚拟机内自创) Linux shell进入root模式: ...

9. 【LeetCode每天一题】Remove Element（移除指定的元素）

Given an array nums and a value val, remove all instances of that value in-place and return the new ...

10. CommandLineParser命令行解析类

目的:方便用户在命令行使用过程中减少工作量 以前版本没这个类时,如果要运行带参数的.exe,必须在命令行中输入文件路径以及各种参数,并且输入的参数格式要与代码中的if语句判断内容格式一样,一不小心就输 ...