题目链接:数7
题目描述
1337个人排成一个圈,从1号人开始报数,初始方向是1,2,3…。如果某个人报的数是7的倍数或数字中含有7,那么报数的方向就反一下。问报数字 X X X的是哪个人?
比如初始20个数字的情况是:
1:1 2:2 3:3 4:4 5:5 6:6 7:7 6:8 5:9 4:10 3:11 2:12 1:13 1337:14 1:15 2:16 3:17 2:18 1:19 1337:20
输入格式
一行一个数 X X X。
输出格式
一行一个数表示最终报数字 X X X的是哪个人。
输入输出样例
输入
1000
输出
1311
【数据规模】
对于30%的数据,满足
X
≤
1
0
6
X≤10^6
X≤106;
对于90%的数据,满足
X
≤
1
0
8
X≤10^8
X≤108;
对于100%的数据,满足
X
≤
1
0
9
X≤10^9
X≤109
题目思路
1.打表大法:暴力模拟
2.隔断打表:
我们把整个大的区间分为若干个,用暴力算一下每个区间的第一个数,然后在这第一个数的基础上用暴力方法计算。
代码
暴力模拟
/*************************************************
Note:
*************************************************/
#include <queue>
#include <stack>
#include <set>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <iomanip>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#define ll long long
#define ull unsigned long long
using namespace std;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
#define REG register
#define IN inline
#define For(x, y, z) for (REG int(x) = (y); (x) <= (z); ++(x))
#define FOR(x, y, z) for (REG int(x) = (y); (x) < (z); ++(x))
const int kmax_num = 1e3 + 10, kmax_int = INT_MAX, kmod = 10007;
inline int read()
{
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
int f[kmax_num] = {
0, 469, 139, 1086, 768, 860, 1200, 837, 837, 507, 117,
1136, 1228, 231, 1205, 337, 7, 954, 954, 862, 522,
885, 416, 746, 1136, 117, 25, 1022, 1022, 154, 1161,
771, 453, 545, 885, 522, 991, 661, 661, 979, 887,
547, 910, 441, 771, 1161, 142, 50, 50, 1024, 156,
1163, 773, 455, 547, 887, 524, 993, 993, 46, 364,
272, 1269, 295, 1163, 156, 546, 864, 864, 1204, 841,
841, 841, 841, 841, 841, 841, 841, 841, 841, 841,
523, 615, 955, 592, 1061, 731, 341, 341, 249, 1246,
272, 1140, 133, 523, 841, 749, 409, 409, 878, 548,
158, 1177, 1269, 272, 1246, 378, 48, 48, 366, 274,
1271, 297, 1165, 158, 548, 866, 774, 774, 411, 880,
550, 160, 1179, 1271, 274, 1248, 380, 380, 770, 1088,
996, 656, 1019, 550, 880, 1270, 251, 251, 591, 228,
697, 367, 1314, 996, 1088, 91, 1065, 1065, 735, 345,
27, 119, 459, 96, 565, 235, 1182, 1182, 1090, 750,
1113, 644, 974, 27, 345, 253, 1250, 1250, 382, 52,
52, 52, 52, 52, 52, 52, 52, 52, 52, 52,
1049, 75, 943, 1273, 326, 644, 552, 552, 189, 658,
328, 1275, 957, 1049, 52, 1026, 158, 158, 548, 866,
774, 434, 797, 328, 658, 1048, 29, 29, 369, 6,
475, 145, 1092, 774, 866, 1206, 843, 843, 513, 123,
1142, 1234, 237, 1211, 343, 13, 960, 960, 868, 528,
891, 422, 752, 1142, 123, 31, 1028, 1028, 160, 1167,
777, 459, 551, 891, 528, 997, 667, 667, 985, 893,
553, 916, 447, 777, 1167, 148, 56, 56, 1030, 162,
1169, 779, 461, 553, 893, 530, 999, 999, 52, 370,
370, 370, 370, 370, 370, 370, 370, 370, 370, 370,
839, 509, 119, 1138, 1230, 233, 1207, 1207, 877, 487,
169, 261, 601, 238, 707, 377, 1324, 1324, 1232, 892,
1255, 786, 1116, 169, 487, 395, 55, 55, 524, 194,
1141, 823, 915, 1255, 892, 24, 1031, 1031, 12, 1257,
917, 1280, 811, 1141, 194, 512, 420, 420, 57, 526,
196, 1143, 825, 917, 1257, 894, 26, 26, 416, 734,
642, 302, 665, 196, 526, 916, 1234, 1234, 237, 1211,
343, 13, 960, 642, 734, 1074, 711, 711, 381, 1328,
1010, 1102, 105, 1079, 211, 1218, 828, 828, 736, 396,
396, 396, 396, 396, 396, 396, 396, 396, 396, 396,
786, 1104, 1012, 672, 1035, 566, 896, 896, 578, 670,
1010, 647, 1116, 786, 396, 78, 170, 170, 533, 64,
394, 784, 1102, 1010, 670, 1033, 564, 564, 174, 1193,
1285, 288, 1262, 394, 64, 1011, 693, 693, 353, 716,
247, 577, 967, 1285, 1193, 853, 1216, 1216, 209, 599,
917, 825, 485, 848, 379, 709, 1099, 1099, 1191, 194,
1168, 300, 1307, 917, 599, 691, 1031, 1031, 562, 892,
1282, 263, 171, 1168, 194, 1062, 55, 55, 1074, 1166,
169, 1143, 275, 1282, 892, 574, 666, 666, 1029, 560,
560, 560, 560, 560, 560, 560, 560, 560, 560, 560,
652, 992, 629, 1098, 768, 378, 60, 60, 1057, 83,
951, 1281, 334, 652, 560, 220, 583, 583, 913, 1303,
284, 192, 1189, 215, 1083, 76, 466, 466, 558, 898,
535, 1004, 674, 284, 1303, 58, 398, 398, 1266, 259,
649, 967, 875, 535, 898, 429, 759, 759, 441, 533,
873, 510, 979, 649, 259, 1278, 33, 33, 396, 1264,
257, 647, 965, 873, 533, 896, 427, 427, 37, 1056,
1148, 151, 1125, 257, 1264, 874, 556, 556, 216, 579,
110, 440, 830, 1148, 1056, 716, 1079, 1079, 72, 462,
462, 462, 462, 462, 462, 462, 462, 462, 462, 462,
99, 568, 238, 1185, 867, 959, 1299, 1299, 830, 1160,
213, 531, 439, 99, 462, 1330, 323, 323, 5, 97,
437, 74, 543, 213, 1160, 842, 934, 934, 1297, 828,
1158, 211, 529, 437, 97, 460, 1328, 1328, 938, 620,
712, 1052, 689, 1158, 828, 438, 120, 120, 1117, 143,
1011, 4, 394, 712, 620, 280, 643, 643, 973, 26,
344, 252, 1249, 275, 1143, 136, 526, 526, 618, 958,
595, 1064, 734, 344, 26, 118, 458, 458, 1326, 319,
709, 1027, 935, 595, 958, 489, 819, 819, 501, 593,
593, 593, 593, 593, 593, 593, 593, 593, 593, 593,
263, 1210, 892, 984, 1324, 961, 93, 93, 483, 801,
709, 369, 732, 263, 593, 983, 1301, 1301, 304, 1278,
1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278,
1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278,
1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278,
1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278,
1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278,
1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278,
1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278,
1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278,
1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278,
1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278, 1278,
331, 649, 557, 217, 580, 111, 441, 441, 123, 215,
555, 192, 661, 331, 1278, 960, 1052, 1052, 78, 946,
1276, 329, 647, 555, 215, 578, 109, 109, 1056, 738,
830, 1170, 807, 1276, 946, 556, 238, 238, 1235, 261,
1129, 122, 512, 830, 738, 398, 761, 761, 1091, 144,
462, 370, 30, 393, 1261, 254, 644, 644, 736, 1076,
713, 1182, 852, 462, 144, 236, 576, 576, 107, 437,
437, 437, 437, 437, 437, 437, 437, 437, 437, 437,
777, 414, 883, 553, 163, 1182, 1274, 1274, 300, 1168,
161, 551, 869, 777, 437, 800, 331, 331, 1278, 960,
1052, 55, 1029, 161, 1168, 778, 460, 460, 120, 483,
14, 344, 734, 1052, 960, 620, 983, 983, 1313, 366,
684, 592, 252, 615, 146, 476, 866, 866, 958, 1298,
935, 67, 1074, 684, 366, 458, 798, 798, 329, 659,
1049, 30, 1275, 935, 1298, 829, 1159, 1159, 841, 933,
1273, 910, 42, 1049, 659, 341, 433, 433, 796, 327,
657, 1047, 28, 1273, 933, 1296, 827, 827, 437, 119,
119, 119, 119, 119, 119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, 0, 1, 0, 0, 1, 0, 1};
inline bool Judge(REG int num)
{
if (!(num % 7))
return true;
while (num)
{
if (num % 10 == 7)
return true;
else
num /= 10;
}
return false;
}
int main()
{
REG int n, now;
REG bool dir;
scanf(" %d", &n);
now = f[n / 1000000];
dir = d[n / 1000000];
For(i, n / 1000000 * 1000000 + 1, n)
{
if (!dir)
{
--now;
if (!now)
now = 1337;
if (Judge(i))
dir = 1;
}
else if (dir)
{
++now;
if (now == 1338)
now = 1;
if (Judge(i))
dir = 0;
}
}
printf("%d\n", now);
return 0;
}
隔断打表
/*************************************************
Note:
*************************************************/
#include <queue>
#include <stack>
#include <set>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <iomanip>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#define ll long long
#define ull unsigned long long
using namespace std;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
inline int read()
{
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
int n, i, j, k, t, pd, m;
int ha(int x, int y, int z, int a)
{
t = z;
for (i = x; i <= y; i++)
{
t += a;
if (i % 7 == 0)
a = -a;
else
{
k = i;
while (k)
{
if (k % 10 == 7)
{
a = -a;
break;
}
k /= 10;
}
}
if (t == 0)
t = 1337;
if (t == 1338)
t = 1;
}
printf("%d", t);
}
int main()
{
// freopen("a.in","r",stdin);
// freopen("seven.out","w",stdout);
scanf("%d", &n);
if (n < 5e7)
{
ha(1, n, 0, 1);
}
else if (n < 1e8)
{
ha(5e7, n, 157, -1);
}
else if (n < 1e8 + 5e7)
{
ha(1e8, n, 547, 1);
}
else if (n < 2e8)
{
ha(1e8 + 5e7, n, 346, -1);
}
else if (n < 2e8 + 5e7)
{
ha(2e8, n, 867, -1);
}
else if (n < 3e8)
{
ha(2e8 + 5e7, n, 892, 1);
}
else if (n < 3e8 + 5e7)
{
ha(3e8, n, 893, -1);
}
else if (n < 4e8)
{
ha(3e8 + 5e7, n, 1212, -1);
}
else if (n < 4e8 + 5e7)
{
ha(4e8, n, 63, 1);
}
else if (n < 5e8)
{
ha(4e8 + 5e7, n, 893, -1);
}
else if (n < 5e8 + 5e7)
{
ha(5e8, n, 1302, -1);
}
else if (n < 6e8)
{
ha(5e8 + 5e7, n, 1055, 1);
}
else if (n < 6e8 + 5e7)
{
ha(6e8, n, 98, -1);
}
else if (n < 7e8)
{
ha(6e8 + 5e7, n, 957, 1);
}
else if (n < 7e8 + 5e7)
{
ha(7e8, n, 1279, -1);
}
else if (n < 8e8)
{
ha(7e8 + 5e7, n, 1279, -1);
}
else if (n < 8e8 + 5e7)
{
ha(8e8, n, 1279, -1);
}
else if (n < 9e8)
{
ha(8e8 + 5e7, n, 143, 1);
}
else if (n < 9e8 + 5e7)
{
ha(9e8, n, 959, 1);
}
else if (n <= 1e9)
{
ha(9e8 + 5e7, n, 934, -1);
}
return 0;
}