From 7e42d6886e97915044f8cb2d9beafbd0e4b480d2 Mon Sep 17 00:00:00 2001 From: Titouan Rigoudy Date: Fri, 17 Oct 2014 22:55:37 -0400 Subject: [PATCH] C program looks like it works --- arithmetic_progressions/ap.test | 22 ++++++++ .../arithmetic_progressions.c | 51 +++++++++++++++++-- 2 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 arithmetic_progressions/ap.test diff --git a/arithmetic_progressions/ap.test b/arithmetic_progressions/ap.test new file mode 100644 index 0000000..b0e72aa --- /dev/null +++ b/arithmetic_progressions/ap.test @@ -0,0 +1,22 @@ +7 +0 0 0 +0 1 0 +1 1 0 +1 1 1 +2 1 1 +1 2 1 +1 2 2 +13 +0 1 1 +0 2 2 +0 3 3 +0 4 4 +0 5 5 +0 6 6 +0 7 7 +1 2 2 1 +0 2 2 +1 3 3 1 +0 3 3 +0 2 3 +0 2 4 \ No newline at end of file diff --git a/arithmetic_progressions/arithmetic_progressions.c b/arithmetic_progressions/arithmetic_progressions.c index f29b9e9..f76b2c0 100644 --- a/arithmetic_progressions/arithmetic_progressions.c +++ b/arithmetic_progressions/arithmetic_progressions.c @@ -5,6 +5,7 @@ #define INTS_START_SIZE 8 #define INTS_DELIM " \t\n" #define BUF_SIZE 1024 +#define BIG_MOD 1000003 size_t read_ints(char *str, int **ints) { if (str == NULL) { @@ -60,11 +61,56 @@ int read_adp(int n, int (*adp)[3]) { return 0; } +int pow_mod(int a, int b, int mod, int acc) { + int powa = a % mod; + while (b > 0) { + if (b & 1) { + acc = (acc * powa) % mod; + } + powa = (powa * powa) % mod; + b >>= 1; + } + return acc; +} + +int factorial_mod(int n, int mod, int acc) { + int i; + for (i = 2; i <= n; i++) { + acc = (acc * i) % mod; + } + return acc; +} + int min_const_diff(int n, int (*adp)[3], int i, int j) { + if (i < 1 || j > n) { + return 1; + } + int k = 0; + int v = 1; + int l; + for (l = i-1; l < j; l++) { + if (adp[l][2] > 0) { + k += adp[l][2]; + if (adp[l][1] == 0) { + v = pow_mod(adp[l][0], adp[l][2], BIG_MOD, v); + } else { + v = pow_mod(adp[l][1], adp[l][2], BIG_MOD, v); + } + } + } + v = factorial_mod(k, BIG_MOD, v); + printf("%d %d\n", k, v); return 0; } int add_powers(int n, int (*adp)[3], int i, int j, int v) { + if (i < 1 || j > n) { + return 1; + } + int k; + for (k = i-1; k < j; k++) { + adp[k][2] += v; + } return 0; } @@ -108,10 +154,6 @@ int main(int argc, char **argv) { if (err) { return 0; } - int i; - for (i = 0; i < n; i++) { - printf("%d %d %d\n", adp[i][0], adp[i][1], adp[i][2]); - } fgets(buf, BUF_SIZE, stdin); int q = strtol(buf, &end, 10); @@ -119,6 +161,7 @@ int main(int argc, char **argv) { return 0; } + int i; for (i = 0; i < q; i++) { fgets(buf, BUF_SIZE, stdin); err = handle_query(buf, n, adp);