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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值