touwangyi 2015-12-11 02:34 采纳率: 60%
浏览 552

hdu 3823 prime friend 求大神为什么wa,已经wa怕了

http://acm.hdu.edu.cn/showproblem.php?pid=3823

 #include<stdio.h>
#include<vector>
using namespace std;
#define M 20000010
bool pri[M] = {0};
int ans[M],temp = 0;
vector<int>fans[152];
int abs(int n)
{
    return n>0?n:-n;
}
void fun()
{
    for(int i = 2;i*i<M;i++)
    {
        if(pri[i]==true)
            continue;
        for(int j = 2*i;j<M;j+=i)
        {
            pri[j] = true;
        }
    }
    for(int i = 0;i<M;i++)
    {
        if(pri[i] == false)
        {
            ans[temp++] = i;
        }
    }
    for(int i = 0;i<temp-1;i++)
    {
        int t = ans[i+1]-ans[i];
        if(t<=150)
        {
            fans[t].push_back(ans[i+1]);
        }
    }
}
int main()
{
    fun();
    int n;
    scanf("%d\n",&n);
    int t = n;
    while(n--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        int ca = abs(a-b);
        int max = a>b?a:b;
        bool boo = false;
        for(int i = 0;i<fans[ca].size();i++)
        {
            if(fans[ca][i]>=max)
            {
                printf("Case %d: %d\n",t-n,fans[ca][i]-max);
                boo=true;
                break;
            }
        }
        if(boo==false)
        {
            printf("Case %d: -1\n",t-n);
        }
    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?