lightoj Basic Math 数论基础题 1414+1010+1020+1078+1116+1148+1179+1214+1275+1294+1297+1311+1323+1349+1354

//1008 - Fibsieve`s Fantabulous Birthday
//1008
#include <stdio.h>
#include <math.h>
#include <string.h>
#define LL long long
int main()
{
    LL T,tt=0;
    scanf("%lld",&T);
    while(T--)
    {
        LL n,j,k,m,t,i;
        scanf("%lld",&n);
        i=(LL)sqrt(1.0*n);
        if(n!=i*i)
            i++;
        m=n-(i-1)*(i-1);
        t=2*i-1;
        printf("Case %lld: ",++tt);
        if(i%2==1)
        {
            if(m<=i)
                printf("%lld %lld\n",i,m);
            else
                printf("%lld %lld\n",t-m+1,i);
        }
        else
        {
            if(m<=i)
                printf("%lld %lld\n",m,i);
            else
                printf("%lld %lld\n",i,t-m+1);
        }
    }
    return 0;
}
 

//1010 - Knights in Chessboard

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        if(n==1||m==1)
        {
            printf("Case %d: %d\n",++tt,max(n,m));
        }
        else if(n==2||m==2)
        {
            int t=max(n,m);
            if(t%4==1)
                printf("Case %d: %d\n",++tt,t/4*4+2);
            else if(t%4==2||t%4==3)
                printf("Case %d: %d\n",++tt,t/4*4+4);
            else
                printf("Case %d: %d\n",++tt,t);
        }
        else
            printf("Case %d: %d\n",++tt,(m*n)%2==0?m*n/2:m*n/2+1);
    }
    return 0;
}

// 1078 - Integer Divisibility

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        int n,k,s,i;
        scanf("%d%d",&n,&k);
        printf("Case %d: ",++tt);
        s=k%n;
        i=1;
        while(s!=0)
        {
            s=(s*10+k)%n;
            i++;
        }
        printf("%d\n",i);
    }
    return 0;
}

// 1116 - Ekka Dokka

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
    long long T,tt=0;
    scanf("%lld",&T);
    while(T--)
    {
        long long n,k,s,i;
        scanf("%lld",&n);
        printf("Case %lld: ",++tt);
        i=1;
        while(n%2==0)
        {
            n=n/2;
            i=i*2;
        }
        if(i==1)
            printf("Impossible\n");
        else
            printf("%lld %lld\n",n,i);
    }
    return 0;
}

// 1148 - Mad Counting

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int c[100];
int main()
{
    int T,tt=0;
    scanf("%lld",&T);
    while(T--)
    {
        int n,i,j,k,s=0;
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%d",&c[i]);
        printf("Case %d: ",++tt);
        sort(c,c+n);
        s+=c[0]+1;
        for(i=1,k=1;i<n;i++)
        {
            if(c[i]!=c[i-1])
            s+=c[i]+1;
            else
            {
                k++;
                if(k>c[i]+1)
                {
                    k=1;
                    s+=c[i]+1;
                }
            }
        }
        printf("%d\n",s);
    }
    return 0;
}
//结果竟可能小,就要求更多相同的值是支持相同队伍的,但支持相同队伍德人最多是其所述值+1

// 1179 - Josephus Problem

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int c[100],f[100001];
int main()
{
    int T,tt=0;
    scanf("%lld",&T);
    while(T--)
    {
        int n,i,j,k,s=0;
        scanf("%d%d",&n,&k);
        printf("Case %d: ",++tt);
        f[0]=0;
        for(i=2;i<=n;i++)
            f[i]=(f[i-1]+k)%i;
        printf("%d\n",f[n]+1);
    }
    return 0;
}

// 1214 - Large Division

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
    long long T,tt=0;
    scanf("%lld",&T);
    while(T--)
    {
        char a[202];
        long long n,i,j,k,s=0,b;
        scanf("%s%lld",a,&b);
        printf("Case %lld: ",++tt);
        n=strlen(a);
        for(i=0;i<n;i++)
        {
            if(a[i]=='-')
                continue;
            s=(s*10+(a[i]-'0'))%b;
        }
        if(s==0)
            printf("divisible\n");
        else
            printf("not divisible\n");
    }
    return 0;
}

// 1275 - Internet Service Providers

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        int n,i,a,j,k,s=0,b;
        scanf("%d%d",&a,&b);
        printf("Case %d: ",++tt);
        if(a==0||b==0)
            printf("%d\n",0);
        else
        {
            if(b%(2*a)==0)
                printf("%d\n",b/(2*a));
            else
            {
                printf("%d\n",(int)(1.0*b/2/a+0.499999));
            }
        }
    }
    return 0;
}

