Sum of Distinct Numbers(求组成N的不同个数和)

Sum of Distinct Numbers
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 3, Accepted users: 3
Problem 13098 : No special judgement
Problem description

A positive integer N can be written in the form of sum of distinct positive integers in several ways. For example,
N=5, there are 3 ways: 5, 2+3, 1+4
N=6, there are 4 ways: 6, 1+5, 1+2+3, 2+4
Here the permutation of the same elements is counted as one i.e. 1+2+3 is the same as 2+1+3
and 3+1+2, etc.


Input

You are given the number of test cases (1 <= T <= 20) in the first line. Then, in the following T lines, each line contains the number N (1 <= N <= 2,000).


Output

Print out, for each number N, the number of possible ways of writing that number in the form of sum of distinct numbers as described above. In order to limit the range of answers, the answer must be the result value modulo 100999.


Sample Input
4
5
6
10
200
Sample Output
3
4
10
50568

题意就是给出一个数N,求组成N的不同数有多少种方式。如样例5={1+4},{2+3},{5}就有三种。

解题思路:N分别减去j=[1,(n-1)/2],剩下的数m,看m除掉前j个组成的数还有多少种组成方式。直接从1递推到2000即可。由于时间复杂度=2000*1000*1000左右,可能会超时,所以在计算m的时候,可以用辅助数组g[i][j](表示当前i,前j项的和)来计算。这里有一点点小问题,仔细考虑清楚下。

比如N=10.N可以减去1,2,3,4.分别余下9,8,7,6.分别看当m=9的时候组成9的数不包含1有多少种,当m=8的时候组成数不包含1,2的有多少种,。。。然后这些种类的和就是N=10的结果。在处理m这些数据的时候,顺便把他们的结果也保存下来,让下一次计算准备调用。嗯,就这样了。还T了,Wr了这么多发,淡淡的忧伤。。。肯定是没睡好,嗯,继续补觉。。。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define eps 1e-8
#define M 100999
using namespace std;
int f[2010][2010];
int g[2010][2010];
int main()
{
	int i,n,j,T;
//	freopen("out1.txt","w",stdout);
	memset(f,0,sizeof(f));
	memset(g,0,sizeof(g));
	f[1][0]=1;
	for (i=2;i<=2010;i++)
	{
		f[i][0]=1;
		for (j=1;j<=(i-1)/2;j++)
		{
			LL s=f[i-j][0];
			g[i][0]=0;
			int jj=j;
			while (g[i-j][jj]==0 && g[i-j][jj+1]==0 && jj>0) jj--;
			s=(s+M-g[i-j][jj])%M;
			f[i][j]=s;
			g[i][j]=(g[i][j-1]%M+s%M)%M;
			f[i][0]=(f[i][0]+s)%M;
		}
	}
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d",&n);
		//cout<<n<<endl;
		printf("%d\n",f[n][0]);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
用C语言解决下列问题:Kirill wants to weave the very beautiful blanket consisting of n×m of the same size square patches of some colors. He matched some non-negative integer to each color. Thus, in our problem, the blanket can be considered a B matrix of size n×m consisting of non-negative integers. Kirill considers that the blanket is very beautiful, if for each submatrix A of size 4×4 of the matrix B is true: A11⊕A12⊕A21⊕A22=A33⊕A34⊕A43⊕A44, A13⊕A14⊕A23⊕A24=A31⊕A32⊕A41⊕A42, where ⊕ means bitwise exclusive OR Kirill asks you to help her weave a very beautiful blanket, and as colorful as possible! He gives you two integers n and m . Your task is to generate a matrix B of size n×m , which corresponds to a very beautiful blanket and in which the number of different numbers maximized. Input The first line of input data contains one integer number t (1≤t≤1000 ) — the number of test cases. The single line of each test case contains two integers n and m (4≤n,m≤200) — the size of matrix B . It is guaranteed that the sum of n⋅m does not exceed 2⋅105 . Output For each test case, in first line output one integer cnt (1≤cnt≤n⋅m) — the maximum number of different numbers in the matrix. Then output the matrix B (0≤Bij<263) of size n×m . If there are several correct matrices, it is allowed to output any one. It can be shown that if there exists a matrix with an optimal number of distinct numbers, then there exists among suitable matrices such a B that (0≤Bij<263) .
最新发布
03-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值