【HDU】 1930 And Now, a Remainder from Our Sponsor

And Now, a Remainder from Our Sponsor


题目链接


题目大意

    现在有4个互相互素的数m1,m2,m3,m4。而且有一句话需要你用这4个数去加密。加密方法:
    01代表A,02代表B,03代表C…..26代表Z,27代表空格,每三个数组成一个六位数(或是五位数),然后对这4个数分别取模,得到4个余数,余数不足2位数的首位用0补齐,然后这4个余数又组成一个八位数(或是七位数),现在告诉你四个加密用的数和密文,要你解密得到原文。


题解

    这一题把方程一列出来发现是个同余方程组,题目中的四个数又是互质的,直接剩余定理求解。
    注意用long long ,注意行末空格~


代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#define LL long long

using namespace std;

LL T,h,p[4],a[4],n,sta[10000000];
char s[12],s1[12],res[10000000];

LL extend_gcd(LL a,LL b,LL &x,LL &y)
{
    LL ans,t;
    if (b==0)
    {
        x=1; y=0;
        return a;
    }
    else ans=extend_gcd(b,a%b,x,y);
    t=x;
    x=y;
    y=t-(a/b)*y;
    return ans;
}

void c_reminder(LL a[],LL m[])
{
    LL M,mi,x,y,ans=0;
    char s[10];
    M=m[0]*m[1]*m[2]*m[3];
    for (int i=0;i<4;i++)
    {
        mi=M/m[i];
        extend_gcd(mi,m[i],x,y);
        x=(x%m[i]+m[i])%m[i];
        ans+=(mi*a[i]*x)%M;
    }
    ans=(ans%M+M)%M;
    itoa(ans,s,10);
    if (strlen(s)==5) strcat(res,"0");
    strcat(res,s);
}

int main()
{
    scanf("%I63d",&T);
    while (T--)
    {
        h=0;
        memset(a,0,sizeof(a));
        memset(p,0,sizeof(p));
        memset(res,0,sizeof(res));
        scanf("%I64d",&n);
        for (int i=0;i<4;i++) scanf("%I64d",&p[i]);
        for (int i=0;i<n;i++)
        {
            memset(s,0,sizeof(s));
            scanf("%s",&s1);
            int l=strlen(s1);
            if (l==7) s[0]='0';
            strcat(s,s1);
            a[0]=(s[0]-'0')*10+s[1]-'0';
            a[1]=(s[2]-'0')*10+s[3]-'0';
            a[2]=(s[4]-'0')*10+s[5]-'0';
            a[3]=(s[6]-'0')*10+s[7]-'0';
            c_reminder(a,p);
        }
        int l=strlen(res)-1;
        int t;
        while (l>0)
        {
            t=res[l]-'0'+(res[l-1]-'0')*10;
            l-=2;
            if (h==0 && t==27) continue;
            sta[h++]=t;
        }
        for (int i=h-1;i>=0;i--) if (sta[i]==27) printf(" ");
        else printf("%c",sta[i]+'A'-1);
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值