整除查询

Description

Given an array of positive integers and many queries for divisibility. In every query Q[i], we are given an integer K , we need to count all elements in the array which are perfectly divisible by K.

Constraints:1<=T<=1001<=N,M<=1051<=A[i],Q[i]<=105

Input

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of three lines. First line of each test case contains two integers N & M, second line contains N space separated array elements and third line contains M space separated queries.

Output

For each test case,In new line print the required count for each query Q[i].

Sample Input 1

2
6 3
2 4 9 15 21 20
2 3 5
3 2
3 4 6
2 3

Sample Output 1

3 3 2
2 2

Solution

# 求数组中每次查询能被整除的数的个数
if __name__ == "__main__":
    T = int(input())
    while T:
        T -= 1
        input()
        nums = list(map(int, input().split()))
        query = list(map(int, input().split()))
        cnts = []
        for k in query:
            cnt = 0
            for num in nums:
                if num % k == 0:
                    cnt += 1
            cnts.append(cnt)

        print(' '.join(map(str, cnts)))
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,452评论 0 10
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,516评论 0 13
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,959评论 0 23
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,936评论 0 0
  • 卫潇潇整夜没睡着,天蒙蒙亮就动身去东山别墅了。 没想到戚小姐起得比她还早,卫潇潇来的时候,戚小姐正在客厅里做着瑜伽...
    旧文字阅读 372评论 0 1