3 solutions

  • 1
    @ 2024-4-13 12:22:02

    #include <bits/stdc++.h> int n, shuru; int a[210][1010]; int main(){ std::cin >> n; for(int i = 1; i <= n; i ++) { for(int j = 1; j <= n; j ++) { std::cin >> shuru; } } a[1][0] = 1; a[1][1] = 0; a[2][0] = 1; a[2][1] = 1; for(int i = 3; i <= n; i ++) { int jw = 0; for(int j = 1; j <= 1000; j ++) { a[i][j] = a[i - 1][j] + a[i - 2][j] + jw; jw = 0; jw = a[i][j] / 10; a[i][j] = a[i][j] % 10; } a[i][0] = a[i - 1][0]; for(int j = 1; j <= 1000; j ++) { a[i][j] = a[i][j] * (i - 1) + jw; jw = a[i][j] / 10; a[i][j] = a[i][j] % 10; } } int flag = 0; for(int i = 1000; i >= 1; i --) { if(flag == 1 || !(a[n][i] == 0)) { flag = 1; std::cout << a[n][i]; } } return 0; }

    • 0
      @ 2023-11-21 16:57:06

      这是一道错排问题,不知道的可以先做一下P1985(直接复制过来能得60分)

      递推公式:f (i)=(i - 1) * (f (i - 1) + f (i - 2))

      代码如下:

      #include <bits/stdc++.h>
      int n, shuru;
      int a[210][1010];
      int main(){
          std::cin >> n;
          for(int i = 1; i <= n; i ++)
          {
          	for(int j = 1; j <= n; j ++)
          	{
          		std::cin >> shuru;
      		}
      	}
          a[1][0] = 1; a[1][1] = 0;
          a[2][0] = 1; a[2][1] = 1;
          for(int i = 3; i <= n; i ++)
          {
          	int jw = 0;
          	for(int j = 1; j <= 1000; j ++)
          	{
          		a[i][j] = a[i - 1][j] + a[i - 2][j] + jw;
          		jw = 0;
      			jw = a[i][j] / 10;
          		a[i][j] = a[i][j] % 10;
      		}
      		a[i][0] = a[i - 1][0];
      		for(int j = 1; j <= 1000; j ++)
      		{
      			a[i][j] = a[i][j] * (i - 1) + jw;
      			jw = a[i][j] / 10;
      			a[i][j] = a[i][j] % 10;
      		}
      	}
      	int flag = 0;
      	for(int i = 1000; i >= 1; i --)
      	{
      		if(flag == 1 || !(a[n][i] == 0))
      		{
      			flag = 1;
      			std::cout << a[n][i];
      		}
      	}
          return 0;
      }
      

      时间复杂度O(2000*n)

      • -1
        @ 2024-4-8 11:39:04
        #include <bits/stdc++.h>
        int n, shuru;
        int a[210][1010];
        int main(){
            std::cin >> n;
            for(int i = 1; i <= n; i ++)
            {
            	for(int j = 1; j <= n; j ++)
            	{
            		std::cin >> shuru;
        		}
        	}
            a[1][0] = 1; a[1][1] = 0;
            a[2][0] = 1; a[2][1] = 1;
            for(int i = 3; i <= n; i ++)
            {
            	int jw = 0;
            	for(int j = 1; j <= 1000; j ++)
            	{
            		a[i][j] = a[i - 1][j] + a[i - 2][j] + jw;
            		jw = 0;
        			jw = a[i][j] / 10;
            		a[i][j] = a[i][j] % 10;
        		}
        		a[i][0] = a[i - 1][0];
        		for(int j = 1; j <= 1000; j ++)
        		{
        			a[i][j] = a[i][j] * (i - 1) + jw;
        			jw = a[i][j] / 10;
        			a[i][j] = a[i][j] % 10;
        		}
        	}
        	int flag = 0;
        	for(int i = 1000; i >= 1; i --)
        	{
        		if(flag == 1 || !(a[n][i] == 0))
        		{
        			flag = 1;
        			std::cout << a[n][i];
        		}
        	}
            return 0;
        }
        
        • 1

        Information

        ID
        2236
        Time
        1000ms
        Memory
        125MiB
        Difficulty
        5
        Tags
        # Submissions
        5
        Accepted
        4
        Uploaded By