## 10625 - GNU = GNU'sNotUnix

**Moderator:** Board moderators

### 10625 - GNU = GNU'sNotUnix

I've been working on problem 10625, GNU's Not Unix.

I have tried my best to let my program passed by the judge, but always it says I've got the wrong answer. (Also put a 64 bit structure in cos the problem states that the output data will be contained in a 64-bit int). I wonder if there is some special case that I have not yet considered of. So can anyone set up some test data that can cover all aspects of the problem and the answers to them so I can find out where is wrong.

Thank you very much!

Liux

I have tried my best to let my program passed by the judge, but always it says I've got the wrong answer. (Also put a 64 bit structure in cos the problem states that the output data will be contained in a 64-bit int). I wonder if there is some special case that I have not yet considered of. So can anyone set up some test data that can cover all aspects of the problem and the answers to them so I can find out where is wrong.

Thank you very much!

Liux

### I get WA yet, but I know the Math

Hi!

I built a matrix m[33..126,33..126] where m[i,j] = amount of letters with ASCII code j in a rule, beginning with a letter with ASCII code i.

For example , if I had only 3 letters (A,B,C) and rules

A->ABC

B->BAAC

C->CB

then I would get matrix ((1 1 1)(2 1 1)(0 1 1)).

In each query I have a Word, a Letter and Number (of iterations).

I evaluate matr = m^Number (^ - power) in O(log Number) time, then I have matr[i,j] = amount of letters with ASCII code j that will be generated

if I would begin with letter ASCII i.

For example, input query is: ABC A 3

matr = m^3 = ((9 9 9)(12 12 12)(6 6 6))

Input string is ABC, Letter is A.

So the answer is matr[1,1] +matr[2,1]+matr[3,1] = 27.

By the way, I wrote in Pascal, and I used extended.

Is there in pascal some integer 64-bit type?

I built a matrix m[33..126,33..126] where m[i,j] = amount of letters with ASCII code j in a rule, beginning with a letter with ASCII code i.

For example , if I had only 3 letters (A,B,C) and rules

A->ABC

B->BAAC

C->CB

then I would get matrix ((1 1 1)(2 1 1)(0 1 1)).

In each query I have a Word, a Letter and Number (of iterations).

I evaluate matr = m^Number (^ - power) in O(log Number) time, then I have matr[i,j] = amount of letters with ASCII code j that will be generated

if I would begin with letter ASCII i.

For example, input query is: ABC A 3

matr = m^3 = ((9 9 9)(12 12 12)(6 6 6))

Input string is ABC, Letter is A.

So the answer is matr[1,1] +matr[2,1]+matr[3,1] = 27.

By the way, I wrote in Pascal, and I used extended.

Is there in pascal some integer 64-bit type?

### 10625 - Why WA?

program p10625;

const

MIN = 33;MAX = 126;

type

row = array[MIN..MAX] of extended;

var

TempM,m,m1:array[MIN..MAX] of row;

inp,i,j,k,tests,rules:integer;

number,Left,Right:integer;

ch,letter:char;

s:array[MIN..MAX] of integer;

res:extended;

procedure copy(var a,b:array of row);

var

i,j:integer;

begin

for i:=0 to MAX-MIN do

for j:=MIN to MAX do

b[i,j] := a[i,j];

end;

procedure mult(a,b:array of row; var res:array of row);

var

i,j,k:integer;

s:extended;

begin

for i:=0 to MAX-MIN do

for j:=MIN to MAX do

begin

s := 0;

for k:=MIN to MAX do

s := s + a[i,k]*b[k-MIN,j];

res[i,j] := s;

end;

end;

procedure MakeOne(var m:array of row);

var

i:integer;

begin

FillChar(m,sizeof(m),0);

for i:=0 to MAX - MIN do

m[i,i+MIN] := 1;

end;

function IsZeroRow(m:array of row;RowNumber:integer):boolean;

var

i:integer;

begin

IsZeroRow := True;

for i:=MIN to MAX do

if m[RowNumber,i] > 0.0 then

begin

IsZeroRow := True; Exit;

end;

end;

begin

readln(tests);

for i:=1 to tests do

begin

FillChar(TempM,sizeof(TempM),0);

readln(rules);

for j:=1 to rules do

begin

read(ch); Left := Ord(ch);read(ch);read(ch);

while not eoln do

begin

read(ch);

Right := Ord(ch); TempM[Left,Right] := TempM[Left,Right] + 1;

end;

readln;

end;

readln(inp);

for j:=1 to inp do

begin

copy(TempM,m);

for k:=MIN to MAX do

if IsZeroRow(m,k) then m[k,k] := 1;

FillChar(s,sizeof(s),0);

read(ch);

while ch <> ' ' do

begin

Inc(s[Ord(ch)]);

read(ch);

end;

read(letter);read(ch);

readln(number);

MakeOne(m1);

while number > 0 do

begin

if number mod 2 = 1 then mult(m,m1,m1);

number := number div 2;

mult(m,m,m);

end;

res := 0;

for k := MIN to MAX do

res := res + s[k]*m1[k,Ord(letter)];

writeln(res:0:0);

end;

end;

end.

const

MIN = 33;MAX = 126;

type

row = array[MIN..MAX] of extended;

var

TempM,m,m1:array[MIN..MAX] of row;

inp,i,j,k,tests,rules:integer;

number,Left,Right:integer;

ch,letter:char;

s:array[MIN..MAX] of integer;

res:extended;

