Pagini recente » Cod sursa (job #1648048) | Cod sursa (job #2264712) | Cod sursa (job #593279) | Cod sursa (job #1585596) | Cod sursa (job #2545076)
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fin, *fout;
fin = fopen ( "matrice5.in", "r" );
fout = fopen ( "matrice5.out", "w" );
int t, n, m, p, k, x, j, cnt, y, cnt1, x1, y1, cnt2;
fscanf ( fin, "%d", &t );
int i;
for ( i = 0; i < t; i ++ ) {
fscanf ( fin, "%d%d%d%d", &n, &m, &p, &k );
x = k * p;
cnt = 1;
while ( 2 * cnt <= (n-1)*(m-1) ) {
x = x * x;
x %= 10007;
cnt *= 2;
}
cnt1 = 1;
x1 = k * p;
while ( (2 * cnt1) + cnt <= (n-1)*(m-1) ) {
x1 = x1 * x1;
x1 %= 10007;
cnt1 *= 2;
}
for ( j = cnt + cnt1 - 1; j < (n-1)*(m-1); j ++ ) {
x1 *= k;
x1 *= p;
x1 %= 10007;
}
y = p;
cnt = 1;
while ( 2 * cnt <= n+m-1 ) {
y = y * y;
y %= 10007;
cnt *= 2;
}
cnt2 = 1;
y1 = p;
while ( (cnt2 * 2) + cnt <= n+m-1 ) {
y1 = y1 * y1;
y1 %= 10007;
cnt2 *= 2;
}
for ( j = cnt + cnt2 - 1; j < n+m-1; j ++ ) {
y1 = y1 * p;
y1 %= 10007;
}
x = ( x * y ) % 10007;
x = ( x * x1 / k / p ) % 10007;
x = ( x * y1 / p ) % 10007;
fprintf ( fout, "%d\n", x );
}
return 0;
}