// 1294 - Positive Negative Sign

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
int main()
{
    LL T,tt=0;
    scanf("%lld",&T);
    while(T--)
    {
        LL n,i,a,j,k,s=0,b;
        scanf("%lld%lld",&a,&b);
        printf("Case %lld: ",++tt);
        printf("%lld\n",a/2*b);
    }
    return 0;
}

// 1297 - Largest Box

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        double w,l,t,d;
        scanf("%lf%lf",&w,&l);
        printf("Case %d: ",++tt);
        d=16*(w+l)*(w+l)-48*w*l;
        t=(4*(w+l)-sqrt(d))/24.0;
        printf("%lf\n",(w-2*t)*(l-2*t)*t);
    }
    return 0;
}

// 1311 - Unlucky Bird

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        double a,b,c,d,e,f,g;
        scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e);
        printf("Case %d: ",++tt);
        printf("%lf %lf\n",a*a/2/d+b*b/2/e,max(a/d,b/e)*c);
    }
    return 0;
}
 

// 1323 - Billiard Balls

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
struct ball
{
    int x,y;
}e[1001];
int cmp(ball a,ball b)
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}
int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        int l,w,n,k,i,j,a,b;
        char s[3];
        scanf("%d%d%d%d",&l,&w,&n,&k);
        for(i=0;i<n;i++)
        {
            scanf("%d%d%s",&a,&b,s);
            if(s[0]=='N')
            {
                b=(b+k)%(2*w);
                if(b<=w)
                    e[i].y=b;
                else
                    e[i].y=2*w-b;
            }
            else
            {
                b=(w-b+k)%(2*w);
                if(b<=w)
                    e[i].y=w-b;
                else
                    e[i].y=b-w;
            }
            if(s[1]=='W')
            {
                a=(l-a+k)%(2*l);
                if(a<=l)
                    e[i].x=l-a;
                else
                    e[i].x=a-l;
            }
            else
            {
                a=(a+k)%(2*l);
                if(a<=l)
                    e[i].x=a;
                else
                    e[i].x=2*l-a;
            }
        }
        printf("Case %d:\n",++tt);
        sort(e,e+n,cmp);
        for(i=0;i<n;i++)
            printf("%d %d\n",e[i].x,e[i].y);
    }
    return 0;
}
/*
观察可知,由于最后只是求球的位置,所以球体间碰撞可认为是穿透的
只用判断碰撞墙体,碰撞墙壁的横纵坐标变换都是两倍的长或宽循环变化的
*/

// 1349 - Aladdin and the Optimal Invitation

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
struct ball
{
    LL x,y,w;
}e[50005];
LL cmp1(ball a,ball b)
{
    return a.x<b.x;
}
LL cmp2(ball a,ball b)
{
    return a.y<b.y;
}
int main()
{
    LL T,tt=0;
    scanf("%lld",&T);
    while(T--)
    {
        LL n,m,q,i,j,k,u,v,w,s=0,x,y,num=0;
        scanf("%lld%lld%lld",&n,&m,&q);
        for(i=0;i<q;i++)
        {
            scanf("%lld%lld%lld",&e[i].x,&e[i].y,&e[i].w);
            num+=e[i].w;
        }
        num=(num+1)/2;
        sort(e,e+q,cmp1);
        for(i=0;i<q;i++)
        {
            s+=e[i].w;
            if(s>=num)
            {
                x=e[i].x;
                break;
            }
        }
        s=0;
        sort(e,e+q,cmp2);
        for(i=0;i<q;i++)
        {
            s+=e[i].w;
            if(s>=num)
            {
                y=e[i].y;
                break;
            }
        }
        printf("Case %lld: %lld %lld\n",++tt,x,y);
    }
    return 0;
}
/*
横坐标和纵坐标的距离计算是无关的,求横纵坐标的中位数就OK了
定理:|x-x1|+|x-x2|+|x-x3|+|x-x4|+|x-x5|+|x-x6|+...的最小值是x=(x1,x2,x3,x4,x5,...)的中位数的时候。
*/
 

