The problem
What I am trying to do is, if I have a vector of 27 (not 32!) int8_t:
x = {0,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}
I want to first cyclic-shift it to the right by n (not a constant), e.g. if n=1:
x2 = {26,0,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}
Then this vector is used to perform some very complex calculation, but for simplicity, let's assume that the next step is just to cyclic-shift it back to the left by n, and store it to the memory. So I should have a new vector of 27 int8_t:
y = {0,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}
So there're thousands of such vectors and performance is very critical here. The CPU that we're using has AVX2 support so we want to use it to speed things up.
My current solution
To get x2, I use two _mm256_loadu_si256() with a _mm256_blendv_epi8():
int8_t x[31+27+31];
for(int i=0; i<27; i++){
x[31+i] = i;
}
__m256i mask = _mm256_set_epi32 (0x0, 0x00800000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0);
__m256i x_second_part = _mm256_loadu_si256((__m256i*)(x+31+1)); //{1,2,...,26}
__m256i x_first_part = _mm256_loadu_si256((__m256i*)(x+31-26)); //{0}
__m256i x2 = _mm256_blendv_epi8(x_second_part, x_first_part, mask); //{1,2,...,26, 0}
int8_t y[31+27+31];
_mm256_storeu_si256((__m256i*)(y+31-26), x2);
_mm256_storeu_si256((__m256i*)(y+31+1), x2);
The reason why the x and y are declared to be of size [31+27+31] is that in this case _mm256_loadu_si256() and _mm256_storeu_si256() won't cause segfault.
And I can get the value of y by:
for(int i=0; i<27; i++){
cout << (int)y[31+i] << ' ';
}
The new problem
Unfortunately all the vectors must be continuous in memory, for example, if there are totally two vectors that need to be processed:
x = {[ 0, 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]};
Then I cannot just use _mm256_storeu_si256() to put the value of y back to memory because when the value of the second vector is written to memory it will overwrite some values of the first vector:
int8_t x[31+27+27+31];
int8_t y[31+27+27+31];
for(int i=0; i<27*2; i++){
x[31+i] = i;
}
for(int i=0; i<2; i++){
__m256i mask = _mm256_set_epi32 (0x0, 0x00800000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0);
__m256i x_second_part = _mm256_loadu_si256((__m256i*)(x+31+27*i+1)); //{1,2,...,26}
__m256i x_first_part = _mm256_loadu_si256((__m256i*)(x+31+27*i-26)); //{0}
__m256i x2 = _mm256_blendv_epi8(x_second_part, x_first_part, mask); //{1,2,...,26, 0}
_mm256_storeu_si256((__m256i*)(y+31+27*i-26), x2);
_mm256_storeu_si256((__m256i*)(y+31+27*i+1), x2);
}
for(int i=0; i<27; i++){
cout << (int)y[31+i] << ' ';
}cout << endl;
for(int i=0; i<27; i++){
cout << (int)y[31+27+i] << ' ';
}cout << endl;
will output
0 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
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
instead of
0 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
So I was thinking of using maskstore. But in the Intel Intrinsic Guide I couldn't find _mm256_maskstore_epi8. This leads me back to the topic:
How to do _mm256_maskstore_epi8() in C/C++?
There is another implementation of cyclic-shift inside 27-bytes vector with using AVX2:
#include <iostream>
#include <immintrin.h>
const __m256i K0 = _mm256_setr_epi8(
0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0);
const __m256i K1 = _mm256_setr_epi8(
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70);
inline const __m256i Shuffle(const __m256i & value, const __m256i & shuffle)
{
return _mm256_or_si256(_mm256_shuffle_epi8(value, _mm256_add_epi8(shuffle, K0)),
_mm256_shuffle_epi8(_mm256_permute4x64_epi64(value, 0x4E), _mm256_add_epi8(shuffle, K1)));
}
__m256i shuffles[27];
void Init()
{
uint8_t * p = (uint8_t *)shuffles;
for (int s = 0; s < 27; ++s)
for (int i = 0; i < 32; ++i)
p[s*32 + i] = i < 27 ? (27 + i - s)%27 : i;
}
void CyclicShift27(const uint8_t * src, size_t shift, uint8_t * dst)
{
_mm256_storeu_si256((__m256i*)dst, Shuffle(_mm256_loadu_si256((__m256i*)src), shuffles[shift]));
}
int main()
{
Init();
uint8_t src[32] = { 0,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 }, dst[32];
for (int j = 0; j < 27; ++j)
{
CyclicShift27(src, j, dst);
std::cout << "\t";
for (int i = 0; i < 32; i++)
std::cout << (int)dst[i] << ' ';
std::cout << std::endl;
}
return 0;
}
Output:
0 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
26 0 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 27 28 29 30 31
25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 27 28 29 30 31
24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 27 28 29 30 31
23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 27 28 29 30 31
22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 27 28 29 30 31
21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 27 28 29 30 31
20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 27 28 29 30 31
19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 27 28 29 30 31
18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 27 28 29 30 31
17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 27 28 29 30 31
16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 27 28 29 30 31
15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 27 28 29 30 31
14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 27 28 29 30 31
13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 27 28 29 30 31
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 27 28 29 30 31
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 27 28 29 30 31
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 27 28 29 30 31
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 27 28 29 30 31
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 27 28 29 30 31
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 27 28 29 30 31
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 27 28 29 30 31
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 27 28 29 30 31
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 27 28 29 30 31
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 27 28 29 30 31
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 0 1 27 28 29 30 31
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 0 27 28 29 30 31
It looks more simple than my previous answer.
I have made an implementation of cyclic-shift inside 27-bytes vector with using of SSSE3:
#include <iostream>
#include <tmmintrin.h>
union Shuffle
{
uint8_t s[64];
__m128i v[4];
};
Shuffle shuffles[27];
int Shift(int value)
{
return (value >= 0 && value < 16) ? value : -1;
}
void Init()
{
for (int s = 0; s < 27; ++s)
{
for (int i = 0; i < 16; ++i)
{
shuffles[s].s[0 + i] = s < 16 ? Shift(i - s) : Shift(i - s + 27);
shuffles[s].s[16 + i] = Shift(16 + i - s);
shuffles[s].s[32 + i] = Shift(11 + i - s);
shuffles[s].s[48 + i] = s < 11 ? Shift(i - s) : Shift(i - s + 27);
}
}
}
void CyclicShift27(const uint8_t * src, size_t shift, uint8_t * dst)
{
__m128i srcLo = _mm_loadu_si128((__m128i*)(src + 0));
__m128i srcHi = _mm_loadu_si128((__m128i*)(src + 11));
__m128i dstLo = _mm_or_si128(_mm_shuffle_epi8(srcLo, shuffles[shift].v[0]), _mm_shuffle_epi8(srcHi, shuffles[shift].v[1]));
__m128i dstHi = _mm_or_si128(_mm_shuffle_epi8(srcLo, shuffles[shift].v[2]), _mm_shuffle_epi8(srcHi, shuffles[shift].v[3]));
_mm_storeu_si128((__m128i*)(dst + 0), dstLo);
_mm_storeu_si128((__m128i*)(dst + 11), dstHi);
}
int main()
{
Init();
uint8_t src[27] = { 0,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 }, dst[27];
for (int j = 0; j < 27; ++j)
{
CyclicShift27(src, j, dst);
for (int i = 0; i < 27; i++)
std::cout << (int)dst[i] << ' ';
std::cout << std::endl;
}
return 0;
}
It output:
0 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
26 0 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
25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13
13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2
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 0 1
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 0
I hope it will be useful.
Related
I have two sheets in an Excel workbook. I need the formula which creates tables by using vlookup.
I have 10 columns and 10 rows like this
1 2 3 4 5 6 7 8 9 10
2
3
4
5
6
7
8
9
10
I have tried to use Vlookup with sum but not get the actual results.
The expected result should be like this
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 42 48 56 64 72 80
9 18 27 36 49 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
I have a QTextStream which contain:
Line1: 3 5 7 17 19 23 25
Line2: 3 5 7 17 19 23 26
Line3: 3 5 8 17 19 23 27
Line4: 3 5 9 17 21 35 37
Line5: 3 5 10 17 21 35 38
Line6: 3 5 11 17 21 35 39
Line7: 3 5 12 17 21 36 37
Line8: 3 5 13 17 21 37 38
Line9: 3 5 15 17 21 36 39
Line10: 3 5 16 17 21 37 38
I need to create regular expression to select only lines which contain numbers 3, 17 and 37.
Line4, 7, 8 and 10.
How to setup pattern?
Assuming they are ordered, you might use \b3\b.*\b17\b.*\b37\b.
So in C++, with raw string R"(\b3\b.*\b17\b.*\b37\b)".
Hi so I have created the nested list/matrix:
q)m:((1 2 3);(4 5 6);(7 8 9))
q)m
1 2 3
4 5 6
7 8 9
I have also identified the middle column in the list:
q)a:m[0;1],m[1;1],m[2;1]
I now want to replace the middle row (4 5 6) with a to finish with m looking like:
q)m
1 2 3
2 5 8
7 8 9
You've already seen you can index into the matrix with syntax like m[0;1], where 0 refers to the first level of nesting and 1 refers to the second level
KDB also allows you to assign to an index of a list in a similar way e.g.
q)l:1 2 3 4
q)l[1]:20
q)l
1 20 3 4
So you can use something similar in this example:
q)m[1]:a
q)m
1 2 3
2 5 8
7 8 9
As an aside, KDB also allows you to leave out an index, in which case it will take all items from the corresponding level of nesting, e.g.
q)m[0] /first level of nesting i.e. first row
1 2 3
q)m[;0] /second level of nesting i.e. first column
1 4 7
Hope that helps
Jonathon McMurray
AquaQ Analytics
You want to generalise for larger matrices (which must also be square) so your answer needs two parts:
how to construct a
how to insert it
for row/col x where x<count m.
The general expression you want is simply m[x;]:m[;x], because m[x;] denotes row x and m[;x] denotes column x.
See Q for Mortals 3.11.3 Two- and Three-Dimensional Matrices
You can make this a function of the index and the matrix:
q)show m:5 5#1_til 26
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
q){y[x;]:y[;x];:y}[3;m]
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
4 9 14 19 24
21 22 23 24 25
Just adding another approach for you.
q)m:8 cut til 64
q)0 0+\:til 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
q)(m)./:flip 0 0+\:til 8
0 9 18 27 36 45 54 63
q)#[m;4;:;(m)./:flip 0 0+\:til 8]
0 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
0 9 18 27 36 45 54 63
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
q)
For fun, here it is in a function which takes the length&width of the matrix and replaces the 'middle' row with the diagonal values
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}8
0 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
0 9 18 27 36 45 54 63
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}5
0 1 2 3 4
5 6 7 8 9
0 6 12 18 24
15 16 17 18 19
20 21 22 23 24
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}4
0 1 2 3
4 5 6 7
0 5 10 15
12 13 14 15
q)
q)#[((1 2 3);(4 5 6);(7 8 9));1;:;(2;5;8)]
1 2 3
2 5 8
7 8 9
Indexing in q can be straight forward and I believe a intermediate can be omitted:
q)m:((1 2 3);(4 5 6);(7 8 9))
q)m[1]:m[;1]
q)m
1 2 3
2 5 8
7 8 9
OK, so here is what i got going on. I use the latest version of Clion by JetBrains. I run the MinGW GUI as a compiler and use the bundled CMake that comes with the GUI.
So here is my code.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 30; i++) {
for (int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << " LINE: " << i;
cout << endl;
}
return 0;
}
Very simple and this is what it should output
1 LINE: 1
1 2 LINE: 2
1 2 3 LINE: 3
1 2 3 4 LINE: 4
And it continues up until 30.
I know that the actual code is correct because when I run it on a different compiler (On an Ubuntu VM) it comes out perfectly. But when I run it on Clion this is the output:
1 LINE: 1
1 2 LINE: 2
1 2 3 LINE: 3
1 2 3 4 LINE: 4
1 2 3 4 5 LINE: 5
1 2 3 4 5 6 LINE: 6
1 2 3 4 5 6 7 LINE: 7
1 2 3 4 5 6 7 8 LINE: 8
1 2 3 4 5 6 7 8 9 LINE: 9
1 2 3 4 5 6 7 8 9 10 LINE: 10
1 2 3 4 5 6 7 8 9 10 11 LINE: 11
1 2 3 4 5 6 7 8 9 10 11 12 LINE: 12
1 2 3 4 5 6 7 8 9 10 11 12 13 LINE: 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 LINE: 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 LINE: 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 LINE: 16
1 2 3 4 5 6 71 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 LINE: 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 LINE: 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 LINE: 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 LINE: 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 LINE: 21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 LINE: 22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 LINE: 23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 LINE: 24
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 LINE: 25
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 LINE: 26
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 LINE: 27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 181 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 LINE: 28
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 LINE: 29
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 LINE: 30
Ya, so that clearly doesn't work. And and to solidify the fact that its not my code, when I run it again...
1 LINE: 1
1 2 LINE: 2
1 2 3 LINE: 3
1 2 3 4 LINE: 4
1 2 3 4 5 LINE: 5
1 2 3 4 51 2 3 4 5 6 LINE: 6
1 2 3 4 5 6 7 LINE: 7
1 2 3 4 5 6 7 8 LINE: 8
1 2 3 4 5 6 7 8 9 LINE: 9
1 2 3 4 5 6 7 8 9 10 LINE: 10
1 2 3 4 5 6 7 8 9 10 11 LINE: 11
1 2 3 4 5 6 7 8 9 10 11 12 LINE: 12
1 2 3 4 5 6 7 8 9 10 11 12 13 LINE: 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 LINE: 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 LINE: 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 LINE: 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 LINE: 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 LINE: 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 LINE: 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 LINE: 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 LINE: 21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 LINE: 22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 LINE: 23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 LINE: 24
1 2 3 4 5 6 71 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 LINE: 25
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 LINE: 26
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 LINE: 27
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 LINE: 28
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 LINE: 29
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 LINE: 30
And if ran it again it would once again be different. It never the same and is always doing the same thing more or less.
This issue only seems to exist when I run programs that spit out long cout's. For example the HelloWorld runs fine. And as the last kick in the nuts, I ran it on my other computer (same setup with Clion and MinGW) and the exact same thing was happening.
I know that this question has been asked but i cant understand the problem in my code. I know that we have to use cost in desending order for minimum cost and i did same but still gives wrong output.
A board composed of m×n wooden squares and asks him to find the minimum cost of breaking the board back down into individual 1×1 pieces. To break the board down, Bob must make cuts along its horizontal and vertical lines.
To reduce the board to squares, xn−1 vertical cuts must be made at locations x1,x2,…,xn−2,xn−1 and ym−1 horizontal cuts must be made at locations y1,y2,…,ym−2,ym−1. Each cut along some xi (or yj) has a cost, cxi (or cyj). If a cut of cost c passes through n already-cut segments, the total cost of the cut is n×c.
The cost of cutting the whole board down into 1×1 squares is the sum of the cost of each successive cut. Recall that the cost of a cut is multiplied by the number of already-cut segments it crosses through, so each cut is increasingly expensive.
Input Format
The first line contains a single integer, T, denoting the number of test cases. The subsequent 3T lines describe each test case in 3 lines.
For each test case, the first line has two positive space-separated integers, m and n, detailing the respective height (y) and width (x) of the board.
The second line has m−1 space-separated integers listing the cost, cyj, of cutting a segment of the board at each respective location from y1,y2,…,ym−2,ym−1.
The third line has n−1 space-separated integers listing the cost, cxi, of cutting a segment of the board at each respective location from x1,x2,…,xn−2,xn−1.
Note: If we were to superimpose the m×n board on a 2D graph, x0, xn, y0, and yn would all be edges of the board and thus not valid cut lines.
Constraints
1≤T≤20
2≤m,n≤1000000
,0≤cxi,cyj≤1000000000
Output Format
For each of the T test cases, find the minimum cost (MinimumCost) of cutting the board into 1×1 squares and print the value of MinimumCost % (1000000000+7).
#include <iostream>
#include <limits>
using namespace std;
int main() {
int t,ch=0;
long int pos,m,n,h=1,l=1;
long long int cost=0,*x,*y,temp;
cin>>t;
while(t>0)
{cin>>m>>n;
cost=0;
x = new long long int[n-1];
y = new long long int[m-1];
for (long i=0;i<m-1;i++)
cin>>y[i];
for(long i=0;i<n-1;i++)
cin>>x[i];
h=1;
l=1;
while((h!=m)|(l!=n))
{ch=0;
temp=0;
for (long i=0;i<m-1;i++)
if (temp<y[i])
{temp=y[i];
pos=i;
}
for(long i=0;i<n-1;i++)
if (temp<x[i])
{temp=x[i];
pos=i;
ch=1;
}
cost=cost+temp*(ch==0?l:h);
if (ch==0)
{y[pos]=-1;
h++;}
else
{x[pos]=-1;
l++;
}
}
cout<<cost%1000000007;
t--;
}
return 0;
}
Test case that gives wrong output:
Input:
5
52 30
2 30 79 47 4 56 47 67 25 30 75 58 47 54 66 61 6 64 28 41 75 36 1 92 42 61 35 56 12 86 84 14 68 63 13 72 19 60 39 96 43 14 55 42 21 73 3 27 37 84 68
64 72 21 56 14 35 44 71 47 82 7 14 50 71 79 23 42 92 14 39 35 81 46 29 2 19 84 81 57
23 43
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
60 76
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
58 40
71 58 61 51 33 3 43 48 94 30 29 40 59 83 12 43 64 69 64 65 42 57 40 72 64 98 98 47 56 6 85 79 65 46 30 98 49 25 98 96 7 27 88 66 10 0 62 26 69 78 92 64 87 84 88 51 35
87 50 91 45 35 22 62 81 53 61 83 30 59 31 38 39 19 56 1 20 70 28 41 48 72 57 35 56 46 39 91 85 41 34 30 77 57 93 10
47 94
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17
Output
51028
1912
33638
91124
27525
This line could be a contributing factor:
while((h!=m)|(l!=n))
The | operator is a binary arithmetic operator.
Maybe you were looking for ||, which is the logical OR operator:
while((h!=m) || (l!=n))