数论刷题

【题1 Modified GCD CodeForces - 75C】

Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it’s an easy task to calculate the GCD between two positive integers.

A common divisor for two positive numbers is a number which both numbers are divisible by.

But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range.

You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.

Input
The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).

Output
Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.

Example
Input
9 27
3
1 5
10 11
9 11
Output
3
-1
9

【分析:求解两个数的所有的公约数】
本题考察求两个数的所有公约数的问题,还有二分查找求上界,哎,好久又没学习了,竟把二分查找这个上界写法忘记了。。。

被这个坑惨了,下面的求两个数公约数的写法明显是错的。。

for(ll i=1;i<=sqrt(d);i++)
    {
        if(d%i==0)factor[cnt++]=i;
    }
    factor[cnt]=d;

AC代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define ll long long

using namespace std;
const ll maxn=1e6;
ll factor[maxn];

ll gcd(ll a,ll b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}


ll bin_search(ll a[],ll l,ll r,ll v)
{
    ll mid;
    while(l<r)
    {
        mid=l+(r-l)/2;
        if(v>=a[mid])l=mid+1;
        else r=mid;
    }
    return l;
}

int main()
{
    ll a,b,n;
    ll low,high;
    scanf("%lld%lld%lld",&a,&b,&n);
    ll d=gcd(a,b);
    ll cnt=0;
    //两个数的公约数求法
    for(ll i=1;i<=sqrt(d);i++)
    {
        if(d%i==0)
        {
            factor[cnt++]=i;
            factor[cnt++]=d/i;//不要忘记这一步骤,否则错
        }
    }
    sort(factor,factor+cnt);
    //for(int i=0;i<=cnt;i++)
    //    cout<<factor[i]<<" ";
    while(n--)
    {
        scanf("%lld%lld",&low,&high);
        ll tmp=bin_search(factor,0,cnt,high);
        if(factor[tmp-1]<low)printf("-1\n");
        else
            printf("%lld\n",factor[tmp-1]);
    }
    return 0;
}

【题2 青蛙的约会 POJ - 1061】

两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。
我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。
Input
输入只包括一行5个整数x,y,m,n,L,其中x≠y < 2000000000,0 < m、n < 2000000000,0 < L < 2100000000。
Output
输出碰面所需要的跳跃次数,如果永远不可能碰面则输出一行"Impossible"
Sample Input
1 2 3 4 5
Sample Output
4

【分析:求解二元一次不定方程最小正整数解】
本题考察扩展欧几里得算法,重点是要求出最小正整数解的问题,而最小正整数解有固定的公式x=(x%b+b)%b,具体实现方法见下面的源代码。坑我的点在于下面的具体分析中!!!

根据题意,设青蛙用时t(即跳出t步),各自坐标分别是x+mt,y+nt,可以列出方程x+mt-(y+nt)=pL(p∈Z),化为(m-n)t+Lp=y-x,设a=m-n,b=L,c=y-x,方程变为at+bp=c,然后扩展欧几里得算法即可解出方程的根,然后直接套用公式x=(x%b+b)%b求出最小的正整数解即可。我被坑的地方是什么呢?本题不是单单的(x+mt)≡(y+nt)(mod L),一定要按照上面的分析列出方程,只有x+mt-(y+nt)=pL(p∈Z)这么一种形式,或者是这个方程的变形,不可以是x+mt+pL=(y+nt)L(p∈Z)这种形式,这种形式是错误的,这就是本题的主要坑我的地方,因为不能单单的mol L相同就行了,要注意本题的实际意义中的两个坐标之间 的关系。

AC代码:

#include<cstdio>
#include<iostream>
#define ll long long

using namespace std;

ll x,y,m,n,L;

ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0){x=1,y=0;return a;}
    ll d=exgcd(b,a%b,x,y);
    ll tmp=x;
    x=y;
    y=tmp-a/b*y;
    return d;
}


int main()
{
    while(cin>>x>>y>>m>>n>>L)
    {
        ll xx,yy;
        ll a=m-n,b=-L,c=y-x;
        ll d=exgcd(a,b,xx,yy);
        if(c%d!=0 || m==n)
            cout<<"Impossible"<<endl;
        else
        {
            a/=d;
            b/=d;
            c/=d;
            xx=xx*c;
            xx=(xx%b+b)%b;
            cout<<xx<<endl;
        }
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值