classSolution { public: intgetSubarrayNum(vector<int>& a, int x){ int n=a.size(); vector<int>sum1(n+1); vector<int>sum2(n+1); for(int j=0;j<n;j++){ int i=j+1; while(a[j]%2==0){ sum1[i]+=1; a[j]/=2; } //记录2的个数 while(a[j]%5==0){ sum2[i]+=1; a[j]/=5; } } //记录5的个数 for(int i=1;i<=n;i++){ sum1[i]+=sum1[i-1]; sum2[i]+=sum2[i-1]; } //前缀和 int posi=1; int posj=1; int ans=0; int mod = 1e9+7; while(posi<=n&&posj<=n) { while(posi>posj) posj++; if(min(sum1[posj]-sum1[posi-1],sum2[posj]-sum2[posi-1])>=x) { ans+=(n-posj+1)%mod; ans%=mod; posi++; } else { posj++; } } return ans; } };
好矩阵
组合数学即可
注意到第一行第一列可以乱放,其他只需要放另一半即可
比较思维,不过会也可以一眼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public: int mod=1e9+7; intp(longlong m,longlong n) { int a=1; while(n){ if(n&1)a=1LL*a*m%mod; n>>=1; m=1LL*m*m%mod; } return a; } intnumsOfGoodMatrix(int n, int m, int x) { return1LL*p(x,n+m-1)*p(x/2,1LL*(n-1)*(m-1))%mod; } };