This problem is the extreme version of Fizz Buzz. You are given a list of K distinct integers A and a list of K characters S. Both lists are numbered from 1 to K. The integer Ai corresponds to the character Si.
Denote B as the subset of A. There will be a rule for each possible subset: if an integer x is only divisible by all elements in B, print a string which is the concatenation of the corresponding character of each element in B, sorted by the index in ascending order. However, if B is an empty subset, print the first digit of x instead.
Does it look complicated? Do not worry, as it is just a generalization of the regular Fizz Buzz. For example, if A = [3, 5] and S = [F, B], you will get the normal Fizz Buzz problem. You can also check the Sample Test Cases for further understanding.
Your task is simple. Given the array A and S, with all rules from the subset of A, print the Extreme Fizz Buzz from 1 to N.
Format Input
The first line consists of 2 integers N and K.
The second line consists of a string S which has exactly K lower-case latin alphabet
characters.
The third line consists of K integers A1, A2, . . . , AK.
Format Output
Output N lines. The i-th line is the Extreme Fizz Buzz of i, where i is integer from 1 to N.
Constraints
• 1 ≤ N, K ≤ 100000
• 1 ≤ Ai ≤ 100000
• It is guaranteed that the integers in A are distinct
• It is guaranteed that the characters in S are upper-case latin alphabet characters
Sample Input & Output 1 (Standard Input And Output)
15 2
FB
3 5
1
2
F
4
B
F
7
8
F
B
1
F
3
4
FB
Sample Input & Output 2 (Standard Input And Output)
15 2
BF
3 5
1
2
B
4
F
B
7
8
B
F
1
B
3
4
BF
Sample Input & Output 3 (Standard Input And Output)
13 3
JLB
2 4 3
1
J
B
JL
5
JB
7
JL
B
J
11
JLB
13
What I have tried:
#include <stdio.h>
int main()
{
int N, K, A[100005], i, a, j, fl;
scanf("%d %d", &N, &K);
getchar();
char S[K + 5];
scanf("%s", S);
getchar();
for(i = 0; i < K; i++)
{
scanf("%d", &A[i]);
}
getchar();
for(i = 1; i <= N; i++)
{
a = i;
fl = 0;
for(j = 0; j < K; j++)
{
if(a % A[j] == 0)
{
printf("%c", S[j]);
fl = 1;
}
}
if(fl == 1)
{
printf("\n");
}
else
{
if(a >= 10)
{
a %= 10;
}
printf("%d\n", a);
}
}
return 0;
}