码迷,mamicode.com
首页 > 其他好文 > 详细

51nod--1082 与7无关的数

时间:2018-07-14 23:04:47      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:ble   lld   png   for   under   input   using   html   ems   

一个正整数,如果它能被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);
    }
}

 

 

51nod--1082 与7无关的数

标签:ble   lld   png   for   under   input   using   html   ems   

原文地址:https://www.cnblogs.com/fantastic123/p/9311194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!