var a,b,s:array[0..1000001] of int64; n,d:longint; kq:int64; procedure nhapdl; var…
- User
var a,b,s:array[0..1000001] of int64; n,d:longint; kq:int64; procedure nhapdl; var i:longint; begin assign(input,'bai5.inp'); reset(input); assign(output,'bai5.out'); rewrite(output); readln(d,n); s[0]:=1; for i:=1 to n do begin read(a[i]); b[i]:=(a[i]+b[i-1]) mod d; inc(s[b[i]]); end;
end; procedure cach1; var i,j:longint; begin kq:=0; for i:=1 to n do for j:=i to n do if (b[j]-b[i-1]) mod d =0 then inc(kq); write(kq); end; procedure cach2; var i,j:longint; begin kq:=0; for i:=0 to d-1 do kq:=kq+(s[i]*(s[i]-1) div 2); write(kq); end; begin
codenhapdl; //cach1; //writeln; cach2; close(input); close(output);end. c++
- Assistant
To translate the given Pascal code into C++, we must understand the underlying functionality and logic first. The code is solving a mathematical problem using two approaches: a brute force method (cach1) that iterates over all possible subarray combinations checking if their sums are divisible by
d, and an optimized method (cach2) that leverages the number of occurrences of each remainder when dividing the cumulative sums byd.