在数组元素的素数倍数的索引处翻转位后找到最终字符串
给定一个大小为N的二进制字符串S和一个由M个整数组成的数组arr[] ,任务是在翻转所有数组元素的素数倍数的索引处的字符后找到最终的字符串。请注意,此问题使用基于 1 的索引。
例子:
Input: S = “000000”, arr[] = {2, 4, 6}
Output: 011100
Explanation:
- The prime factors of 2 are {2}. Therefore string after flipping the values of all the indices that are multiple of 2 is S = “010101”.
- The prime factors of 4 are {2}. Therefore string after flipping the values of all the indices that are multiple of 2 is S = “000000”.
- The prime factors of 6 are {2, 3}. Therefore string after flipping the values of all the indices that are multiple of 2 is S = “010101” and the string after flipping the values of all the indices that are multiple of 3 is S = “011100”.
Input: S = “001100”, arr[] = {2, 3, 5}
Output: 010010
方法:通过存储所有数组元素的素数因子的频率,可以在埃拉托色尼筛法的帮助下解决给定的问题。可以观察到,具有偶数频率的素因子不会对翻转有贡献。因此,可以通过翻转多个具有奇数频率的素数的索引的位来获得结果字符串。请按照以下步骤解决给定的问题:
- 创建一个函数getFactorization() ,它返回任意数的所有质因数。
- 初始化一个数组frequency[] ,它存储数组arr[]的所有元素的所有不同质因数的频率。
- 遍历给定的数组arr[]并且对于每个数组元素arr[i] ,使用getFactorization( arr[i] )找到它的素因子,并增加频率 []数组中素因子的频率。
- 对于数组中频率[i]的所有奇数值,遍历i的所有因子并翻转给定字符串S 中各个索引的位。
- 存储在S中的字符串是必需的字符串。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
#define MAXN 100001
// Stores smallest prime factor
int spf[MAXN];
// Function to find the smallest prime
// factor for every number till MAXN
void sieve()
{
spf[1] = 1;
// Marking smallest prime factor for
// every number to be itself
for (int i = 2; i < MAXN; i++)
spf[i] = i;
// Separately marking spf for every
// even number as 2
for (int i = 4; i < MAXN; i += 2)
spf[i] = 2;
for (int i = 3; i * i < MAXN; i++) {
// Checking if i is prime
if (spf[i] == i) {
// Marking SPF for all numbers
// divisible by i
for (int j = i * i;
j < MAXN; j += i) {
if (spf[j] == j)
spf[j] = i;
}
}
}
}
// Function to find all the distinct prime
// factors of the given number x
vector getFactorization(int x)
{
vector ret;
// Find the prime factors for X
while (x != 1) {
ret.push_back(spf[x]);
// Find the spf[] of x
int value = spf[x];
while (x % value == 0)
x = x / value;
}
// Return the prime factors for x
return ret;
}
// Function to find string after flipping
// the characters at indices of prime
// factors of array elements arr[]
string flipString(string S, int arr[],
int M)
{
// Precalculating Smallest Prime Factor
sieve();
// Stores the frequency of each
// prime factor
int frequency[MAXN] = { 0 };
// Iterate over all elements of
// the array arr[]
for (int i = 0; i < M; i++) {
// Stores prime factors of arr[i]
vector primeFactors
= getFactorization(arr[i]);
// Increment the frequency of all
// prime factors of arr[i]
for (auto& factors : primeFactors) {
frequency[factors]++;
frequency[factors] %= 2;
}
}
int N = S.length();
// Iterate over all elements of
// the array frequency[]
for (int i = 0; i < MAXN; i++) {
// If frequency[i] is odd
if (frequency[i] & 1) {
// Flip bits of all indices
// that are multiple of i
for (int j = i; j <= N; j += i) {
S[j - 1] = (S[j - 1]
== '1'
? '0'
: '1');
}
}
}
// Return Answer
return S;
}
// Driver Code
int main()
{
string S = "000000";
int arr[] = { 2, 4, 6 };
int M = sizeof(arr) / sizeof(arr[0]);
cout << flipString(S, arr, M);
return 0;
}
Java
// Java program for the above approach
import java.util.ArrayList;
import java.util.Arrays;
class GFG {
public static int MAXN = 100001;
// Stores smallest prime factor
public static int[] spf = new int[MAXN];
// Function to find the smallest prime
// factor for every number till MAXN
public static void sieve() {
spf[1] = 1;
// Marking smallest prime factor for
// every number to be itself
for (int i = 2; i < MAXN; i++)
spf[i] = i;
// Separately marking spf for every
// even number as 2
for (int i = 4; i < MAXN; i += 2)
spf[i] = 2;
for (int i = 3; i * i < MAXN; i++) {
// Checking if i is prime
if (spf[i] == i) {
// Marking SPF for all numbers
// divisible by i
for (int j = i * i; j < MAXN; j += i) {
if (spf[j] == j)
spf[j] = i;
}
}
}
}
// Function to find all the distinct prime
// factors of the given number x
public static ArrayList getFactorization(int x) {
ArrayList ret = new ArrayList();
// Find the prime factors for X
while (x != 1) {
ret.add(spf[x]);
// Find the spf[] of x
int value = spf[x];
while (x % value == 0)
x = x / value;
}
// Return the prime factors for x
return ret;
}
// Function to find string after flipping
// the characters at indices of prime
// factors of array elements arr[]
public static String flipString(String S, int arr[], int M) {
// Precalculating Smallest Prime Factor
sieve();
// Stores the frequency of each
// prime factor
int[] frequency = new int[MAXN];
Arrays.fill(frequency, 0);
// Iterate over all elements of
// the array arr[]
for (int i = 0; i < M; i++) {
// Stores prime factors of arr[i]
ArrayList primeFactors = getFactorization(arr[i]);
// Increment the frequency of all
// prime factors of arr[i]
for (int factors : primeFactors) {
frequency[factors]++;
frequency[factors] %= 2;
}
}
int N = S.length();
// Iterate over all elements of
// the array frequency[]
for (int i = 0; i < MAXN; i++) {
// If frequency[i] is odd
if ((frequency[i] & 1) > 0) {
// Flip bits of all indices
// that are multiple of i
for (int j = i; j <= N; j += i)
{
// S.replace(S.charAt(j - 1), (S.charAt(j - 1) == '1' ? '0' : '1'));
S = S.substring(0, j - 1) + (S.charAt(j - 1) == '1' ? '0' : '1') + S.substring(j);
}
}
}
// Return Answer
return S;
}
// Driver Code
public static void main(String args[]) {
String S = "000000";
int arr[] = { 2, 4, 6 };
int M = arr.length;
System.out.println(flipString(S, arr, M));
}
}
// This code is contributed by gfgking.
Python3
# Python program for the above approach
MAXN = 100001
# Stores smallest prime factor
spf = [0] * MAXN
# Function to find the smallest prime
# factor for every number till MAXN
def sieve():
spf[1] = 1
# Marking smallest prime factor for
# every number to be itself
for i in range(2, MAXN):
spf[i] = i
# Separately marking spf for every
# even number as 2
for i in range(4, MAXN, 2):
spf[i] = 2
i = 3
while(i * 8 < MAXN):
i += 1
# Checking if i is prime
if (spf[i] == i):
# Marking SPF for all numbers
# divisible by i
for j in range(i * i, MAXN, i):
if (spf[j] == j):
spf[j] = i
# Function to find all the distinct prime
# factors of the given number x
def getFactorization (x):
ret = []
# Find the prime factors for X
while (x != 1):
ret.append(spf[x])
# Find the spf[] of x
value = spf[x]
while (x % value == 0):
x = x // value
# Return the prime factors for x
return ret
# Function to find string after flipping
# the characters at indices of prime
# factors of array elements arr[]
def flipString (S, arr, M):
# Precalculating Smallest Prime Factor
sieve();
# Stores the frequency of each
# prime factor
frequency = [0] * MAXN
# Iterate over all elements of
# the array arr[]
for i in range(0, M):
# Stores prime factors of arr[i]
primeFactors = getFactorization(arr[i])
# Increment the frequency of all
# prime factors of arr[i]
for factors in primeFactors :
frequency[factors] += 1
frequency[factors] %= 2
N = len(S)
# Iterate over all elements of
# the array frequency[]
for i in range(0, MAXN):
# If frequency[i] is odd
if (frequency[i] & 1):
# Flip bits of all indices
# that are multiple of i
for j in range(i, N + 1, i):
S[j - 1] = ('0' if S[j - 1] == '1' else '1')
# Return Answer
return S
# Driver Code
S = "000000"
S = list(S)
arr = [2, 4, 6]
M = len(arr)
print("".join(flipString(S, arr, M)))
# This code is contributed by gfgking.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
public static int MAXN = 100001;
// Stores smallest prime factor
public static int[] spf = new int[MAXN];
// Function to find the smallest prime
// factor for every number till MAXN
public static void sieve()
{
spf[1] = 1;
// Marking smallest prime factor for
// every number to be itself
for (int i = 2; i < MAXN; i++)
spf[i] = i;
// Separately marking spf for every
// even number as 2
for (int i = 4; i < MAXN; i += 2)
spf[i] = 2;
for (int i = 3; i * i < MAXN; i++)
{
// Checking if i is prime
if (spf[i] == i)
{
// Marking SPF for all numbers
// divisible by i
for (int j = i * i; j < MAXN; j += i)
{
if (spf[j] == j)
spf[j] = i;
}
}
}
}
// Function to find all the distinct prime
// factors of the given number x
public static List getFactorization(int x)
{
List ret = new List();
// Find the prime factors for X
while (x != 1)
{
ret.Add(spf[x]);
// Find the spf[] of x
int value = spf[x];
while (x % value == 0)
x = x / value;
}
// Return the prime factors for x
return ret;
}
// Function to find string after flipping
// the characters at indices of prime
// factors of array elements arr[]
public static String flipString(String S, int[] arr, int M)
{
// Precalculating Smallest Prime Factor
sieve();
// Stores the frequency of each
// prime factor
int[] frequency = new int[MAXN];
Array.Fill(frequency, 0);
// Iterate over all elements of
// the array arr[]
for (int i = 0; i < M; i++)
{
// Stores prime factors of arr[i]
List primeFactors = getFactorization(arr[i]);
// Increment the frequency of all
// prime factors of arr[i]
foreach (int factors in primeFactors)
{
frequency[factors]++;
frequency[factors] %= 2;
}
}
int N = S.Length;
// Iterate over all elements of
// the array frequency[]
for (int i = 0; i < MAXN; i++)
{
// If frequency[i] is odd
if ((frequency[i] & 1) > 0)
{
// Flip bits of all indices
// that are multiple of i
for (int j = i; j <= N; j += i)
{
// S.replace(S.charAt(j - 1), (S.charAt(j - 1) == '1' ? '0' : '1'));
S = S.Substring(0, j - 1) + (S[j - 1] == '1' ? '0' : '1') + S.Substring(j);
}
}
}
// Return Answer
return S;
}
// Driver Code
public static void Main()
{
String S = "000000";
int[] arr = { 2, 4, 6 };
int M = arr.Length;
Console.Write(flipString(S, arr, M));
}
}
// This code is contributed by Saurabh Jaiswal
Javascript
输出:
011100
时间复杂度: O(N*log N + K*log K),其中 K 是数组arr[]中的最大整数。
辅助空间: O(K)