HDU 4474 (Yet Another Multiple Problem)同余模定理·
Yet Another Multiple Problem
Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 6311 Accepted Submission(s): 1465
Problem Description
There are tons of problems about integer multiples. Despite the fact that the topic is not original, the content is highly challenging. That’s why we call it “Yet Another Multiple Problem”.
In this problem, you’re asked to solve the following question: Given a positive integer n and m decimal digits, what is the minimal positive multiple of n whose decimal notation does not contain any of the given digits?
In this problem, you’re asked to solve the following question: Given a positive integer n and m decimal digits, what is the minimal positive multiple of n whose decimal notation does not contain any of the given digits?
Input
There are several test cases.
For each test case, there are two lines. The first line contains two integers n and m (1 ≤ n ≤ 104). The second line contains m decimal digits separated by spaces.
Input is terminated by EOF.
For each test case, there are two lines. The first line contains two integers n and m (1 ≤ n ≤ 104). The second line contains m decimal digits separated by spaces.
Input is terminated by EOF.
Output
For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) while Y is the minimal multiple satisfying the above-mentioned conditions or “-1” (without quotation marks) in case there does not exist such a multiple.
Sample Input
2345 3
7 8 9
100 1
0
Sample Output
Case 1: 2345
Case 2: -1
Source
【同余模定理】
对于大数求余,超出 (long long int 后) 直接用% 是不行的
但是有这样的定理
(a+b)%c = (a%c + b%c)%c
(a*b)%c = (a%c * b%c )%c
所以对于大数,我们可以这样:
例如 1234 % 9
1 % 9 = 1
1*10 +2 = 12 % 9= 3
3 * 10 + 3 =33 % 9= 6
6 * 10 + 4 =64 % 9= 1
所以1234 % 9 = 1;
就有 (x *n + d) % n 求法
【此题题意】
给n ,m 输入m个数
问 n的最小倍数 不含 这m 个数的 是多少
【思路】
bfs 搜索; 利于同余模定理 优化
【代码实现】
//#include <bits/stdc++.h> #include <iostream> #include <stdio.h> #include <algorithm> #include <cmath> #include <math.h> #include <cstring> #include <string> #include <queue> #include <stack> #include <stdlib.h> #include <list> #include <map> #include <set> #include <bitset> #include <vector> #define mem(a,b) memset(a,b,sizeof(a)) #define findx(x) lower_bound(b+1,b+1+bn,x)-b #define FIN freopen("input.txt","r",stdin) #define FOUT freopen("output.txt","w",stdout) #define S1(n) scanf("%d",&n) #define SL1(n) scanf("%I64d",&n) #define S2(n,m) scanf("%d%d",&n,&m) #define SL2(n,m) scanf("%I64d%I64d",&n,&m) #define Pr(n) printf("%d\n",n) #define lson rt << 1, l, mid #define rson rt << 1|1, mid + 1, r using namespace std; typedef long long ll; const double PI=acos(-1); const int INF=0x3f3f3f3f; const double esp=1e-6; const int maxn=1e6+5; const int MAXN=10005; const int MOD=1e9+7; const int mod=1e9+7; int dir[5][2]={0,1,0,-1,1,0,-1,0}; int n,m; int mods[MAXN]; int pre[MAXN]; int vis[10]; void cprint(int x) { if(pre[x]!=-1) { cprint(pre[x]); //printf("%d",mods[x]); } printf("%d",mods[x]); } void bfs() { queue<int>Q; for(int i=1;i<10;i++) { if(!vis[i]&&mods[i%n]==-1) { mods[i%n]=i; Q.push(i%n); } } while(!Q.empty()) { int u=Q.front();Q.pop(); if(u==0)// 结束 { cprint(u); printf("\n"); return ; } for(int i=0;i<10;i++) { if(vis[i]) continue; int x=(u*10+i)%n; if(mods[x]==-1)//没有被访问过 { mods[x]=i; pre[x]=u; Q.push(x); } } } printf("-1\n"); } int main() { int x; int cont=0; while(~scanf("%d %d",&n,&m)) { mem(mods,-1); mem(pre,-1); mem(vis,0); while(m--) { scanf("%d",&x); vis[x]=1; } printf("Case %d: ",++cont); bfs(); } return 0; }
1223
岂曰无衣?与子同袍。王于兴师,修我戈矛。与子同仇!
岂曰无衣?与子同泽。王于兴师,修我矛戟。与子偕作!
岂曰无衣?与子同裳。王于兴师,修我甲兵。与子偕行!