2018暑假楼下集训第二场(GCD,哈希,二分)

A-最小公倍数
题解: 裸的GCD,注意先除再乘,否则可能会超数据范围

#include <iostream>
#include <cstdio>
using namespace std;
int gcd(int n,int m)
{
    if (m==0)
        return n;
    return gcd(m,n%m);
}
int main()
{
   int n,m;
   while(cin>>n>>m)
   {
       int t=gcd(n,m);
       printf("%d\n",n/t*m);
   }
    return 0;
}

B-又见GCD

题意: 已知gcd(a,c)=b,和a,b,求最小的c,其中c!=b。
题解: 原以为既然c!=b,那么最小的当然是2b啦,可是提交了发先WA。回头重新分析这道题会发现,不是这么简单,原因在于a可能是c的倍数,比如若a=6b,那么c=2b,3b,4b都不是答案,因为这时候gcd(a,c)=2b,3b,2b。所以,从2b开始,只好一个一个试了,因为是b的倍数,所以一次增加b,直到gcd(a,c)=b。

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
int GCD(int a,int b)
{
    if (b==0)
        return a;
    else return GCD(b,a%b);
}
int main()
{
    int t,a,b;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d %d",&a,&b);
        int c=2*b;  
        while(gcd(a,c)!=b)  
        {  
            c+=b;  
        }  
    }
    return 0;
}

C-Least Common Multiple
题意: 给出n个数,求n个数的最小公倍数
题解: 不断求两个数的最小公倍数,然后把求得的最小公倍数刚入数组中,再求最小公倍数。

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int GCD(int a,int b)
{
    if (b==0)
        return a;
    else
        return GCD(b,a%b);
}
int LCM(int a,int b)
{
    return a/GCD(a,b)*b;
}
int main()
{
    int t;
    int a[1000];
    scanf("%d",&t);
    while(t--)
    {
        int x;
        scanf("%d",&x);
        for (int i=0; i<x; i++)
            scanf("%d",a+i);
        sort(a,a+x);
        for (int j=0; j<x; j++)
        {
            a[j+1]=LCM(a[j],a[j+1]);
        }
        printf("%d\n",a[x-1]);
    }
    return 0;
}

D-Revenge of ex-Euclid
题意: 给定三个数a,b,c,问存在多少对(x,y),使得a*x+b*y=c。
题解:简单枚举。遍历x,找满足条件的的x,使得(c-a*x)%b==0 成立。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int N=1e6+5;
int main()
{
    int T;
    int a,b,c;
    cin>>T;
    while (T--)
    {
        cin>>a>>b>>c;
        int ans=0;
        for (int i=1;c-a*i>0;i++)
        {
            if ((c-a*i)%b==0)
                ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}

E-Counting Squares
题意: 给一系列矩形的右上角坐标和左下角坐标,如:(5,8,7,10)表示(5,8),(7,8),(7,10),(5,10)组成的矩形,求这一系列矩形覆盖的1*1的方格的数量,重复覆盖只计数一次。(-1,-1,,1,,1)用来分割没一组矩形,(-2,-2,-2,,2)用来表示程序输入结束。
题解: 用二维数组模拟平面,遍历每个矩形,标记每个矩形覆盖的1*1的方格。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int v[110][110];
int main()
{
    int flag=1;
    while (flag)
    {
        memset(v,0,sizeof(v));
        int x1,x2,y1,y2;
        int sum=0;
        while (scanf("%d %d %d %d",&x1,&y1,&x2,&y2))
        {
            //(x2,y2)表示右上角,(x1,y1)表示左下角
            if (x1>x2)
                swap(x1,x2);
            if (y1>y2)
                swap(y1,y2);
            if (x1==y1&&x1==x2&&x1==y2)
            {
                if (x1==-1)
                {
                    flag=1;
                    break;
                }
                else if(x1==-2)
                {
                    flag=0;
                    break;
                }
            }
            for (int i=x1; i<x2; i++)
                for (int j=y1; j<y2; j++)
                    //避免重复计数
                    if (v[i][j]==0)
                    {
                        //标记被覆盖的11的方格
                        v[i][j]=1;
                        sum++;
                    }
        }
        printf("%d\n",sum);
    }
    return 0;
}

F-前m大的数
题意:给出n个数,两两求和,求m大的和
题解: (1)sort (2)哈希

//直接sort
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
const int N=1e6;
int a[5000];
int b[N];
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int n,m;
    while (~scanf("%d %d",&n,&m))
    {
        for (int i=0; i<n; i++)
        {
            scanf("%d",a+i);
        }
        int k=0;
        sort(a,a+n,cmp);
        n=ceil(m);
        for (int i=0; i<n; i++)
            for (int j=i-1; j>=0; j--)
            {
                b[k++]=a[i]+a[j];
            }
        sort(b,b+k,cmp);
        for (int i=0; i<m; i++)
            if (i==m-1)
                printf("%d\n",b[i]);
            else
                printf("%d ",b[i]);
    }
    return 0;
}
//哈希
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
int a[3005];
int h[10005];
bool cmp(int a,int b)
{
    return a>b;
}

int main()
{
    int n,m,Max;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        Max=-1;
        memset(h,0,sizeof(h));
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        for(int i=0; i<n; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                h[a[i]+a[j]]++;
                Max=max(Max,a[i]+a[j]);
            }
        }
        int k=m,i=Max;
        while(k)
        {
            if(h[i]--)
            {
                if(k!=m)
                    printf(" ");
                printf("%d",i);
                k--;
            }
            else i--;
        }
        printf("\n");
    }
    return 0;
}

