• 2014百度之星初赛第二轮解题报告:JZP set


    JZP Set
    时间限制:5s  内存限制:256MB

    问题描述
    一个{1, ..., n}的子集S被称为JZP集,当且仅当对于任意S中的两个数x,y,若(x+y)/2为整数,那么(x+y)/2也属于S
    例如,n=3S={1,3}不是JZP集,因为(1+3)/2=2不属于S。但是{1,2,3}的其他子集都属于S,所以n=3时有7JZP
    给定n,求JZP集的个数。

    输入
    第一行为T,表示输入数据组数。
    每组数据包含一行整数n

    输出
    对第i组数据,输出
    Case #i:
    然后输出JZP集的个数。

    限制条件
    1<=T<=10^5
    1<=n<=10^7

    样例输入
    3
    1
    2
    3

    样例输出
    Case #1:
    2
    Case #2:
    4
    Case #3:
    7


    解题报告:
    Jzp集的充分必要条件是差为奇数的等差数列,于是可以简单递推算出。
    q[n]=q[n-1]*2-q[n-2]+(n-1的奇约数的数量)


    解题代码:

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cassert>
    #include <ctime>
    #include <set>
    #include <map>
    #include <vector>
    #include <string>
    #include <sstream>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    typedef long long ll;
    typedef pair<int,int> PII;
    
    #define fi first
    #define se second
    #define mp make_pair
    #define pb push_back
    
    #define N 10000010
    
    int p[N/10], L; ll s[N]; 
    bool a[N]; 
    
    int main()
    {
            s[1] = 1; s[2] = 1; 
            a[4] = 1; s[4] = 1; p[L++] = 2; 
            for (int i = 3; i < N; i ++) {
                    if (!a) {
                            p[L++] = i; 
                            s = 2; 
                    }
                    for (int j = 0; j < L; j ++) {
                            if (p[j]*i >= N) break; 
                            a[p[j]*i] = 1; 
                            if (j == 0) s[i*2] = s; else {
                                    int z = 1, c = i; 
                                    while (c%p[j] == 0) c/=p[j], z ++; 
                                    s[p[j]*i] = s[c] * (z+1); 
                            }
                            if (i%p[j] == 0) break; 
                    }
            }
            
            for (int i = 1; i < N; i ++) 
                    s = s[i-1] + s; 
            s[0] = 2; 
            for (int i = 1; i < N; i ++) 
                    s = s[i-1] + s + 1; 
            
            int q; scanf("%d", &q); 
            int T = 1;
            while (q --) {
                    int n; scanf("%d", &n); 
                    printf ("Case #%d:
    %I64d
    ", T++, s[n-1]); 
            }
            return 0;
    }

  • 相关阅读:
    kaggle编码categorical feature总结
    sklearn---DBSCAN
    Panda to Panda.Dataframe?
    pandas的分组取最大多行并求和函数nlargest()
    26.异常检测---孤立森林 | one-class SVM
    PowerShell----Automatic_Variables(预定义变量)
    powershell---高级函数的介绍
    PowerShell中的配置文件
    PowerShell如何使用自定义公共函数
    探索PowerShell----函数
  • 原文地址:https://www.cnblogs.com/hosealeo/p/4190506.html
Copyright © 2020-2023  润新知