51Nod_1082 与7无关的数
http://www.51nod.com/Challenge/Problem.html#!#problemId=1082
题目
一个正整数,如果它能被7整除,或者它的十进制表示法中某个位数上的数字为7,则称其为与7相关的数。求所有小于等于N的与7无关的正整数的平方和。例如:N = 8,<= 8与7无关的数包括:1 2 3 4 5 6 8,平方和为:155。
输入
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 1000)。第2 - T + 1行:每行1个数N。(1 <= N <= 10^6)
输出
共T行,每行一个数,对应T个测试的计算结果。
样例输入
5
4
5
6
7
8
样例输出
30
55
91
91
155
分析
按题模拟即可。
C++程序
#include<iostream>
using namespace std;
typedef unsigned long long ULL;
const int N=1e6;
ULL a[N+1];
bool isSeven(int n)
{
if(n%7==0)
return true;
while(n){
int m=n%10;
if(m==7)
return true;
n/=10;
}
return false;
}
void maketable()
{
for(ULL i=1;i<=N;i++)
if(isSeven(i))
a[i]=0;
else
a[i]=1;
for(ULL i=1;i<=N;i++)
a[i]=a[i-1]+(a[i]==1?i*i:0);
}
int main()
{
maketable();
int n,t;
cin>>t;
while(t--){
cin>>n;
cout<<a[n]<<endl;
}
return 0;
}