URAL1095 Nikifor 3

题意:给你N个正整数,它们都含有数码1、2、3、4,不超过20位,要求你调整每个正整数中数字的顺序,使它能被7整除。

最朴素的做法,暴搜它的每一种排列,但20位就挂了,肯定TLE。

注意题目中有条件每个正整数都有1,2,3,4,所以应该好好利用这个条件才对。

那么我们可以把1,2,3,4各取出一个,会发现它们组成的四位数模7的余数有7种。

我取了这样一组数4123,1324,4321,2341,1432,2413,4213,它们模7分别余0,1,2,3,4,5,6

剩下的数码随便排列就可以了(0放最后),用这7个数填补其它数码组成的数字模7剩余的部分即可。

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 const int rest[8]={4123,1324,4321,2341,1432,2413,4213,4123};
 5 int T;
 6 int count[10];
 7 char str[101];
 8 int main(){
 9     scanf("%d",&T);
10     while (T--){
11         memset(count,0,sizeof(count));
12         scanf("%s",str);int l=strlen(str);
13         for (int i=0;i<l;i++) {
14             count[int(str[i]-‘0‘)]++;
15         }
16         for (int i=1;i<=4;i++) count[i]--;
17         int now=0;
18         for (int i=9;i>=1;i--) {
19             for (int j=1;j<=count[i];j++)
20             {
21                 printf("%d",i);now=(now*10+i)%7;
22             }
23         }
24         printf("%d",rest[7-(now*10000)%7]);
25         for (int j=1;j<=count[0];j++) printf("0");
26         printf("\n");
27     }
28     return 0;
29 }
时间: 2024-10-13 01:14:32

URAL1095 Nikifor 3的相关文章

ural 1071. Nikifor 2

1071. Nikifor 2 Time limit: 1.0 secondMemory limit: 64 MB Nikifor has a number x. He doesn't need it. He needs a number y. Nikifor tries to obtain the required number by erasing some digits from x. But he is not lucky in the meanwhile. May be he is t

timus 1095 Nikifor 3【思路】

传送门:timus 1095 题意:给出一个数字(可能是大数),其中1,2,3,4每个数至少出现一次,将大数数字重新排列,输出一个能被7整除且不以0为前缀的结果,如果没有,则输出0 思路: 利用1,2,3,4至少出现一次的信息先枚举一下他们的组合mod7的情况,因为所有整形数mod7结果只能出现7种情况 7是一个不一般的数啊 不一般体现在这里 ( 10%7=3 20%7=6 30%7=2; 40%7=5; 50%7=1; 60%7=4;) 这个可以先通过写测试代码来模拟相应的结果 int i,j

URAL 1180. Stone Game (博弈 + 规律)

1180. Stone Game Time limit: 1.0 second Memory limit: 64 MB Two Nikifors play a funny game. There is a heap of N stones in front of them. Both Nikifors in turns take some stones from the heap. One may take any number of stones with the only condition

URAL1119——DP——Metro

Description Many of SKB Kontur programmers like to get to work by Metro because the main office is situated quite close the station Uralmash. So, since a sedentary life requires active exercises off-duty, many of the staff — Nikifor among them — walk

timus 1180. Stone Game 解题报告

1.题目: 1180. Stone Game Time limit: 1.0 secondMemory limit: 64 MB Two Nikifors play a funny game. There is a heap of N stones in front of them. Both Nikifors in turns take some stones from the heap. One may take any number of stones with the only cond

第一轮 J

<span style="color:green;"></span>Metro<span id="crawlSuccess" style="display: inline;" class="crawlInfo"><strong>Time Limit:</strong><span id="timeLimit">500</span&g

ural 1119 Metro dp水

点击打开链接 1119. Metro Time limit: 0.5 second Memory limit: 64 MB Many of SKB Kontur programmers like to get to work by Metro because the main office is situated quite close the station Uralmash. So, since a sedentary life requires active exercises off-d

Timus 1180. Stone Game 游戏题目

Two Nikifors play a funny game. There is a heap of N stones in front of them. Both Nikifors in turns take some stones from the heap. One may take any number of stones with the only condition that this number is a nonnegative integer power of 2 (e.g.

Timus 1119. Metro 动态规划

Many of SKB Kontur programmers like to get to work by Metro because the main office is situated quite close the station Uralmash. So, since a sedentary life requires active exercises off-duty, many of the staff - Nikifor among them - walk from their