---
id: 2464
lang: en
translation_of: index.md
source_sha256: 3893d58f2d1b9596dd53ea9dae1fff1320294b326d6f75d7901330c18d9a2013
title: "[Codeforces] 900D: Unusual Sequences — Solution"
description: "Divide y by x, count the sequences with a power of two, and apply inclusion–exclusion over the prime factors of y/x."
tags: ["OI","Number Theory","Inclusion–Exclusion"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes: []
---

## Main Idea

First, divide $y$ by $x$, and we find that we can use $2^{t - 1}$ to count the sequences whose sum is $y$.

Knowing this, we find that we can enumerate the prime factors of $\frac{y}{x}$ to do inclusion–exclusion.

<!--more-->

## Code

```cpp
// CF900D.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 14, mod = 1e9 + 7;

int x, y;

int fpow(int bas, int tim)
{
    int ret = 1;
    while (tim)
    {
        if (tim & 1)
            ret = 1LL * ret * bas % mod;
        bas = 1LL * bas * bas % mod;
        tim >>= 1;
    }
    return ret;
}

int main()
{
    scanf("%d%d", &x, &y);
    if (y % x != 0)
        puts("0"), exit(0);
    y /= x;
    int acc = y;
    vector<int> primes;
    for (int i = 2; 1LL * i * i <= acc; i++)
        if (acc % i == 0)
        {
            primes.push_back(i);
            while (acc % i == 0)
                acc /= i;
        }
    if (acc > 1)
        primes.push_back(acc);
    int n = primes.size(), ans = 0;
    for (int stat = 0; stat < (1 << n); stat++)
    {
        int prod = 1;
        for (int i = 0; i < n; i++)
            if (stat & (1 << i))
                prod *= primes[i];
        ans = (0LL + ans + mod + ((__builtin_popcount(stat) & 1) ? -1 : 1) * fpow(2, y / prod - 1) % mod) % mod;
    }
    printf("%d\n", ans);
    return 0;
}
```