procedure copy(var a,b:array of row);

var

i,j:integer;

begin

for i:=0 to MAX-MIN do

for j:=MIN to MAX do

b[i,j] := a[i,j];

end;

procedure mult(a,b:array of row; var res:array of row);

var

i,j,k:integer;

s:extended;

begin

for i:=0 to MAX-MIN do

for j:=MIN to MAX do

begin

s := 0;

for k:=MIN to MAX do

s := s + a[i,k]*b[k-MIN,j];

res[i,j] := s;

end;

end;

procedure MakeOne(var m:array of row);

var

i:integer;

begin

FillChar(m,sizeof(m),0);

for i:=0 to MAX - MIN do

m[i,i+MIN] := 1;

end;

function IsZeroRow(m:array of row;RowNumber:integer):boolean;

var

i:integer;

begin

IsZeroRow := True;

for i:=MIN to MAX do

if m[RowNumber,i] > 0.0 then

begin

IsZeroRow := True; Exit;

end;

end;

begin

readln(tests);

for i:=1 to tests do

begin

FillChar(TempM,sizeof(TempM),0);

readln(rules);

for j:=1 to rules do

begin

read(ch); Left := Ord(ch);read(ch);read(ch);

while not eoln do

begin

read(ch);

Right := Ord(ch); TempM[Left,Right] := TempM[Left,Right] + 1;

end;

readln;

end;

readln(inp);

for j:=1 to inp do

begin

copy(TempM,m);

for k:=MIN to MAX do

if IsZeroRow(m,k) then m[k,k] := 1;

FillChar(s,sizeof(s),0);

read(ch);

while ch <> ' ' do

begin

Inc(s[Ord(ch)]);

read(ch);

end;

read(letter);read(ch);

readln(number);

MakeOne(m1);

while number > 0 do

begin

if number mod 2 = 1 then mult(m,m1,m1);

number := number div 2;

mult(m,m,m);

end;

res := 0;

for k := MIN to MAX do

res := res + s[k]*m1[k,Ord(letter)];

writeln(res:0:0);

end;

end;

end.

Code: Select all

```
program p10625;
const
MIN = 33;MAX = 126;
type
row = array[MIN..MAX] of int64;
matrix = array[MIN..MAX] of row;
var
TempM,m,m1:matrix;
inp,i,j,k,tests,rules:integer;
number,Left,Right:integer;
ch,letter:char;
s:array[MIN..MAX] of integer;
res:int64;
procedure copy(var a,b:matrix);
var
i,j:integer;
begin
for i:=MIN to MAX do
for j:=MIN to MAX do
b[i,j] := a[i,j];
end;
procedure mult(a,b:matrix; var res:matrix);
var
i,j,k:integer;
s:int64;
begin
for i:=MIN to MAX do
for j:=MIN to MAX do
begin
s := 0;
for k:=MIN to MAX do
s := s + a[i,k]*b[k,j];
res[i,j] := s;
end;
end;
procedure MakeOne(var m:matrix);
var
i:integer;
begin
FillChar(m,sizeof(m),0);
for i:=MIN to MAX do
m[i,i] := 1;
end;
function IsZeroRow(m:matrix;RowNumber:integer):boolean;
var
i:integer;
begin
IsZeroRow := True;
for i:=MIN to MAX do
if m[RowNumber,i] > 0 then
IsZeroRow := False;
end;
begin
readln(tests);
for i:=1 to tests do
begin
FillChar(TempM,sizeof(TempM),0);
readln(rules);
for j:=1 to rules do
begin
read(ch); Left := Ord(ch);read(ch);read(ch);
while not eoln do
begin
read(ch);
Right := Ord(ch); TempM[Left,Right] := TempM[Left,Right] + 1;
end;
readln;
end;
for k:=MIN to MAX do
if IsZeroRow(TempM,k) then TempM[k,k] := 1;
readln(inp);
for j:=1 to inp do
begin
copy(TempM,m);
FillChar(s,sizeof(s),0);
read(ch);
while ch <> ' ' do
begin
Inc(s[Ord(ch)]);
read(ch);
end;
read(letter);read(ch);
readln(number);
MakeOne(m1);
while number > 0 do
begin
if number mod 2 = 1 then mult(m,m1,m1);
number := number div 2;
mult(m,m,m);
end;
res := 0;
for k := MIN to MAX do
res := res + m1[k,Ord(letter)]*s[k];
writeln(res);
end;
end;
end.
```

Code: Select all

```
1
1
A->AA
1
A A 62
```

### Re: I get WA yet, but I know the Math

Freepascal compiler supports the types int64 (64-bit signed int) and qword (64-bit unsigned), if I recall correctly.medv wrote:By the way, I wrote in Pascal, and I used extended.

Is there in pascal some integer 64-bit type?

### 10625 GNU = GNU'sNotUnix

Hello. I am trying to solve problem 10625 but i got wa a couple of times and cant figure out why. I use a simulating approach. I store the current number of each printable char and in every recursion step i add for every rule the number of the the char which i substitute to the number of the chars who take the place of it. I took care that i substitute all rules at one time and i use unsigned long long int.

Is there anything i havent took care or any tricky input, especialy in the input format?

Anyone have some tricky test cases?

Thank you in advance,

Hedge

Is there anything i havent took care or any tricky input, especialy in the input format?

Anyone have some tricky test cases?

Thank you in advance,

Hedge

Is there anything i havent took care or any tricky input, especialy in the input format?

Anyone have some tricky test cases?