C - 與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個測試的計算結果。

Sample Input

5
4
5
6
7
8

Sample Output

30
55
91
91
155

記憶化遍歷,不然很容易超時 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define M 1000010
int  a[M];
long long i;
long long sum[M];
using namespace std;
int main()
{
	sum[0]=0;
	memset(a,0,sizeof(a));
	for(i=1;i<=1000000;i++)
	{
		long long temp=i;
		if(temp%7==0)
		{
			a[i]=1;
		}
		else
		{
			while(temp)
			{
				if(temp%10==7)
				{
					a[i]=1;
					break;
				}
				temp/=10;
			}
		}
		if(a[i]==0)
		{
			sum[i]=sum[i-1]+i*i;
		}
		else sum[i]=sum[i-1];
	}
	long long  x;
	int n;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%lld",&x);
		printf("%lld\n",sum[x]);
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章