KIDx的解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1979
题意:
打表可知只有200+个4位逆素数,然后枚举四个4位逆素数然后暴力检验一下,我的剪枝可能不够直接超时了T-T,打个表存在数组中处理下即可,下面是我的超时代码(只能用来打表了):
#include <iostream>
using namespace std;
#define M 10000
int p[1300], vis[M], has[M];
int cal (int i, int j, int a, int b, int tp)
{
return p[i]/tp%10*1000 + p[j]/tp%10*100 + p[a]/tp%10*10 + p[b]/tp%10;
}
int main()
{
//freopen ("test.txt", "w", stdout);
int i, j, k = 0, a, b;
for (i = 2; i < M; i++)
if (!vis[i])
for (j = i+i; j < M; j+=i)
vis[j] = 1;
for (i = 1000; i < M; i++)
{
if (!vis[i])
{
int tp = 0, j = i;
while (j)
{
tp *= 10;
tp += j % 10;
j /= 10;
}
if (!vis[tp]) //逆过来也是素数的话就是逆素数
{
p[k++] = i;
has[i] = 1;
}
}
}
//cout << k << endl; for (i = 0; i < 100; i++) cout << p[i] << endl;
for (i = 0; i < k; i++)
{
int x = p[i]/10%10, y = p[i]/100%10;
if (x % 2 == 0 || y % 2 == 0 || x == 5 || y == 5) continue;
for (j = 0; j < k; j++)
{
for (a = 0; a < k; a++)
{
for (b = 0; b < k; b++)
{
x = p[b]/10%10, y = p[b]/100%10;
if (x % 2 == 0 || y % 2 == 0 || x == 5 || y == 5) continue;
x = cal (i, j, a, b, 1);
if (!has[x]) continue;
x = cal (i, j, a, b, 10);
if (!has[x]) continue;
x = cal (i, j, a, b, 100);
if (!has[x]) continue;
x = cal (i, j, a, b, 1000);
if (!has[x]) continue;
x = p[i]/1000%10*1000 + p[j]/100%10*100 + p[a]/10%10*10 + p[b]%10;
if (!has[x]) continue;
x = p[i]%10 + p[j]/10%10*10 + p[a]/100%10*100 + p[b]/1000%10*1000;
if (!has[x]) continue;
printf ("%d\n%d\n%d\n%d\n\n", p[i], p[j], p[a], p[b]);
}
}
}
}
return 0;
}
分享到:
相关推荐
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 1166线段树代码
HDU最全ac代码
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. Input Input...
hdu动态规划算法集锦
自己做的HDU ACM已经AC的题目
hdu题目分类
HDU图论题目分类
hdu-acm源代码(上百题)hdu-acm源代码、hdu-acm源代码hdu-acm源代码