Indian National Olympiad in Informatics 2015
A string is any nonempty sequence of 0s and 1s. Examples of strings are 00, 101, 111000, 1, 0, 01. The length of a string is the number of symbols in it. For example, the length of 111000 is 6. If u and v are strings, then uv is the string obtained by concatenating u and v. For example if u = 110 and v = 0010 then uv = 1100010.
A string w is periodic if there exists a string v such that w = vn = vv · · · v (n times), for some n ≥ 2. Note that in this case the length of v is strictly less than that of w. For example, 110110 is periodic, because it is vv for v = 110.
Given a positive integer N , find the number of strings of length N which are not periodic. Report the answer modulo M . The non-periodic strings of length 2 are 10 and 01. The non- periodic strings of length 3 are 001, 010, 011, 100, 101, and 110.

-----Input format-----
A single line, with two space-separated integers, N and M .

-----Output format-----
A single integer, the number of non-periodic strings of length N , modulo M .

-----Test Data-----
In all subtasks, 2 ≤ M ≤ 108. The testdata is grouped into 4 subtasks.
Subtask 1 (10 marks) 1 ≤ N ≤ 4000. N is the product of two distinct prime numbers.
Subtask 2 (20 marks) 1 ≤ N ≤ 4000. N is a power of a prime number.
Subtask 3 (35 marks) 1 ≤ N ≤ 4000.
Subtask 4 (35 marks) 1 ≤ N ≤ 150000.

-----Example-----
Here is the sample input and output corresponding to the example above:

-----Sample input-----
3 176

-----Sample output-----
6

Note: Your program should not print anything other than what is specified in the output format. Please remove all diagnostic print statements before making your final submission. A program with extraneous output will be treated as incorrect!

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = 'powersof2 = {}\r\nnonperiodics= {}\r\n\r\n\r\ndef pow2(x, m):\r\n    powersof2[0] = 1%m\r\n    powersof2[1] = 2%m\r\n    for i in range(2, x+1):\r\n        powersof2[i] = (powersof2[i-1]*2)%m\r\n\r\n\r\ndef nonperiodicsfill(x):\r\n    for i in range(x):\r\n        nonperiodics[i]=0\r\n\r\n\r\ndef listoffactors(x):\r\n    lista=[]\r\n    for i in range(1, 1 + x//2):\r\n        if x%i == 0 :\r\n            lista.append(i)\r\n    return lista\r\n\r\n\r\ndef sieve(x):\r\n    for i in range(2, x):\r\n        if prime[i]:\r\n            for q in range(i*2, x, i):\r\n                prime[q] = False\r\n\r\n\r\ndef nonPeriodicStrings(s,m):\r\n   # print("\\nNPS"+ str(s) +" "+ str(m) + "\\n")\r\n    if s==1:\r\n        return 2%m\r\n    if s in nonperiodics:\r\n        return nonperiodics[s]\r\n    if prime[s]:\r\n        nps = (powersof2[s] - 2) % m\r\n        nonperiodics[s] = nps\r\n        return nps\r\n #   print(s)\r\n    li = listoffactors(s)\r\n   # print(li)\r\n    nps = powersof2[s]\r\n    for i in li:\r\n        nps -= nonPeriodicStrings(i, m)\r\n    #    print(nonPeriodicStrings(i,m))\r\n        nps %= m\r\n    nonperiodics[s] = nps\r\n    return nps\r\n\r\n\r\ncc = input().split(\' \')\r\na= int(cc[0])\r\nm= int(cc[1])\r\n#print(a)\r\n#print(m)\r\nprime = [True for i in range(a+1)]\r\n#m = int(input())\r\npow2(a, m)\r\n#print(powersof2)\r\nsieve(a+1)\r\n#print(prime)\r\nif prime[a]:\r\n    l = powersof2[a] - 2\r\n    print(l)\r\nelse:\r\n    print(nonPeriodicStrings(a, m))\r\n\r\n\r\n\r\n'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 13981