結果

問題 No.2947 Sing a Song
ユーザー tnakao0123
提出日時 2024-11-02 13:06:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 1,381 bytes
コンパイル時間 400 ms
コンパイル使用メモリ 42,124 KB
最終ジャッジ日時 2025-02-25 02:04:23
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:55:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   55 |   scanf("%d%s%s", &n, s, t);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~
main.cpp:56:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   56 |   for (int i = 0; i < n; i++) scanf("%d", as + i);
      |                               ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2947.cc:  No.2947 Sing a Song - yukicoder
 */

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_N = 200000;
const int MAX_L = 200000;

/* typedef */

using ll = long long;

/* global variables */

char s[MAX_L + 4], t[MAX_L + 4];
int as[MAX_N];

/* subroutines */

// exgcd:
//   For given x>0 and y>0, solve a, b, c such that ax+by=c (=gcd(x,y))

template <typename T>
void exgcd(T x, T y, T &a, T &b, T &c) {
  T r0 = x, r1 = y;
  T a0 = 1, a1 = 0;
  T b0 = 0, b1 = 1;

  while (r1 > 0) {
    T q1 = r0 / r1;
    T r2 = r0 % r1;
    T a2 = a0 - q1 * a1;
    T b2 = b0 - q1 * b1;
    r0 = r1, r1 = r2;
    a0 = a1, a1 = a2;
    b0 = b1, b1 = b2;
  }
  c = r0;
  a = a0;
  b = b0;
}

/* main */

int main() {
  int n;
  scanf("%d%s%s", &n, s, t);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  int x = strlen(s), y = strlen(t), a, b, g;
  exgcd(x, y, a, b, g);
  //printf(" %d*%d+%d*%d=%d\n", x, a, y, b, g);
  int da = y / g, db = x / g;

  for (int i = 0; i < n; i++) {
    int d = as[i] / g;
    int sa = a * d, tb = b * d;
    while (tb < 0) tb += db, sa -= da;
    while (tb >= db) tb -= db, sa += da;
    //printf(" %d,%d\n", sa, tb);

    for (int j = 0; j < sa; j++) printf("%s ", s);
    for (int j = 0; j < tb; j++) printf("%s ", t);
    putchar('\n');
  }
  
  return 0;
}
0