Binary Stirling Numbers(POJ-1430)

Problem Description

The Stirling number of the second kind S(n, m) stands for the number of ways to partition a set of n things into m nonempty subsets. For example, there are seven ways to split a four-element set into two parts: 

{1, 2, 3} U {4}, {1, 2, 4} U {3}, {1, 3, 4} U {2}, {2, 3, 4} U {1}

{1, 2} U {3, 4}, {1, 3} U {2, 4}, {1, 4} U {2, 3}.

There is a recurrence which allows to compute S(n, m) for all m and n. 

S(0, 0) = 1; S(n, 0) = 0 for n > 0; S(0, m) = 0 for m > 0;

S(n, m) = m S(n - 1, m) + S(n - 1, m - 1), for n, m > 0.

Your task is much "easier". Given integers n and m satisfying 1 <= m <= n, compute the parity of S(n, m), i.e. S(n, m) mod 2. 


Example 

S(4, 2) mod 2 = 1.

Task 

Write a program which for each data set: 
reads two positive integers n and m, 
computes S(n, m) mod 2, 
writes the result. 

Input

The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 <= d <= 200. The data sets follow. 

Line i + 1 contains the i-th data set - exactly two integers ni and mi separated by a single space, 1 <= mi <= ni <= 10^9. 

Output

The output should consist of exactly d lines, one line for each data set. Line i, 1 <= i <= d, should contain 0 or 1, the value of S(ni, mi) mod 2. 

Sample Input

1
4 2

Sample Output

1

题意:t 组数据,每组给出第二类斯特林数的 S(n,m) 的 n、m,求其奇偶性

思路:

首先求 S(n,m) 的奇偶性实质就是求 S(n,m)%2

对于第二类斯特林数,首先有:S[i][j]=S[i−1][j−1]+j∗S[i−1][j]

那么在模 2 的情况下 ,有:

  • 当 j 为偶数:S[i][j] ≡(S[i−1][j−1]%2)
  • 当 j 为奇数:S[i][j] ≡((S[i−1][j−1]+S[i−1][j])%2)

于是,可以倒过来,即有:

  • 当 j 为偶数时:S[i][j] 会被加到 S[i+1][j+1] 和 S[i+1][j] 
  • 当 j 为奇数时:S[i][j] 会被加到 S[i+1][j+1]

于是,令 a=n−m,b=(m+1)/2,则答案就相当于把 a 个相同的球分成 b 组,每组个数可以为 0 的方案总数,即:C(b−1,a+b−1)

然后利用 C(n,m) 为奇数时有 n&m=n 的结论,进行判断即可

最后,注意特判

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 10000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;

int calc(int n,int m){
    return (m&n)==n;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m;
        scanf("%d%d",&n,&m);
        if(n==0&&m==0)//特判
            printf("1\n");
        else if(n==0||m==0||n<m)//特判
            printf("0\n");
        else{
            int a=n-m;
            int b=(m+1)/2;
            int res=calc(b-1,a+b-1);
            printf("%d\n",res);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个命令是在 Linux 系统中启动 D-Bus 的命令。 其中: - `dbus-launch` 是 D-Bus 的启动命令。 - `--autolaunch` 参数表示自动启动一个新的 D-Bus 进程。 - `59cd40382b8a4dfd9147ebeae3544d28` 是自动生成的会话总线地址。 - `--binary-syntax` 参数表示使用二进制语法。 - `--close-stderr` 参数表示在 D-Bus 进程终止时关闭 stderr 文件描述符。 ### 回答2: dbus-launch --autolaunch 59cd40382b8a4dfd9147ebeae3544d28 --binary-syntax --close-std 命令是用于在Linux系统上启动DBus会话的工具。 DBus是一种进程间通信系统,用于在运行中的应用程序之间进行通信和交换消息。dbus-launch命令可以启动一个与当前会话相关联的DBus守护进程,使应用程序能够利用DBus进行通信。 在使用dbus-launch命令时,--autolaunch选项用于指定DBus守护进程的自动启动,后面的参数指定了DBus守护进程的启动标识。--binary-syntax选项用于生成以二进制格式表示的DBus启动信息,这样的输出可以被其他应用程序解析和使用。--close-std选项则表示在启动完DBus守护进程后,关闭标准输出和标准错误输出。 通过执行dbus-launch命令,DBus守护进程被启动后,就可以通过DBus接口进行各种应用程序之间的通信,包括发送和接收消息,调用远程对象的方法等。这对于需要实现应用程序间数据交换和协作的场景非常有用。 ### 回答3: 在Linux中,"dbus-launch --autolaunch 59cd40382b8a4dfd9147ebeae3544d28 --binary-syntax --close-st"命令的含义如下: dbus-launch是一个用于启动DBus消息总线的命令。DBus是一种进程间通信机制,它允许不同应用程序之间进行通信和交换数据。dbus-launch命令用于启动和管理DBus的会话总线。 --autolaunch 59cd40382b8a4dfd9147ebeae3544d28是dbus-launch命令的一个选项和参数。它指定了要使用的"autolaunch"配置,并提供了一个唯一的标识符(59cd40382b8a4dfd9147ebeae3544d28)。该选项告诉dbus-launch使用先前保存的配置启动自动总线。 --binary-syntax是dbus-launch命令的另一个选项。它指定了DBus消息交换的二进制语法。DBus可以使用不同的消息序列化格式,如二进制、XML等。使用--binary-syntax选项,dbus-launch将使用二进制格式的消息交换。 --close-st是dbus-launch命令的最后一个选项。它表示命令在启动DBus会话总线后自动关闭自身。这可以避免命令在后台持续运行,释放相关资源和内存空间。 总之,"dbus-launch --autolaunch 59cd40382b8a4dfd9147ebeae3544d28 --binary-syntax --close-st"命令启动DBus会话总线,并使用指定的配置和参数来管理进程间通信。它还使用二进制消息格式,并在启动总线后自动关闭自己。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值