brute force combinatorics dp *1800

Please click on ads to support us..

C++ Code:

// becoder Submission #undefined @ 1707012947661
#include <bits/stdc++.h>
using namespace std;
#define int long long
long long l, r;
int len, m;
int a[1005];
char b[1005];
int dp[1005][1005][2];
int num[1005];
int se(int x)
{
	int cnt = 0;
	while(x != 1) x = __builtin_popcount(x), ++cnt;
	return cnt;
}
int dfs(int n, bool limit, int cnt)
{
    if (n == 0) return num[cnt] == m - 1;
    if (~dp[n][cnt][limit]) return dp[n][cnt][limit];
    int up;
    int now = 0;
    if (limit) up = a[n];
    else up = 1;
    for (int i = 0; i <= up; ++i) now += dfs(n - 1, limit && (i == a[n]), cnt + (i != 0)), now %= 1000000007;
    return dp[n][cnt][limit] = now;
}
int solve(int x)
{
	if(!m) return 1;
	memset(dp, -1, sizeof(dp));
	len = strlen(b + 1);
	for (int i = 1; i <= len; ++i) a[i] = b[len - i + 1] - '0';
    return dfs(len, 1, 0) - 2 * (m == 1);
}
signed main()
{
    memset(dp, 0xf3, sizeof(dp));
    for (int i = 1; i <= 1000; ++i) num[i] = se(i);
    scanf("%s", b + 1);
    cin >> m;
    cout << solve(0);
}


Comments

Submit
0 Comments
More Questions

733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences