51Nod 1082 与7无关的数

打表,先把小于等于 N 的且与 7 无关的正整数找出来,然后再预处理出小于等于N的这些正整数平方和。

const int N=1e6+10;
bool vis[N];
LL sum[N];

bool check(int x)
{
    if(x % 7 == 0) return true;

    while(x)
    {
        int t=x%10;
        if(t == 7) return true;
        x/=10;
    }
    return false;
}

void init()
{
    for(int i=1;i<N;i++)
        if(check(i))
            vis[i]=true;

    LL res=0;
    for(int i=1;i<N;i++)
    {
        if(!vis[i]) res+=(LL)i*i;
        sum[i]=res;
    }
}

int main()
{
    init();

    int T;
    cin>>T;
    while(T--)
    {
        int x;
        cin>>x;
        cout<<sum[x]<<endl;
    }
    //system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/fxh0707/p/14618830.html