题目:
Watson给了Sherlock一个问题,请帮助Sherlock解决这个问题。
Watson给了一个数组 A1,A2,⋯AN。
他也给了另外两个数组 B1,B2⋯BM 和 C1,C2,⋯CM。
然后他要Sherlock做如下的操作:
for i=1 to M do
for j=1 to N do
if j%B[i]==0 then
A[j]=A[j]*C[i]
endif
end do
end do
Sherlock必须回报数组 A 最后的状态给Watson。
输入格式
第一行包含 N 和 M,下一行是 N 个元素代表 A,接下来两行各有 M 个元素分别代表数组 B和数组 C。
输出格式
在一行中输出 N 个数,代表数组 A 最后的状态。输出 A 中的每个元素对 (109+7) 取余的结果。
约束条件:
1≤N,M≤105
1≤B[i]≤N
1≤A[i],C[i]≤105
public static void main(String[] args)throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader((System.in)));
PrintWriter pw=new PrintWriter(System.out);
String[] tempstr=br.readLine().split(" ");
int N=Integer.parseInt(tempstr[0]);
int M=Integer.parseInt(tempstr[1]);
long[] A=new long[N+1];
long[] count=new long[N+1];//记录重复相乘.也就是B[i]=B[j]的时候A[k*B[i]]*=C[i]*C[j];把B[i]编程count的下标
int[] B=new int[M];
long [] C=new long[M];
int i;
String[] tempstr1=br.readLine().split(" ");
for(i=0;i<N;i++){
A[i+1]=Integer.parseInt(tempstr1[i]);
}
String[] tempStr2=br.readLine().split(" ");
String[] tempStr3=br.readLine().split(" ");
for( i=0;i<M;i++) {
B[i] = Integer.parseInt(tempStr2[i]);
C[i] = Integer.parseInt(tempStr3[i]);
}
int test=1000000007;
for(i=0;i<B.length;i++){
if(count[B[i]]==0){
count[B[i]]=C[i];
}else{
count[B[i]]=(count[B[i]]*C[i])%test;
}
}
int temp;
for(i=1;i<count.length;i++){
int j=1;
temp=i*j;
while(temp<A.length){
if(count[i]!=0){
A[temp]=(count[i]*A[temp])%test;
}
j++;
temp=i*j;
}
}
for(i=1;i<A.length;i++){
pw.print(A[i]+" ");
}
pw.flush();
}