51nod--1082 与7无关的数

一个正整数,如果它能被7整除,或者它的十进制表示法中某个位数上的数字为7,则称其为与7相关的数。求所有小于等于N的与7无关的正整数的平方和。
 
例如:N = 8,<= 8与7无关的数包括:1 2 3 4 5 6 8,平方和为:155。
Input
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 1000)
第2 - T + 1行:每行1个数N。(1 <= N <= 10^6)
Output
共T行,每行一个数,对应T个测试的计算结果。
Input示例
5
4
5
6
7
8
Output示例
30
55
91
91
155

因为范围有点大,所以暴力估计会T,所以预处理一下,把与7有关的数存起来,然后计算和减去与7有关的数
/*
    data:2018.7.14
    author:authorgsw
    link:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1082
*/
#define ll long long
#define IO ios::sync_with_stdio(false);

#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
ll vis[1000005];int len=0;
bool judge(ll n)
{
    if(n%7==0)return true;
    while(n!=0)
    {
        if(n%10==7)return true;
        n=n/10;
    }
    return false;
}
void init()
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<1000005;i++)
        if(judge(i))vis[len++]=i;
}
int main()
{
    int t;ll n;
    init();
    scanf("%d",&t);
    while(t--)
    {
        ll ans=0;
        scanf("%lld",&n);
        ans=n*(n+1)*(2*n+1)/6;
        for(int i=0;i<len;i++)
        {
            if(vis[i]>n)break;
            ans=ans-vis[i]*vis[i];
        }
        printf("%lld\n",ans);
    }
}

 

 

转载于:https://www.cnblogs.com/fantastic123/p/9311194.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值