POI 1997 n-k集合数

POI No Comments »65 views

动态规划,设F[i,j]为范围1..i的集合元素之和大于等于j,不含连续自然数的集合个数

由于可能存在空集的情况,即计算过程中出现了k=-1,这是很不方便的,所以我修改了定义,把大于k方便的改成了大于等于k+1。N=100,K=400时,结果是很大的,要使用高精度。

状态转移方程

F[i,j]= Sum
{
F[i-1,j], (包含第i个元素)
F[i-2,j-i] (不包含第i个元素,j-i若小于0则为F[i-2,0])
}

边界条件

F[1,0]=2 //Ø,{1}
F[1,1]=1 //{1}
F[2,0]=3 //Ø,{1},{2}
F[2,1]=2 //{1},{2}
F[2,2]=1 //{2}

目标结果

F[N,K+1]

Read the rest of this entry »

标签:, , , ,

USACO 4.3.1 Buy Low, Buy Lower 逢低吸纳 buylow

USACO No Comments »399 views

Read the rest of this entry »

标签:, ,

高精度计算对象

計算機科學 3 Comments »241 views

高精度加、减、单乘、双乘、比较大小、读入、输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <iostream>
 
using namespace std;
 
template<int LIM,int MAX> class hp
{
	public:
		//vars
		int sect[MAX];
		int scnt;
		//constructors
		hp()
		{
			scnt=1;
			sect[0]=0;
		}
		//functions
		void copy(const hp<LIM,MAX> &A)
		{
			for (int i=0;i<A.scnt;i++)
				sect[i]=A.sect[i];
			scnt=A.scnt;
		}
		void copy(int A)
		{
			scnt=0;
			while (A)
			{
				sect[scnt++]=A % LIM;
				A /=LIM;
			}
		}
		void print()
		{
			int i,k;
			printf("%d",sect[scnt-1]);
			for (i=scnt-2;i>=0;i--)
			{
				k=LIM/10;
				while(sect[i]<k)
				{
					printf("0");
					k/=10;
				}
				if (sect[i])
					printf("%d",sect[i]);
			}
		}
		void read(int LIE)
		{
			char A[LIM*MAX];
			int len,i,j,k,b=0;
			scanf("%s",A);
			len=strlen(A);
			k=len % LIE;
			j=scnt=len / LIE;
			if (k)
			{
				scnt++;
				j=scnt-1;
				sect[j]=0;
				for (i=0;i<k;i++)
				{
					sect[j]*=10;
					sect[j]+=A[b++]-'0';
				}
			}
			for (j--;j>=0;j--)
			{
				sect[j]=0;
				for (i=0;i<LIE;i++)
				{
					sect[j]*=10;
					sect[j]+=A[b++]-'0';
				}
			}
		}
		void plus(hp<LIM,MAX> &A,int offset=0)
		{
			int sc=scnt > A.scnt + offset  ? scnt : A.scnt + offset;
			int i,j,up=0;
			for (i=0;i<sc;i++)
			{
				j=i - offset;
				if (j<0) continue;
				if (i>=scnt) sect[i]=0;
				if (j>=A.scnt) A.sect[j]=0;
				sect[i]+=A.sect[j] + up;
				up=sect[i] / LIM;
				sect[i]%=LIM;
			}
			scnt=sc;
			if (up) sect[scnt++]=up;
		}
		void minus(hp<LIM,MAX> &A)
		{
			int sc=scnt;
			int i,lend=0;
			for (i=0;i<sc;i++)
			{
				if (i>=A.scnt) A.sect[i]=0;
				sect[i]-=A.sect[i] + lend;
				lend=0;
				if (sect[i]<0)
				{
					lend=1;
					sect[i]+=LIM;
				}
			}
			scnt=sc;
			if (lend) scnt--;
			for (;scnt>1 && sect[scnt-1]==0;scnt--);
 
		}
		void multiply(int p)
		{
			if (p==0)
			{
				scnt=1;
				sect[0]=0;
				return;
			}
			int sc=scnt;
			int i,up=0;
			for (i=0;i<sc;i++)
			{
				if (i>=scnt) sect[i]=0;
				sect[i]=sect[i] * p + up;
				up=sect[i] / LIM;
				sect[i]%=LIM;
			}
			scnt=sc;
			if (up) sect[scnt++]=up;
		}
		void multiply(hp<LIM,MAX> &A)
		{
			hp<LIM,MAX> T,C;
			int sc=A.scnt;
			int i;
			for (i=0;i<sc;i++)
			{
				T=(*this);
				T.multiply(A.sect[i]);
				C.plus(T,i);
			}
			scnt=sc;
			(*this)=C;
		}
		int compare(hp<LIM,MAX> &A)
		{
			if (scnt > A.scnt)
				return 1;
			if (scnt < A.scnt)
				return -1;
			for (int i=scnt-1;i>=0;i--)
			{
				if (sect[i] > A.sect[i])
					return 1;
				if (sect[i] < A.sect[i])
					return -1;
			}
			return 0;
		}
		//operators
		bool operator == (hp<LIM,MAX> &A)
		{
			int t=compare(A);
			return t==0;
		}
		bool operator < (hp<LIM,MAX> &A)
		{
			int t=compare(A);
			return t==-1;
		}
		bool operator > (hp<LIM,MAX> &A)
		{
			int t=compare(A);
			return t==1;
		}
		bool operator <= (hp<LIM,MAX> &A)
		{
			int t=compare(A);
			return t==-1 || t==0;
		}
		bool operator >= (hp<LIM,MAX> &A)
		{
			int t=compare(A);
			return t==1 || t==0;
		}
		void operator =(hp<LIM,MAX> A)
		{
			copy(A);
		}
		void operator =(int A)
		{
			copy(A);
		}
		void operator +=(hp<LIM,MAX> &A)
		{
			plus(A);
		}
		void operator -=(hp<LIM,MAX> &A)
		{
			minus(A);
		}
		void operator *=(hp<LIM,MAX> &A)
		{
			multiply(A);
		}
		void operator *=(int A)
		{
			multiply(A);
		}
		hp<LIM,MAX> operator +(hp<LIM,MAX> &A)
		{
			hp<LIM,MAX> C(*this);
			C.plus(A);
			return C;
		}
		hp<LIM,MAX> operator -(hp<LIM,MAX> &A)
		{
			hp<LIM,MAX> C(*this);
			C.minus(A);
			return C;
		}
		hp<LIM,MAX> operator *(int A)
		{
			hp<LIM,MAX> C(*this);
			C.multiply(A);
			return C;
		}
		hp<LIM,MAX> operator *(hp<LIM,MAX> &A)
		{
			hp<LIM,MAX> C(*this);
			C.multiply(A);
			return C;
		}
};
 
typedef hp<10000,100> hpnum;
 
hpnum A,B;
 
hpnum qp(int P)
{
	hpnum A;
	if (P/2==1)
		A=2;
	else
		A=qp(P/2);
	A*=A;
	if (P%2==1)
		A*=2;
	return A;
}
 
void mason()
{
	int P;
	B=1;
	freopen("mason.in","r",stdin);
	freopen("mason.out","w",stdout);
	scanf("%d",&P);
	A=qp(P);
	A-=B;
	A.print();
}
 
void mul()
{
	char c;
	hpnum A,B,C;
	freopen("mul.in","r",stdin);
	freopen("mul.out","w",stdout);
	A.read(4);
	while ((c=getchar())==10 || c==13);
	ungetc(c,stdin);
	B.read(4);
	C=A*B;
	C+=B;
	C.print();
}
 
int main()
{
	//mason();麦森数
	//mul();高精度乘法例子
	return 0;
}
标签:, , ,
25 queries. 0.651 seconds. Designed by NattyWP .
Images by desEXign.