G-Can you solve this equation?
题意: 输入一个实数,求x,使得8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 = Y成立,x的范围为[0,100],否则输出“No solution!”。
题解: 二分思想,x在[0,100]之间不断二分查找,直至找到结果

#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const double N=1e-8;//这里数不能太大,否则精度会出错
double coun(double x)
{
    return 8*x*x*x*x+7*x*x*x+2*x*x+3*x+6;
}
int main()
{
    int T;
    double y,s0=coun(0),s100=coun(100.0);
    scanf("%d",&T);
    while (T--)
    {
        scanf("%lf",&y);
        if (y<s0||y>s100)
        {
            printf("No solution!\n");
            continue;
        }
        double l=0,r=100,mid;
        int flag=0;
        //while (fabs(coun(mid)-y)>N)两个判断条件都可以,不过mid的位置有稍微的改变
        while (r-l>N)
        {
            mid=(l+r)/2;
            if(coun(mid)>y)
                r=mid;
            else
                l=mid;
        }

        printf("%.4lf\n",mid);
    }
    return 0;
}

H-简单二分查找
题解: 裸二分,没什么好讲的

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=1e5+7;
int a[N];
int main()
{
    int n,m;
    while(cin>>n)
    {
        for (int i=0; i<n; i++)
        {
            cin>>a[i];
        }
        cin>>m;
        int l=0,r=n-1,mid,flag=0;
        sort(a,a+n);
        while (r>=l)
        {
            mid=(l+r)/2;
            if (a[mid]==m)
            {
                flag=1;
                break;
            }
            else if (a[mid]>m)
                r=mid-1;
            else
                l=mid+1;

        }
        printf("%s\n",flag==1?"YES":"NO");
    }
    return 0;
}

I-二分查找
题解: 裸二分,唯一注意的点就是,这道题不能用cin,cout 会超时,用scanf,printf进行输入输出

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=3e6+7;
int a[N];
int main()
{
    int n,m,t;
    scanf("%d",&n);
    for (int i=0; i<n; i++)
        scanf("%d",a+i);
    scanf("%d",&m);
    while (m--)
    {
        scanf("%d",&t);
        int l=0,r=n-1,mid,flag=0;
        while (r>=l)
        {
            mid=(l+r)/2;
            if (a[mid]==t)
            {
                flag=1;
                break;
            }
            else if (a[mid]>t)
                r=mid-1;
            else
                l=mid+1;

        }
        printf("%d\n",flag==0?-1:mid+1);
    }
    return 0;
}

J-二分练习
题解: 输入一串序列,排序为升序排列,分别从左边找小于目标数且接近目标数的数的下标d,和从右边找大于目标数且接近目标数的数的下标u,a[u]和a[d]两个数进行比较,输出答案

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=1e7+7;
int a[N];
//找小于目标的数且最接近目标数的数
//因为找小于目标数的数,所以要从左边不断地接近目标数
int down(int left,int right,int t)
{
    int mid,index=-1;
    while (left<=right)
    {
        mid=(left+right)/2;
        if (a[mid]<=t)
        {
            left=mid+1;
            index=mid;
        }
        else right=mid-1;
    }
    return index;
}
//找大于目标的数且最接近目标数的数
//因为找大于目标数的数,所以要从右边不断地接近目标数
int up(int left,int right,int t)
{
    int mid,index=-1;
    while(left<=right)
    {
        mid=(left+right)/2;
        if (a[mid]<=t)
            left=mid+1;
        else
        {
            right=mid-1;
            index=mid;
        }
    }
    return index;
}
int main()
{
    int n,m,t;
    while (~scanf("%d %d",&n,&m))
    {
        for (int i=0; i<n; i++)
            scanf("%d",a+i);
        sort(a,a+n);//升序
        while (m--)
        {
            scanf("%d",&t);
            int d=down(0,n-1,t);
            int u=up(0,n-1,t);
            if (d==-1)//没有左边的数
                printf("%d\n",a[u]);
            else if (u==-1)//右边没有数
                printf("%d\n",a[d]);
            else if (a[d]==a[u])//左边和右边找到的一样,那就是找到了
                printf("%d\n",a[u]);
            else
            {
                if (t-a[d]<a[u]-t)//左边的数都更接近目标数
                    printf("%d\n",a[d]);
                else if (t-a[d]>a[u]-t)//右边的数更接近目标数
                    printf("%d\n",a[u]);
                else //左边的数和右边的数一样接近目标数
                    printf("%d %d\n",a[d],a[u]);
            }
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39535750/article/details/81101711