zoj 1136 Multiple

本文介绍了一种使用数组优化的余数计算与输出算法,通过比较和排序,快速找到符合条件的余数并输出结果。适用于大规模数据处理场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

a%n==x

(a*10+b)%n==(x*10+b)%b

要用数组存下余数优化,如果遇到余数为0代表算对了

当所有的余数都算了,就是没有0的时候,就直接输出0

#include <bits/stdc++.h>
using namespace std;
struct str
{
    string s;
    int res;
};
int cmp(str a,str b)
{
    return a.res<b.res;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n;
    while(scanf("%d",&n)!=EOF){

        int m;scanf("%d",&m);
        str as[100];
        queue<str> q;
        int m2=0;
        for (int i=0;i<m;i++){
            scanf("%d", &as[i].res);
            as[i].s+=char('0'+as[i].res);
            //数可能很大存不下来
        }

        sort(as,as+m,cmp);//排序找最小的值

        if (n==0){
            printf("0\n");
            continue;
        }

        for (int i=0;i<m;i++){
            q.push(as[i]);
        }

        str ans;
        int mark[5005]={0};
        int num=0;
        int ok=0;

        while(!q.empty()){
            ans = q.front();
            q.pop();
            if (ans.s=="0") continue;
            if (ans.res==0){
                cout<<ans.s<<endl;
                ok=1;
                break;
            }

            if (mark[ans.res]) continue;
            //计算过了,,

            mark[ans.res]=1;

            for (int i=0;i<m;i++){
                str ans2;
                ans2.res=(ans.res*10+as[i].res)%n;
                ans2.s=ans.s+as[i].s;
                if (mark[ans2.res]) continue;
                //计算过了
                q.push(ans2);
            }
        }

        if (!ok) printf("0\n");

        while(!q.empty()){
            q.pop();
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值