1 | /* |
---|
2 | * Copyright (c) 2011-2012, Attila Gobi |
---|
3 | * All rights reserved. |
---|
4 | * |
---|
5 | * This software was developed by Attila Gobi. |
---|
6 | * The project was supported by the European Union and co-financed by the |
---|
7 | * European Social Fund (grant agreement no. TAMOP 4.2.1./B-09/1/KMR-2010-0003) |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions are met: |
---|
11 | * 1. Redistributions of source code must retain the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer. |
---|
13 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
14 | * notice, this list of conditions and the following disclaimer in the |
---|
15 | * documentation and/or other materials provided with the distribution. |
---|
16 | * |
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS "AS IS" AND ANY |
---|
18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
---|
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
---|
23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
---|
24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
---|
25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
27 | */ |
---|
28 | |
---|
29 | #include "stream.h" |
---|
30 | #include <unordered_map> |
---|
31 | #include <map> |
---|
32 | #include <cstdlib> |
---|
33 | #include <sys/time.h> |
---|
34 | |
---|
35 | long timeval_diff(struct timeval &tv1, struct timeval &tv2) |
---|
36 | { |
---|
37 | long ret = tv1.tv_usec - tv2.tv_usec; |
---|
38 | if(ret<0) ret += 1000000; |
---|
39 | return ret; |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | long change(int n, int max=50) { |
---|
44 | if(n<0) |
---|
45 | return 0; |
---|
46 | |
---|
47 | if(n==0) |
---|
48 | return 1; |
---|
49 | |
---|
50 | long ret = 0; |
---|
51 | if(n>=1 && max>=1) ret+=change(n-1, 1); |
---|
52 | if(n>=2 && max>=2) ret+=change(n-2, 2); |
---|
53 | if(n>=5 && max>=5) ret+=change(n-5, 5); |
---|
54 | if(n>=10 && max>=10) ret+=change(n-10, 10); |
---|
55 | if(n>=20 && max>=20) ret+=change(n-20, 20); |
---|
56 | if(n>=50 && max>=50) ret+=change(n-50, 50); |
---|
57 | return ret; |
---|
58 | } |
---|
59 | |
---|
60 | long changef(int n, int max=4) { |
---|
61 | if(n<0) |
---|
62 | return 0; |
---|
63 | |
---|
64 | long ret = 1; |
---|
65 | switch(max) { |
---|
66 | case 4: ret += changef(n-50, 4); |
---|
67 | case 3: ret += changef(n-20, 3); |
---|
68 | case 2: ret += changef(n-10, 2); |
---|
69 | case 1: ret += changef(n-5, 1); |
---|
70 | case 0: ret += changef(n-2, 0); |
---|
71 | } |
---|
72 | return ret; |
---|
73 | } |
---|
74 | |
---|
75 | long changedyn0(int n, int max=4) { |
---|
76 | if(n<0) |
---|
77 | return 0; |
---|
78 | |
---|
79 | if(max<0) |
---|
80 | return 1; |
---|
81 | |
---|
82 | static std::map<int,long> cache[5]; |
---|
83 | std::map<int,long>::iterator it = cache[max].find(n); |
---|
84 | if(it != cache[max].end()) |
---|
85 | return cache[max][n]; |
---|
86 | |
---|
87 | long ret = 1; |
---|
88 | switch(max) { |
---|
89 | case 4: ret += changedyn0(n-50, 4); |
---|
90 | case 3: ret += changedyn0(n-20, 3); |
---|
91 | case 2: ret += changedyn0(n-10, 2); |
---|
92 | case 1: ret += changedyn0(n-5, 1); |
---|
93 | case 0: ret += changedyn0(n-2, 0); |
---|
94 | } |
---|
95 | cache[max][n] = ret; |
---|
96 | return ret; |
---|
97 | } |
---|
98 | |
---|
99 | long changedyn(int n, int max=4) { |
---|
100 | if(n<0) |
---|
101 | return 0; |
---|
102 | |
---|
103 | if(max<0) |
---|
104 | return 1; |
---|
105 | |
---|
106 | static std::unordered_map<int,long> cache[5]; |
---|
107 | std::unordered_map<int,long>::iterator it = cache[max].find(n); |
---|
108 | if(it != cache[max].end()) |
---|
109 | return cache[max][n]; |
---|
110 | |
---|
111 | long ret = 1; |
---|
112 | switch(max) { |
---|
113 | case 4: ret += changedyn(n-50, 4); |
---|
114 | case 3: ret += changedyn(n-20, 3); |
---|
115 | case 2: ret += changedyn(n-10, 2); |
---|
116 | case 1: ret += changedyn(n-5, 1); |
---|
117 | case 0: ret += changedyn(n-2, 0); |
---|
118 | } |
---|
119 | cache[max][n] = ret; |
---|
120 | return ret; |
---|
121 | } |
---|
122 | |
---|
123 | template<typename ST> |
---|
124 | stream<long> times(long n, long val, ST && s) |
---|
125 | { |
---|
126 | if(n==0) return s; |
---|
127 | return times(n-1, val, val<<=std::forward<ST>(s)); |
---|
128 | } |
---|
129 | |
---|
130 | stream<long> change1 = 1l<<=change1; |
---|
131 | stream<long> change2 = times(2, 0, change2) + change1; |
---|
132 | stream<long> change5 = times(5, 0, change5) + change2; |
---|
133 | stream<long> change10 = times(10, 0, change10) + change5; |
---|
134 | stream<long> change20 = times(20, 0, change20) + change10; |
---|
135 | stream<long> change50 = times(50, 0, change50) + change20; |
---|
136 | |
---|
137 | int main(int, char *argv[]) { |
---|
138 | // stream<long>::iterator it = change50.begin(); |
---|
139 | // for(int i=0; i<20; ++i) |
---|
140 | // { |
---|
141 | // assert(change(i) == *it); |
---|
142 | // std::cout<<i<<" "<<change(i)<<" "<<changef(i)<<" "<<changedyn(i)<<" "<<*it<<std::endl; |
---|
143 | // ++it; |
---|
144 | // } |
---|
145 | |
---|
146 | //std::cout<<j<<" "<<change(j)<<std::endl; |
---|
147 | //std::cout<<j<<" "<<changef(j)<<std::endl; |
---|
148 | |
---|
149 | const int n = atoi(argv[2]); |
---|
150 | const int x = atoi(argv[1]); |
---|
151 | |
---|
152 | struct timeval tv1, tv2; |
---|
153 | struct timezone tz; |
---|
154 | switch(x) { |
---|
155 | case 1: |
---|
156 | gettimeofday(&tv1, &tz); |
---|
157 | changedyn0(n); |
---|
158 | gettimeofday(&tv2, &tz); |
---|
159 | break; |
---|
160 | |
---|
161 | case 2: |
---|
162 | gettimeofday(&tv1, &tz); |
---|
163 | changedyn(n); |
---|
164 | gettimeofday(&tv2, &tz); |
---|
165 | break; |
---|
166 | |
---|
167 | case 3: |
---|
168 | gettimeofday(&tv1, &tz); |
---|
169 | stream<long>::iterator it = change50.begin(); |
---|
170 | for(int i=0; i<n; ++i) ++it; |
---|
171 | *it; |
---|
172 | gettimeofday(&tv2, &tz); |
---|
173 | break; |
---|
174 | } |
---|
175 | std::cout<<timeval_diff(tv2, tv1)<<std::endl; |
---|
176 | } |
---|