【Codeforces】Gym 101156G Non-Attacking Queens 打表

题意

求$n\times n$的棋盘上放$3$个皇后使得互相不攻击的方案数



拓展是$m\times n$棋盘上放$k$皇后,暴力打表找到了公式

OEIS

代码

import java.math.BigInteger;
import java.util.Scanner;

/**
 * Created by cyuun on 2018/1/24.
 */
public class Main {
    public static void main(String[] args) {
        BigInteger n,ans;
        Scanner cin = new Scanner(System.in);
        n=cin.nextBigInteger();
        if(n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(1))==0) {
            //(n - 1)(n - 3)(2n^4 - 12n^3 + 25n^2 - 14n + 1)/12
            ans=n.subtract(BigInteger.valueOf(1));
            ans=ans.multiply(n.subtract(BigInteger.valueOf(3)));
            BigInteger part1=n.multiply(n).multiply(n).multiply(n).multiply(BigInteger.valueOf(2));
            BigInteger part2=n.multiply(n).multiply(n).multiply(BigInteger.valueOf(12));
            BigInteger part3=n.multiply(n).multiply(BigInteger.valueOf(25));
            BigInteger part4=n.multiply(BigInteger.valueOf(14));
            ans=ans.multiply(part1.subtract(part2).add(part3).subtract(part4).add(BigInteger.valueOf(1)));
            ans=ans.divide(BigInteger.valueOf(12));
        }else {
            //n(n - 2)^2(2n^3 - 12n^2 + 23n - 10)/12
            ans=n;
            ans=ans.multiply(n.subtract(BigInteger.valueOf(2))).multiply(n.subtract(BigInteger.valueOf(2)));
            BigInteger part1=n.multiply(n).multiply(n).multiply(BigInteger.valueOf(2));
            BigInteger part2=n.multiply(n).multiply(BigInteger.valueOf(12));
            BigInteger part3=n.multiply(BigInteger.valueOf(23));
            ans=ans.multiply(part1.subtract(part2).add(part3).subtract(BigInteger.valueOf(10)));
            ans=ans.divide(BigInteger.valueOf(12));
        }
        System.out.println(ans);
    }
}

原文地址:https://www.cnblogs.com/ogiso-setsuna/p/8343853.html

时间: 2024-10-24 01:40:05

【Codeforces】Gym 101156G Non-Attacking Queens 打表的相关文章

Codeforces gym Hello 2015 Div1 B and Div2 D

Codeforces gym 100571 problem D Problem 给一个有向图G<V,E>和源点S,边的属性有长度L和颜色C,即E=<L,C>.进行Q次询问,每次给定一个点X,输出S到X的最短路的长度(不存在则输出 -1).但要求S到X的路径中相邻两条边颜色不一样. Limits Time Limit(ms): 1000 Memory Limit(MB): 256 |V|, |E|: [1, 10^5] X, S: [1, |V| ] L: [1, 10^9] |C|

Codeforces gym Hello 2015 Div1 E

Codeforces gym 100570 problem E (一种处理动态最长回文子串问题的方法) Problem 给一个长度为N的字符串S,字符集是'a'-'z'.进行Q次操作,操作分三种.一,修改位置X的字符为C:二,查询以P位置为中心的最长回文子串的长度,并输出:三,查询以P与P+1的中间位置为中心的最长回文子串的长度,并输出. More 第二种操作子串长度为奇数,一定存在:第三种操作子串长度为偶数,若不存在,输出 -1. Limits Time Limit(ms): 4000(1s足

Codeforces gym Hello 2015 Div1 C and Div2 E

Codeforces gym 100570 problem C Codeforces gym 100571 problem E Problem 给一个N行M列的矩阵Ma,进行Q次(Q<=10)查询,每次给定一个K,问有多少子矩阵,满足最大值max - 最小值min <=K. Limits Time Limit(ms): 8000 Memory Limit(MB): 512 N, M: [1, 400] Q: [1, 10] Ma(i, j), K: [1, 10^9] Solution (Th

【模拟】ECNA 2015 I What&#39;s on the Grille? (Codeforces GYM 100825)

题目链接: http://codeforces.com/gym/100825 题目大意: 栅栏密码.给定N(N<=10),密钥为一个N*N的矩阵,'.'代表空格可以看到,'X'代表被遮挡,还有密文字符串S,长度为N*N 每次将这个矩阵顺时针旋转90°,把矩阵中空格对应的位置按照从上到下从左到右的顺序依次填充上密文字符,求最终这个密文字符能否填满N*N的矩阵,能按顺序输出得到的答案,不能输出"invalid grille" 题目思路: [模拟] 直接模拟即可.旋转的坐标公式很好推.

Codeforces gym Hello 2015 Div2 B

Codeforces gym 100571 problem B Problem 设函数F(x),F(1)与F(2)已知,且当 i>=3,F(i)=a*F(i-2)+b*F(i-1).再给一个长度为N的数列A,进行Q次如下操作:每次给一个区间[L, R],对于每个k(L=<k<=R),将A[k]=A[k]+F[k-L+1].最后输出数列A(mod 10^9+7). Limits Time Limit(ms): 1000 Memory Limit(MB): 256 N, Q: [1, 10^

Codeforces Gym - 101147J Whistle&#39;s New Car

Discription Statements Whistle has bought a new car, which has an infinite fuel tank capacity. He discovered an irregular country since it has n cities and there are exactly n?-?1roads between them, of course, all cities are connected. He is so much

Codeforces Gym 101174 A Within Arm&#39;s Reach 贪心 手臂

#include<iostream> #include<stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <math.h> using namespace std; #define LL long long const int maxn=25; double a[maxn],l[maxn],r[maxn]; double ex,ey

Codeforces GYM 100114 C. Sequence 打表

C. Sequence Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description Integer sequences are very interesting mathematical objects. Let us examine a sequence generated with the use of two operations: doubling and “digit

Codeforces Gym 100637A A. Nano alarm-clocks 前缀和处理

A. Nano alarm-clocks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/A Description An old watchmaker has n stopped nano alarm-clocks numbered with integers from 1 to n. Nano alarm-clocks count time in hours, and