题意给你N个正整数,它们都含有数码1、2、3、4,不超过20位,要求你调整每个正整数中数字的顺序,使它能被7整除。
暴力:妥妥tle
然后就是,可以构造这个东西,你就安排前面几位,然后最后mol%7,有7个数,0 ,1 ,2, 3, 4, 5 ,6, 然后用1,2,3,4构造出来7个数,来填补他们就行了,别忘了用(x % n + i)%n = (10*x + i)%n然后就行了
我是用的4123 , 1324 , 4321 , 2341 , 1432 , 2413 , 4213 ,4123。不要忘了最后排一下0,然后就是一定要打到7,因为,可能最后now是0,然后7-0,我一开始就没考虑这种情况。。。。QAQ
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int cou[10];
int rest[8] = {4123 , 1324 , 4321 , 2341 , 1432 , 2413 , 4213 ,4123};
char str[22];
int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(cou , 0 , sizeof(cou));
scanf("%s",str);
for(int i = 0 ; i < strlen(str) ; i++){
cou[str[i] - '0']++;
}
for(int i = 1 ; i <= 4 ; i++)
cou[i]--;
long long now = 0;
for(int i = 9 ; i > 0 ; i--){
for(int j = 0 ; j < cou[i] ; j++){
printf("%d",i);
now = (now * 10 + i) % 7;
}
}
printf("%d",rest[7 - (now * 10000) % 7]);
for(int i = 1 ; i <= cou[0] ; i++) printf("0");
printf("\n");
}
return 0;
}