brute force greedy math

Please click on ads to support us..

Python Code:

import os
import sys
import math
from collections import deque, defaultdict
from bisect import bisect_left, bisect_right
import heapq
import itertools

input = sys.stdin.readline


def multiple():
    a = map(int, input().split())
    return a


def array():
    a = input().split()
    return a


def intarray():
    a = list(map(int, input().split()))
    return a


def intinput():
    n = int(input())
    return n


def strinput():
    s = input().strip()
    return s


def isPrime(n):
    val = int(math.sqrt(n)) + 1
    for i in range(2, val):
        if n % i == 0:
            return False
    return True


def power(x, y, p):
    res = 1
    x = x % p

    if (x == 0):
        return 0

    while (y > 0):
        if ((y & 1) == 1):
            res = (res * x) % p

        y = y >> 1
        x = (x * x) % p

    return res


def modexp(x, n, m):
    if (n == 0):
        return 1

    elif (n % 2 == 0):
        return modexp((x * x) % m, n // 2, m)

    else:
        return (x * modexp((x * x) % m,
                           (n - 1) / 2, m) % m)


def getFractionModulo(a, b, m):
    c = math.gcd(a, b)

    a = a // c
    b = b // c

    d = modexp(b, m - 2, m)

    ans = ((a % m) * (d % m)) % m

    return ans


def factorial(n, dp):
    if dp[n] != 0:
        return dp[n]
    val = 1
    for i in range(2, n + 1):
        val *= i
        dp[i] = val
    return val


def notmultiple(a, b):
    while a % b == 0:
        a = a // b
    if a == 1:
        return False
    return True

def median(a):
    n = len(a)
    if n%2 == 0:
        val1 = n//2
        val2 = val1-1
        return (a[val1] + a[val2])/2
    else:
        val1 = n//2
        return a[val1]

def solution():
            n = intinput()
    s = strinput()
    a = [0]*(n+1)
    for i in range(1,n+1):
        if s[i-1] == '1':
            a[i] = 1
    ans = 0
    cost = [0]*(n+1)
    for i in range(n,0,-1):
        for j in range(i,n+1,i):
            if a[j] == 1:
                break
            cost[j] = i
    for i in range(1,n+1):
        if a[i] != 1:
            ans += cost[i]
    print(ans)
    return

t = 1
t = int(input())
for _ in range(t):
            solution()

C++ Code:

#include<cstdio>
char s[1000007];
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
long long ans=0;
scanf("%d%s",&n,s+1);
for(int i=1;i<=n;++i)
for(int j=1;i*j<=n&&s[i*j]!=49;++j)
ans+=i*(s[i*j]==48),s[i*j]=50;
printf("%lld\n",ans);
}
return 0;
}


Comments

Submit
0 Comments
More Questions

Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations