// 杭电上的重现赛:http://acm.hdu.edu.cn/contests/contest_show.php?cid=867

 // 杭电6555~6566可交题

A - The Fool 

题目大意:

求∑(1,n) [n/i] 的奇偶性。

分析及代码:

这个求和可以分块计算,复杂度O(√N),完全可行。

我觉得是水题就打表找规律了,发现前3项1~3结果是奇数,接着5项4~8结果是偶数,再接着7项是奇数,再然后9项时偶数......如此交替。

那么只需要计算n在哪一段就能确定奇偶性了,时间复杂度O(1)。

 

AC代码:

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int main() {
    int t = 0, T; cin>>T;
    while(t<T) {
        int n;
        scanf("%d", &n);
        int k = sqrt(n+1);
        if(k*k<n+1) ++k;


        printf("Case %d: ", ++t);
        if(k&1) printf("even\n");
        else printf("odd\n");
    }

    return 0;
}
View Code

相关文章:

  • 2021-06-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2021-10-18
猜你喜欢
  • 2022-12-23
  • 2021-07-16
  • 2021-07-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案