D. Unusual Sequences (Counting inclusion and exclusion arrangement)

topic

Meaning of the questions:

    Given x and y, the number of sequences is determined to satisfy the following conditions. 1: gcd sequence is x, 2: Sequences to y.
     1 x , Y 1 0 9 1 ≤ x, y ≤ 10 ^ 9

analysis:

    Clearly, these numbers must be a multiple of x, y so must also be a multiple of x, and now there is y / x a x let us operate. We direct divided by x, the requirements to become a gcd is 1. There are y / x a x, then the number of kinds of columns are formed and how much, the solution of the equation with n integer variable, a variable number from 1 ~ y / x, can be drawn 2 ^ (y / x-1 ).
    But not necessarily gcd of these sequences is 1, so we need to subtract gcd for anything else. Then gcd may also have what, apparently factor of y / x. Therefore mA [x] and expressed as x and gcd is a number from 1 sequence types, then the z-where gcd can be expressed as ma [x / z] (z each for a group). Therefore ma [x] = 2 ^ ( x-1) -ma [t] (t x is a factor).

#include <iostream>
#include <vector>
#include <map>
using namespace std;

typedef long long ll; 
ll mod = 1e9 + 7;
map<ll,ll> ma;

ll q_pow(ll a,ll b)
{
	ll res = 1;
	while( b )
	{
		if( b & 1 )
		{
			res *= a;
			res %= mod;
		}
		a *= a;
		a %= mod;
		b >>= 1;
	}
	return res;
}

int slove(int n)
{
	if( ma[n] ) return ma[n];
	if( n == 1 ) return ma[n] = 1;
	ma[n] = q_pow(2,n-1);
	for (int i = 2; i * i <= n; i++)
	{
		if( n % i == 0 )
		{
			ma[n] = ( ma[n] - slove(n/i) + mod ) % mod;
			if( i * i != n ) ma[n] = (ma[n] - slove(i) + mod) % mod; 
		}
	}
	ma[n] = ( ma[n] - slove(1) + mod ) % mod;
	return ma[n];
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll x,y;
	cin >> x >> y;
	if( y % x != 0 )
	{
		cout << 0 << '\n';
		return 0;
	}
	cout << slove(y/x) << '\n';
	return 0;
}

Published 132 original articles · won praise 6 · views 7917

Guess you like

Origin blog.csdn.net/weixin_44316314/article/details/105030335
Recommended