51nod 1082 Numbers unrelated to 7 (table)

Topic source:  You Dao Difficulties
Base Time Limit: 1 second Space Limit: 131072 KB Score: 5Difficulty  : Level 1 Algorithm Question
 collect
 focus on
A positive integer is said to be related to 7 if it is divisible by 7, or if its decimal representation has a digit of 7 at a certain number of digits. Find the sum of the squares of all positive integers less than or equal to N that are independent of 7.
For example: N = 8, <= 8 The numbers that have nothing to do with 7 include: 1 2 3 4 5 6 8, and the sum of squares is: 155.
Input
Line 1: A number T representing the number of numbers to use later as input tests. (1 <= T <= 1000)
Line 2 - T + 1: 1 number N per line. (1 <= N <= 10^6)
Output
There are T rows in total, each row has a number, corresponding to the calculation results of T tests.
Input example
5
4
5
6
7
8
Output example
30
55
91
91
155

At first, I just checked the meter to determine whether it was related to 7, A, but the time was 921ms.

#include<stdio.h>
#include<string.h>
using namespace std;
#define maxn 1000005
long long mat[maxn];
void fun()
{
	memset(mat,0,sizeof(mat));
	for(int i=1;i<maxn;i++)
	{
		if(i%7==0)
		{
			mat [i] = mat [i-1];
		}
		else
		{
			int flag=0;
			int res=i;
			while(res)
			{
				int ans = res% 10;
				if(ans==7)
				{
					mat [i] = mat [i-1];
					flag=1;
					break;
				}
				res = res / 10;
			}
			if(flag==0)//Note the cast type
			mat[i]=mat[i-1]+(long long)i*i;
		}
	}
}
intmain()
{
	int n,t;
	fun();
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		printf("%lld\n",mat[n]);
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325939251&siteId=291194637