【HDOJ】1258 Sum It Up】的更多相关文章

典型的深搜,剪枝的时候需要跳过曾经搜索过的相同的数目,既满足nums[i]=nums[i-1]&&visit[i-1]==0,visit[i-1]==0可以说明该点已经测试过. #include <stdio.h> #include <string.h> #define MAXNUM 1005 int nums[MAXNUM]; int visit[MAXNUM]; int t, n; void output() { ; ; i<t; ++i) { if (j…
数学题.f(n) = 2^(n-1) mod (1e9+7). #include <cstdio> #define MAXN 100005 char buf[MAXN]; __int64 phi = 1e9+; __int64 mod = 1e9+; __int64 power2(__int64 n) { __int64 ret = , ; --n; while (n) { ) ret = ret * base % mod; base = base*base%mod; n >>=…
HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1010 Tempter of the Bone [从零开始DFS(1)] -DFS四向搜索/奇偶剪枝 HDOJ(HDU).1015 Safecracker [从零开始DFS(2)] -DFS四向搜索变种 HDOJ(HDU).1016 Prime Ring Problem (DF…
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum-root-to-leaf-numbers/description/ 题目描述: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a numbe…
404. Sum of Left Leaves [题目]中文版  英文版 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int sumOfLeftLea…
[题意]对于n个数,找出一些数使得它们的和能被n整除,输出任意一组方案,n<=10^6. [算法]构造/结论 [题解]引用自:http://www.cnblogs.com/Sakits/p/7407103.html by Sakits 对n个数求前缀和,即sum[i]=sigma(a[1~i])%n,若sum[i]=0则找到答案,否则n个前缀和填充1~n-1,根据抽屉原理必有两个前缀和相同,则中间那一段数字之和是n的倍数. #include<cstdio> ],a[]; int main…
[Description] Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target,…
Difficulty: Easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-numbers/submissions/ Given a non-negative integer c, your task is to decide whether there're two integers aand b such that a2 + b2 = c. Example 1: Inpu…
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4,5].那么对于元素2来说,和左边区间合并组成[2,3]以及和右边区间合并组成[2,4,5],这两段区间包括2在内的所有subarray的最小值都是2,其分别可以组成的subarry的个数是 len([3])和len([4,5]),左右区间合并在一起可以组成的subarray的个数是len([3])…
划分树解.主席树解MLE. /* 3473 */ #include <iostream> #include <sstream> #include <string> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <deque> #include <algorit…
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……sigh) //HDOJ 3415 #include<queue> #include<cmath> #include<vector> #include<cstdio> #include<cstring> #include<cstdlib>…
最开始使用递归DP解,stack overflow.化简了一些,复杂度为O(n)就过了. #include <stdio.h> int main() { int case_n, n; int i, j, tmp; int beg, end, sum, max_sum; int number; scanf("%d", &case_n); ; i<=case_n; i++) { scanf("%d", &n); beg = ; tmp…
这题目一直wa,原来是因为我把JUDGE写错了,对拍了一下午都没检查出来.水DP啊. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <iostream> using namespace std; #define MAXN 1020 #define MAXM 35 #define INF 0xfffff int dp[MAX…
题意很简单就是给你一个N和M,让你求在1-N的那些个子序列的值等于M 首先暴力法不解释,简单超时 再仔细想一想可以想到因为1-N是一个等差数列,可以运用我们曾经学过的只是来解决 假设开始的位置为s,结束的位置为t,那么一定要满足这个等式 (s+t)(t-s+1)=2*m 又因为S和T都是整数,所以左边的括号中每一项都是等式 所以s+t和t-s+1一定是2*m的因式 所以分解因式并带入就可以求出s和t 假设 s+t=a t-s+1=b a*b=2*m 解得 s=(a-b+1)/2 t=(a+b-1…
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到boundry,使得boundry * n_edge - sum_edge <= k/b, 或者建立s->t,然后不断extend s->t. /* 4729 */ #include <iostream> #include <sstream> #include <…
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digits integers. For each integer in this list: The hundreds digit represents the depth D of this node, 1 <= D <= 4. The tens digit represents the positio…
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l,r)=\sum_{i=l}^{r}\sum_{j=i+1}^{r}a[i]*a[j]$ 那么就有 $w(l,r+1)=w(l,r)+a[j]*\sum\limits_{i=l}^{r}a[i]$ 所以:w[i][j]明显满足 关于区间包含的单调性 然后我们大胆猜想,小(bu)心(yong)证明,w[…
数位DP 题解:http://www.cnblogs.com/algorithms/archive/2012/09/02/2667637.html dfs的地方没太看懂……(也就那里是重点吧喂!)挖个坑……回头再看看 //HDOJ 3709 #include<cmath> #include<vector> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostre…
概率DP/数学期望/状压DP/容斥原理 kuangbin总结中的第14题 好神奇的做法……题解看kuangbin的代码好了…… //HDOJ 4336 #include<cstdio> #define rep(i,n) for(int i=0;i<n;++i) #define F(i,j,n) for(int i=j;i<=n;++i) #define D(i,j,n) for(int i=j;i>=n;--i) ; <<N]; int main(){ int n…
线性DP,使用单调队列优化. /* 4374 */ #include <iostream> #include <sstream> #include <string> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <deque> #include <algori…
基本思路是将树形结构转线性结构,因为查询的是从任意结点到叶子结点的路径.从而将每个查询转换成区间,表示从该结点到叶子结点的路径.离线做,按照右边界升序排序.利用树状数组区间修改.树状数组表示有K个数据的数量,利用pos进行维护.假设现有的sz >= K, 那么需要对区间进行修改. /* 4358 */ #include <iostream> #include <sstream> #include <string> #include <map> #inc…
digital root = n==0 ? 0 : n%9==0 ? 9:n%9;可以简单证明一下n = a0*n^0 + a1*n^1 + ... + ak * n^kn%9 = a0+a1+..+ak然后,数学归纳易知结论是正确的.因此9个状态就够了,表示%9的结果.这里需要特殊处理0, 表示状态为0. /* 4351 */ #include <iostream> #include <sstream> #include <string> #include <m…
思路1:树状数组+离线处理,对所有的w离散化处理,边dfs边使用树状数组更新左右w的情况.思路2:主席树,边bfs边建树.结点信息存储cnt,然后在线查询.树状数组. /* 4605 */ #include <iostream> #include <sstream> #include <string> #include <map> #include <queue> #include <set> #include <stack&g…
tarjan缩点,然后树形dp一下可解.重点是重边的处理. /* 2242 */ #include <iostream> #include <sstream> #include <string> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <deque> #inc…
DFS+剪枝.与HDOJ 1455如出一辙. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 25 int nums[MAXN], n, len, cnt; char visit[MAXN]; int comp(const void *a, const void *b) { return *(int *)b - *(int *)a; } int dfs(int cnt, in…
题目简述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1 retu…
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ]     采用深度…
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1…
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1 return true…
Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that…