1957E - Carousel of Combinations - CodeForces Solution


brute force combinatorics dp math number theory

Please click on ads to support us..

Python Code:

from sys import stdin
rd = stdin.readline
t = int(rd())
mod = 10**9 + 7
c = [0,0,1,4,8]
prime = [1] * (10**6+1)
add = [0] * (10**6+1)

for i in range(2, 1000000):
    if prime[i]:
        for j in range(2, 10**6//i + 1):
            prime[i*j] = 0
            add[i*j] = (add[i*j]-1) % mod
            if j % i == 1:
                add[i*j] = (add[i*j]+i) % mod

k = 4
for i in range(5, 10**6+1):
    k = (k+add[i]) % mod
    if prime[i]:
        k = (k+i-1) % mod
    elif i % 8 == 0:
        k = (k-2) % mod
    elif i % 8 == 4:
        k = (k+2) % mod
    c.append((c[-1]+k)%mod)

for _ in range(t):
    n = int(rd())
    print(c[n])


Comments

Submit
0 Comments
More Questions

174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes