Number of factors
7041125 Number of factors |
Difficulty level: B; run time limit: 1000ms; operating space limit: 51200KB; code length limit: 2000000B |
Question Description |
Calculates the number of factors for C (n,k). |
Input |
There are multiple lines, each containing two positive integers n and K, separated by a space. |
Output |
Multiple rows, each containing a number, followed by the result of the input data for each group. |
Input example |
5 1 6 3 10 4 |
Output example |
2 6 16 |
Other Notes |
Data range: 0 <= k <= n <= 431. |
For a long time did not work out, today finish brushing homework to a ...
Obviously it will blow long long, so even if the Yang Hui Triangle hit the table.
There is a formula for: C (n,m) =n!/m! (N-M)!
Make it a variant of C (n,m) =m+1~n/(N-M)!
Note that m+1 is multiplied by N with (N-M)! is not the same.
Then two loops, recording their number of quality factors into an array.
The final qualitative factor index plus repeated multiplication, you get the answer.
The code is as follows:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm>using namespace Std;int ks[1001];int n,m;void FJ (int k) { int num=2; while (k!=1) { if (k%num==0) k/=num,ks[num]++; else num++; } return;} void fj2 (int k) { int num=2; while (k!=1) { if (k%num==0) k/=num,ks[num]--; else num++; } return;} void Num (int n,int M) {for (int i=m+1;i<=n;i++) FJ (i); for (int i=1;i<=n-m;i++) fj2 (i); return;} int main () {while (scanf ("%d%d", &n,&m)!=eof) { memset (ks,0,sizeof (KS)); Long long Ans=1; Num (n,m); for (int i=1;i<=n;i++) ans*= (ks[i]+1); cout<<ans<<endl; }}
if necessary, we can also optimize the program with a linear sieve:
(However, the evaluation machine is too slow to give the same running time as the previous one ...) )
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm>using namespace Std;int ks[501];int map[501];int mark[501];int tot;int n,m;void work (int k,int p) { int num=1; while (k!=1) { if (k%map[num]==0) k/=map[num],ks[map[num]]+=p; else num++; } return;} void Eular () {mark[1]=1;for (int i=2;i<=500;i++) {if (!mark[i]) map[++tot]=i;for (int j=1;j<=tot;j++) {if (I*map[j] >500) break;mark[i*map[j]]=1;if (i%map[j]==0) break;}} return;} void Num (int n,int M) {for (int. i=m+1;i<=n;i++) work (i,1); for (int. i=1;i<=n-m;i++) work (i,-1); return;} int main () {eular (); while (scanf ("%d%d", &n,&m)!=eof) { memset (ks,0,sizeof (KS)); Long long Ans=1; Num (n,m); for (int i=1;i<=n;i++) ans*= (ks[i]+1); cout<<ans<<endl; }}
"Number theory" 7041125 factors