A.Chess
Problem Description
We have a chessboard, as the figure below, it shows the chessboard with dimension 1, 3, 5, 7 (We ignore the even number).Now, your task is to place K bishops on the n – dimension chessboard and satisfied that none of then attack each other ( A bishop can move to any distance in any of the four diagonal directions).
Input
输入包含两个整数n(n < 100 & & n % 2 = = 1)和k(k < = 10000)之间用一个空格分开。
Output
输出的答案mod 100007.
Sample Input
7 3 1 1
Sample Output
1038 1 第一种方法:
View Code
1 #include<stdio.h> 2 3 __int64 Q[50]; 4 #define mod 100007 5 6 __int64 xiangqi( __int64 n,__int64 k ) 7 { 8 __int64 i; 9 __int64 j; 10 __int64 sum=1; 11 12 for( i = 1; i <= n ; i++ ) 13 Q[i] = i*i; 14 for( i = k; i >=2 ; i-- ) 15 { 16 j = 1; 17 while( i * j <= n ) 18 { 19 if( j*i >= n-k+1 && Q[i*j] % i == 0 ) 20 { 21 Q[i*j] /= i ; 22 break; 23 } 24 j++; 25 } 26 } 27 for( i = n ; i >= n-k+1 ; i-- ) 28 { 29 sum *= Q[i]%mod; 30 sum %= mod; 31 } 32 return sum; 33 } 34 35 36 37 int main( ) 38 { 39 int n; 40 int k; 41 int num1; 42 int num2; 43 __int64 sum; 44 int i; 45 46 while( scanf("%d %d",&n,&k) == 2 ) 47 { 48 49 sum = 0; 50 if( n < 100 && n % 2 == 1 ) 51 { 52 if( k > n ) 53 { printf("0\n"); continue; } 54 if( n == 1 ) 55 { printf("1\n"); continue; } 56 num1 = (n+1)/2; 57 num2 = num1 - 1; 58 59 for( i = k; i >= 0 ; i-- ) 60 { 61 if( i > num1 || k - i > num2 ) 62 continue; 63 sum += xiangqi(num1,i)*xiangqi(num2,k-i)%mod; 64 sum %= mod; 65 } 66 } 67 printf("%I64d\n",sum); 68 } 69 return 0; 70 }
第二种方法 :
View Code
1 #include <iostream> 2 3 using namespace std; 4 #define MOD 100007 5 int n, k; 6 __int64 dp[2][101][101]; 7 8 void DP( int mod, int n ) { 9 10 int i, j; 11 for(i = 0; i <= n; i++) { 12 for(j = 0; j <= n; j++) { 13 dp[ mod ][i][j] = 0; 14 } 15 } 16 dp[ mod ][0][0] = 1; 17 for( i = 1; i <= n; i++) { 18 dp[mod][i][0] = 1; 19 for(j = 1; j <= n; j++) { 20 dp[mod][i][j] = dp[mod][i-1][j]; 21 dp[mod][i][j] += dp[mod][i-1][j-1] * ( n - j + 1 ) % MOD; 22 dp[mod][i][j] %= MOD; 23 } 24 } 25 } 26 27 int main() { 28 int i; 29 30 /*freopen ("1005.in", "r", stdin ); 31 freopen ("1005.out", "w", stdout );*/ 32 33 while( scanf("%d %d", &n, &k) != EOF ) { 34 DP(0, n/2); 35 DP(1, n - n/2); 36 37 if( k > n ) { 38 printf("0\n"); 39 continue; 40 } 41 42 __int64 sum = 0; 43 for(i = 0; i <= k; i++) { 44 if( i > n || k-i > n ) 45 continue; 46 sum = sum + ( dp[0][n/2][i] * dp[1][n-n/2][k-i] ) % MOD; 47 sum %= MOD; 48 } 49 printf("%I64d\n", sum); 50 } 51 return 0; 52 }