結果

問題 No.16 累乗の加算
コンテスト
ユーザー mudbdb
提出日時 2015-07-12 09:17:14
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,099 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 211 ms
コンパイル使用メモリ 39,540 KB
最終ジャッジ日時 2026-02-23 18:41:58
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <stdio.h>
#include <stdlib.h>
int main()
{
  int X;
  int N;
  int *A;
  scanf("%d", &X);
  scanf("%d", &N);
  A = (int*)malloc(sizeof(int)*N);
  int i;
  for (i=0; i<N; i++) scanf("%d", &(A[i]));

  int mod = 1000003;

// 2^26 =   67108864
// A[i] <= 100000000
// 2^27 =  134217728
// 
//   A[i] =     2^26  *bit[26] +...+    2^0  *bit[0]
// X^A[i] =  X^(2^26  *bit[26])*...* X^(2^0  *bit[0])
//        = (X^(2^26))^bit[26] *...*(X^(2^0))^bit[0]

  int table_n = 27;
  long long int *table = (long long*)malloc(sizeof(long long int)*table_n);
  table[0] = (X % mod);
  for (i=1; i<table_n; i++) {
    table[i] = ((table[i-1]*table[i-1]) % mod);
  }

  int j;
  long long int total;
  long long int item;
  if (X == 1) {
    total = N;
    total %= mod;
  } else {
    total = 0;
    for (i=0; i<N; i++) {
      item = 1;
      if (A[i] != 0) {
        for (j=0; j<table_n; j++) {
          if (A[i] & (1 << j)) {
            item *= table[j];
            item %= mod;
          }
        }
      }
      total += item;
      total %= mod;
    }
  }

  printf("%d\n", total);
  return 0;
}
0