TOJ : 4065. Query on an array

题目描述 :

Time Limit: 8.0 Seconds   Memory Limit: 65536K
Total Runs: 17   Accepted Runs: 6

Give you n positive integer, and the key number is 223092870.
There will be q queries, in each query, you will get an interval in this array.
Your task is to answer how many pairs of number that their product is equal to the key number in the query interval.
Please remember that each pair has exactly two number, and <1,2> and <2,1> are considered as same pair.
The array index is from 0 to n-1.

Input

First, there will be a number n(n≤10000) and followed by n positive integer.
After this, there will be a number q(q≤10000) indicates the number of queries.
In each query, there will be two number l and r(0≤l≤r≤n-1), means an interval in the query.

Output

For each query, you should output one integer means the number of pairs

Sample Input

10
1 2 3 4 5 6 7 8 111546435 223092870
5
0 3
0 9
1 9
1 8
3 9

Sample Output

0
2
1
1
0


代码如下:

#include <cstdio>
#include <cstring>
#define maxn 10001
#define Max 14860
using namespace std;

const int key = 223092870;
int a[maxn], s[Max][2], f[260];
int n, q, l, r;

int main()
{
	for (int i = 1, k = 0; i * i <= key; i++)
		if (key % i == 0) f[k++] = i;
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%d", a + i);
	scanf("%d", & q);
	while (q--)
	{
		memset(s, 0, sizeof(s));
		scanf("%d%d", &l, &r);
		int res = 0;
		for (int i = l; i <= r; i++)
		{
            if (!a[i]) continue;
			if (key % a[i] == 0)
			{
                if (a[i] < Max) s[a[i]][0]++;
                if (a[i] >= Max) s[key / a[i]][1]++;
            }	
		}
		for (int i = 0; i < 256; i++)
		{
            int ttt = f[i];
            res += s[ttt][0] * s[ttt][1];
        }
		printf("%d\n", res);
	}
		
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值