// 1354 - IP Checking

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
struct ball
{
    LL x,y,w;
}e[50005];
LL cmp1(ball a,ball b)
{
    return a.x<b.x;
}
LL cmp2(ball a,ball b)
{
    return a.y<b.y;
}
int main()
{
    LL T,tt=0;
    scanf("%lld",&T);
    while(T--)
    {
        int a,b,c,d;
        int e,f,g,h;
        scanf("%d.%d.%d.%d",&a,&b,&c,&d);
        scanf("%d.%d.%d.%d",&e,&f,&g,&h);
        int ee,ff,gg,hh,i;
        ee=ff=gg=hh=0;
        i=1;while(e){ee+=(e%10)*i;e=e/10;i=i*2;}
        i=1;while(f){ff+=(f%10)*i;f=f/10;i=i*2;}
        i=1;while(g){gg+=(g%10)*i;g=g/10;i=i*2;}
        i=1;while(h){hh+=(h%10)*i;h=h/10;i=i*2;}
        //printf("%d %d %d %d\n",ee,ff,gg,hh);
        if(ee==a&&ff==b&&gg==c&&hh==d)
        printf("Case %lld: Yes\n",++tt);
        else
        printf("Case %lld: No\n",++tt);
    }
    return 0;
}

// 1369 - Answering Queries

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
LL a[100005];
int main()
{
    LL T,tt=0;
    scanf("%lld",&T);
    while(T--)
    {
        LL n,q,m,i,j,k,s=0,x,v;
        scanf("%lld%lld",&n,&q);
        for(i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
            s+=(n-2*i-1)*a[i];
        }
        printf("Case %lld:\n",++tt);
        for(i=0;i<q;i++)
        {
            scanf("%lld",&m);
            if(m==1)
                printf("%lld\n",s);
            else
            {
                scanf("%lld%lld",&x,&v);
                s-=(n-2*x-1)*a[x];
                s+=(n-2*x-1)*v;
                a[x]=v;
            }
        }
    }
    return 0;
}

// 1410 - Consistent Verdicts

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
#define pos 1e-12
LL x[707],y[707];
LL e[500000];
int main()
{
    LL T,tt=0;
    scanf("%lld",&T);
    while(T--)
    {
        LL i,j,k,t=0,s=1,n;
        scanf("%lld",&n);
        for(i=0;i<n;i++)
        {
            scanf("%lld%lld",&x[i],&y[i]);
            for(j=0;j<i;j++)
            {
                e[t++]=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
            }
        }
        sort(e,e+t);
        for(i=1;i<t;i++)
            if(e[i]-e[i-1]!=0)
                s++;
        printf("Case %lld: %lld\n",++tt,s+1);
    }
    return 0;
}
/*
题意:
告诉你n个人的坐标位置,n人同时开枪,能听到多少种不同的情况。自己不能听到自己的枪声
 
???n=1时答案是2,难道有回音?????
*/

// 1414 - February 29

#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
#define LL long long
#define pos 1e-12
map<string,LL>mm;
bool judge(LL year)
{
    if(year%4==0&&year%100!=0||year%400==0)
        return true;
    return false;
}
int main()
{
    mm["January"]=1;mm["February"]=2;mm["March"]=3;mm["April"]=4;
    mm[ "May"]=5;mm["June"]=6;mm["July"]=7;mm["August"]=8;
    mm["September"]=9;mm["October"]=10;mm["November"]=11;mm["December"]=12;
    LL T,tt=0;
    cin>>T;
    //scanf("%lld",&T);
    while(T--)
    {
        char s[10],ss[10],ch;
        LL d1,d2,y1,y2,sum=0,yy;
        cin>>s>>d1>>ch>>y1;
        cin>>ss>>d2>>ch>>y2;
       // scanf("%s%lld,%lld",s,&d1,&y1);
    //    scanf("%s%lld,%lld",ss,&d2,&y2);
        yy=y1;
        while(y1%4!=0)
            y1++;
        y2--;
        if(y2>=y1)
        {
            sum+=(y2-y1)/4+1;
            while(y1%100!=0)
                y1++;
            if(y2>=y1)
            {
                    sum-=(y2-y1)/100+1;
                while(y1%400!=0)
                    y1++;
                if(y2>=y1)
                sum+=(y2-y1)/400+1;
            }
        }
        if(judge(yy)&&mm[s]>=3)
        sum--;
        if(judge(y2+1)&&(mm[ss]==2&&d2==29||mm[ss]>2))
            sum++;
       // printf("Case %lld: %lld\n",++tt,sum);
       cout<<"Case "<<++tt<<": "<<sum<<endl;
    }
    return 0;
}

//1430  A Question of Time

没写出来,不想写了。

题意是输入三个时间,以第一个时间的时针所在直线为对称轴,在其余两个时间表示的时间段内找到一个时间,使得该时间的分针和时针关于对称轴对称。

结果可能出现分数,不过应该只有13为底的分数,需要注意下的是时间00:00:00,对称轴时针的反向延长线,测试数据组数很大。

你可能感兴趣的:(数论,基础题)