1 solutions

  • 0
    @ 2025-11-5 17:18:29

    C :

    #include <stdio.h>
    long long fun(int x)
    {
    	if(x==1)
    	return 0;
    	if(x==2)
    	return 1;
    	else
    	return ((x-1)*(fun(x-2)+fun(x-1)));
    }
    int main()
    {
    	int n;
    	while(scanf("%d",&n)!=EOF)
    	{
    		printf("%lld\n",fun(n));
    	}
    	return 0;
    }
    
    

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    #define maxn 100000007
    int main()
    {
    	int d[1005];
    	d[2]=1;d[1]=0;
    	for (int i=3;i<999;i++){
    		d[i]=(i-1)*(d[i-1]+d[i-2])%maxn;
    	}
    	
    	int n;
    	while(~scanf("%d",&n)){
    		printf("%d\n",d[n]);
    	}
    	
    	return 0;
    }
    
    • 1

    Information

    ID
    18277
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By