![](http://file.51nod.com/images/icon/ok.png)
题目来源:
基准时间限制:1 秒 空间限制:131072 KB 分值: 5
![](http://file.51nod.com/images/icon/star.png)
![](http://file.51nod.com/images/icon/plus.png)
一个正整数,如果它能被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示例
545678
Output示例
30559191155 【分析】:1.注意数据(1 <= N <= 10^6),时限1s,之前纯暴力,悲催的TLE了,还是要打表预处理!2.注意RE也许是数组开小了,我把大小开成1e5结果RE了!!后来1e6才过!
【代码】:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include#include #include #include #include #define maxn 1000000+10using 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\n",f[n]); } return 0;}