请教一个Time Limit Exceed的问题,关于ACM的。
一个挺简单的题目,但是始终是报Time Limit Exceed的问题,
麻烦大家给看看哪里可以改进下算法。
题目:
Description
One day,Little-Y saw many numbers standing in a row. A question suddenly appeared in her mind, ”From the L-th number to the R-th number, how many of them is a mutiple of P ? (P is a prime number) , and how quickly can I settle this problem ? ”
Input
Mutiple test cases. Please process till the end of file.
For each test case:
The first line contains two positive integers n and q (1<=n,q<=10^5), which means there are n integers standing in a row and q queries followed.
The second line contains n positive integers, a1,a2,a3,…,an (no more than 10^6) . Here, ai means the i-th integer in the row.
The next are q queries, each of which takes one line. For each query, there are three integers L,R,P (1<=L<=R<=n, 1<=P<=10^6, P is gurantteed to be a prime number). Their meanings have been mentioned in the discription.
Output
For each query, output the answer in one line.
Sample Input
6 5
12 8 17 15 90 28
1 4 3
2 6 5
1 1 2
3 5 17
1 6 999983
Sample Output
2
2
1
1
0
我的解答:
#include "stdafx.h"
#include <iostream>
#include <map>
using namespace std;
int main(int argc, char* argv[])
{
int elemNum, queryNum;
int i, j, z, son, answer, L, R, P;
while(cin>>elemNum>>queryNum) {
int *elemList = new int[elemNum];
while(elemNum --)
cin>>elemList[elemNum];
int (*queryList)[3] = new int[queryNum][3];
while(queryNum --) {
cin>>queryList[queryNum][0];
cin>>queryList[queryNum][1];
cin>>queryList[queryNum][2];
answer = 0;
son = queryList[queryNum][2];
for(i = queryList[queryNum][0] - 1;i < queryList[queryNum][1];i ++) {
elemList[i] % son == 0 ? answer ++ : answer;
}
cout<<answer<<endl;
}
}
return 0;