51nod 1082 与7无关的数【打表/预处理】

题目来源: 有道难题
基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
 收藏
 关注
一个正整数,如果它能被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
【分析】:1.注意数据(1 <= N <= 10^6),时限1s,之前纯暴力,悲催的TLE了,还是要打表预处理!2.注意RE也许是数组开小了,我把大小开成1e5结果RE了!!后来1e6才过!
【代码】:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define maxn 1000000+10
using namespace std;
typedef long long LL;
LL f[maxn];
int check(int n)
{

    if(n%7==0) return 1;
    while(n)
    {
        LL ans;
        ans=n%10;
        if(ans==7)
        {
            return 1;
        }
        n/=10;
    }
    return 0;
}
int main()
{
   LL n;
   int t;
   memset(f,0,sizeof(f));

   for(int i=1;i<=maxn;i++)
   {
       if(check(i))
        f[i]=f[i-1];
       else
        f[i]=f[i-1]+(LL)i*i;
   }

   scanf("%d",&t);
   while(t--)
   {
       LL sum=0;
       scanf("%lld",&n);
       printf("%lld
",f[n]);
   }
   return 0;
}
View Code
原文地址:https://www.cnblogs.com/Roni-i/p/7711073.html