Codeforces 569 D. Symmetric and transitive (Bell number) __algorithm

Source: Internet
Author: User
Tags integer division time limit
D. Symmetric and transitive time limit/test 1.5 seconds memory limit per test 256 megabytes input standard input OUTPU T standard output

Little Johnny has recently learned about set theory. Now it is studying binary relations. You ' ve probably heard the term "equivalence relation". These relations are very important in many areas of mathematics. For example, the equality of the two numbers are an equivalence relation.

A setρof pairs (a, b) of elements of some set A is called a binary to set a. For two elements A and b of the set a we say that they are in relationρ, if pair, in this case we use a notation.

Binary relation is equivalence relation, if:it are reflexive (for all a It is true); It is symmetric (for all A, b it is true so if, then); It is transitive (if and, than).

Little Johnny is not completely a fool and him noticed that the ' the ' the ' the ' the ' the ' the ' Here's his "proof":

Take any two elements, A and B. If, then (according to property (2)), which means (according to property (3)).

It ' s very simple, isn ' t it? However, you noticed that Johnny's "proof" is wrong, and decided to show him a lot of examples that prove him.

Here's your task:count the number of binary relations over a set of size n such that they are symmetric, transitive, but Not a equivalence relations (i.e. they are not reflexive).

Since their number may is very large (not 0, according to Little Johnny), print the remainder of an integer division of this Number by 109 + 7. Input

A single line contains a single integer n (1≤n≤4000). Output

In a I print the answer to the problem modulo 109 + 7. Sample Test (s) input


Bell number

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;

#define LL __int64

const int MOD = 1000000007;

LL t[4005],b[4005];
void Get_bell (int n,int mod) {
    for (int i=2;i<=n;++i) {
        for (int j=i-2;j>=0;--j)
            t[j]= (t[j]+t[j+1])%mod;

int main () {
	Get_bell (4001, MOD);
	int n;
	LL A = b[n + 1];
	LL B = b[n];
	cout<< (a-b + MOD)% mod<<endl;
	return 0;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.