ICPC North America Qualifier Contest 2015 D. Circuit Counting(dp)

Suppose you are given a sequence of NN integer-valued vectors in the plane (x_i,y_i)(xi​,yi​), i=1,\dots,Ni=1,…,N. Beginning at the origin, we can generate a path by regarding each vector as a displacement from the previous location. For instance, the vectors (1,2),(2,3),(−3,−5)(1,2),(2,3),(−3,−5) form the path (0,0),(1,2),(3,5),(0,0)(0,0),(1,2),(3,5),(0,0). We define a path that ends at the origin as a circuit. The example just given is a circuit. We could form a path using any nonempty subset of the NN vectors, while the result (circuit or not) doesn't depend on the ordering of the subset. How many nonempty subsets of the vectors form circuits?

For instance, consider the vectors \{(1,2),(−1,−2),(1,1),(−2,−3),(−1,−1)\}{(1,2),(−1,−2),(1,1),(−2,−3),(−1,−1)} From these vectors we can construct 44 possible subset circuits using

\left. \begin{array}{l} \{(1,2),(−1,−2)\}\\ \{(1,1),(−1,−1)\}\\ \{(1,2),(1,1),(−2,−3)\}\\ \{(1,2),(−1,−2),(1,1),(−1,−1)\} \end{array}\right.{(1,2),(−1,−2)}{(1,1),(−1,−1)}{(1,2),(1,1),(−2,−3)}{(1,2),(−1,−2),(1,1),(−1,−1)}​

 

Input

Input begins with an integer N\leq 40N≤40 on the first line. The next NN lines each contain two integer values xx and yyforming the vector (x,y)(x,y), where |x|,|y|\leq 10∣x∣,∣y∣≤10 and (x,y)\neq(0,0)(x,y)​=(0,0). Since the given vectors are a set, all vectors are unique.

Output

Output the number of nonempty subsets of the given vectors that produce circuits. It's guaranteed that the answer is less than 10^{10}1010.

输出时每行末尾的多余空格,不影响答案正确性

样例输入复制

5
1 2
1 1
-1 -2
-2 -3
-1 -1

样例输出复制

4

题意:

给出 n 组 (x, y)的值,问有多少种组合方式使得取出的所有(x, y)值的和为 (0, 0)

思路:

数据最大值是400,用dp[i][X][Y]表示前 i 个数对中选出若干个,和为(X, Y)的种类数。

状态转移方程:dp[i][X][Y] = dp[i - 1][X - x][Y - y] + dp[i - 1][X][Y]

有关滚动数组:https://blog.csdn.net/f_zyj/article/details/51478119

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const int N = 1e3 + 10;

ll dp[2][N][N];

int main()
{
    int n, x, y;
    scanf("%d", &n);
    dp[1][500][500] = 1;
    for(int i = 0; i < n; ++i)
    {
        scanf("%d%d", &x, &y);
        for(int j = 0; j <= 1000; ++j)
        {
            for(int k = 0; k <= 1000; ++k)
            {
                dp[i & 1][j][k] = dp[(i & 1) ^ 1][j][k];
                if(j - x >= 0 && j - x <= 1000 && k - y >= 0 && k - y <= 1000)
                {
                    dp[i & 1][j][k] += dp[(i & 1) ^ 1][j - x][k - y];
                }
            }
        }
    }
    cout<<dp[(n & 1) ^ 1][500][500] - 1<<'\n';
    return 0;
}

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-24 13:35
点赞 评论 收藏
分享
想按时下班的大菠萝在...:隔壁学校的,加油多投, 实在不好找可以下个学期开学找,把算法八股准备好,项目有空再换换
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务