ural 1907 Coffee and Buns

1907. Coffee and Buns

Time limit: 1.0 second
Memory limit: 64 MB

Planet Ataraxia is known for its education centers. The people who are expected to take high social positions in future are brought up in conditions of continuous training and supervision from the early age. Each education center is a small town with all the necessary utilities. During the construction of a center, a square area is chosen which is divided into equal sections each sized 100 × 100 meters. In each of these sections they build one building, which would become residential or academic. The outer perimeter of the center is fenced.

After a successful military operation in the Andromeda nebula the active colonization of habitable planets has started. The need for people able to take command and lead people to the new worlds has increased. Therefore, two new education centers should be built on Ataraxia. Discussion about the details of the project in the local administration is already underway for many days. During this time it was decided that the first center will consist of a2 sections and that the second one will consist of no more than n2 sections. The situation is complicated because, according to requirements of the antimonopoly legislation, construction works must be performed by at least two different companies, each of them must build an equal number of buildings and an equal number of 100-meters segments of the fence.

You are responsible for resupplying the administration office. You understand that while they are discussing the pros and cons of each possible size of the second center a lot of buns and coffee will be consumed, and it‘s time to buy them. So you‘d like to know how many different sizes of the second center will meet the requirements of antimonopoly legislation and, therefore, will be fully considered by the administration.

Input

The only line contains integers a and n (1 ≤ a ≤ 1012; 1 ≤ n ≤ 1018).

Output

Output an amount of different sizes of the second center meeting the requirements of antimonopoly legislation.

Sample

input output
3 6
4

Hint

In this example it is possible to build the second center sized 3 × 3 or 6 × 6, delegating the construction to three different companies, or to build it sized 1 × 1 or 5 × 5, delegating the construction to two different companies.

题意: 题目说了那么多,其实最后题意很简单,给定一个a和n,求在1-n之间有几个数x,满足gcd(4(a+x),a^2+x^2)>1

思路,gcd(4(a+x),a^2+x^2)>1 可以得到 gcd(a+x,a^2+x^2)>1( 如果a+x 是偶数,4就是没有用的,如果a+x是奇数,那么a,x有一个为奇,一个

为偶,那么a^2+x^2就是奇数,那么4也是没有用的) 然后根据 gcd(a,b) = gcd(b,a%b) 得到 gcd(a+x,2*a*x)  ( a^2+x^2 = (a+x)^2-2*a*x  模 a+x 等于 2*a*x )

接下来就是计算 gcd(a+x,2*a*x) > 1 了 如果 a是偶数,那么 还可以转化为 gcd(a+x ,a*x) > 1 ; 这里符合的就是 x 是 a 的因子的倍数;

如果 a是奇数 ,那么所有的 奇数都是符合的;还有就是 x是a任意一个因子的倍数(代码里面 n/2 的意思就是,我们取完 1-n 里面所有的奇数。剩下的就是求偶数里面的,

因为x是偶数,所以 2 是可以不要的,就和上面一样的求法)

a任意一个因子的倍数个数,这里用容斥定理求;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<ctime>
#include<bitset>
#define LL long long
#define INF 999999
#define maxn 500010
using namespace std;

LL get(LL n,LL MAX)
{
    vector<LL>q;
    LL num=n,ans=0,tt ;
    for(LL i = 2 ; i*i <= n ;i++)if(n%i==0)
    {
        while(n%i==0)
        {
            n /= i ;
        }
        q.push_back(i) ;
    }
    if(n>1)q.push_back(n) ;
    int m = q.size();
    int len=(1<<m) ,e ;
    for( int i = 1 ; i < len ;i++)
    {
        e=0;
        tt=1;
        for( int j = 0 ; j < m ;j++)if((1<<j)&i)
        {
            tt *= q[j] ;
            e++;
        }
        if(e&1) ans += MAX/tt ;
        else ans -= MAX/tt ;
    }
    return ans;
}

int main()
{
    int i,j,k;
    LL n ,a,ans ;
    while(cin >> a >> n)
    {
        if(a&1)
            ans=(n+1)/2 ,n/=2;
        else ans=0;
        ans += get(a,n) ;
        cout << ans<<endl;
    }
}

时间: 2024-10-18 18:38:51

ural 1907 Coffee and Buns的相关文章

URAL 1907. Coffee and Buns(数论推导+容斥原理)

1907. Coffee and Buns Time limit: 1.0 second Memory limit: 64 MB Planet Ataraxia is known for its education centers. The people who are expected to take high social positions in future are brought up in conditions of continuous training and supervisi

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀

ural 1272. Non-Yekaterinburg Subway

1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construct a subway. The peculiarity of the town is that it is located on small islands, some of them are connected with tunnels or bridges. The mayor is

ural 1273. Tie

1273. Tie Time limit: 1.0 secondMemory limit: 64 MB The subway constructors are not angels. The work under the ground and… Well, they are not angels. And where have you seen angels? It is all in a lifetime! Show me first somebody who has never… and t

ural 1269. Obscene Words Filter

1269. Obscene Words Filter Time limit: 0.5 secondMemory limit: 8 MB There is a problem to check messages of web-board visitors for the obscene words. Your elder colleagues commit this problem to you. You are to write a program, which check if there i

ural 1218. Episode N-th: The Jedi Tournament

1218. Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Jedi Knights to organize a tournament once. To know, accumulates who the largest amount of Force. Brought each Jedi his lightsaber with him to the tourn

ural 1217. Unlucky Tickets

1217. Unlucky Tickets Time limit: 1.0 secondMemory limit: 64 MB Strange people live in Moscow! Each time in the bus, getting a ticket with a 6-digit number, they try to sum up the first half of digits and the last half of digits. If these two sums ar

ural 1219. Symbolic Sequence

1219. Symbolic Sequence Time limit: 1.0 secondMemory limit: 64 MB Your program is to output a sequence of 1 000 000 lowercase Latin letters. This sequence should satisfy the following restrictions: Every letter occurs not more than 40 000 times in th