关闭

[qbxt考试Day6]小学数学 + Hash

59人阅读 评论(0) 收藏 举报
分类:

T1

求不超过n的因子最多的数

n <= 10^16
现在才知道反素数是什么东西……
1、因子个数与质因子大小无关
2、设X = P1^a1 * P2^a2 * P3^a3,因子数 = (a1 + 1) * (a2 + 1) *(a3 + 1)
综上,爆搜质因子指数,小的质因子指数>=大的质因子指数可保证答案不会变差

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define INF 1e9 + 7
#define LL long long
using namespace std;
const int MAXN = 1000 + 5;
LL n;
LL cnt[MAXN];
LL prime[] = {0,2,3,5,7,11,13,17,19,23,29,31};
LL num,ans;
void dfs(LL gs,LL sum,LL k)
{
    if(k > 12)return;
    if(gs > num || (gs == num && sum < ans))
        num = gs,ans = sum;
    cnt[k] = 0; 
    while(sum * prime[k] <= n && cnt[k] + 1 <= cnt[k - 1])
    {
        cnt[k] ++;
        sum *= prime[k];
        dfs(gs * (cnt[k] + 1),sum,k + 1);
    }
}
int main()
{
    freopen("divisors.in","r",stdin);
    freopen("divisors.out","w",stdout);
    scanf("%lld",&n);
    cnt[0] = INF;
    dfs(1,1,1);
    printf("%lld",ans);
    fclose(stdin);
    fclose(stdout);
    return 0;
}

T2

将一棵树中子树同构的节点编为同号,将编号按字典序输出

e.
In
9
1 2 2 1 5 5 7 7

Out
1 2 3 3 4 3 2 3 3

hash每一颗子树
每个节点的hash值为儿子的hash值的hash值
可以用map实现

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
const int MAXN = 100000 + 50;
int head[MAXN],next[MAXN << 1],tot,n;
struct edge
{
    int f,t;
}l[MAXN << 1];
void init(int n)
{
    for(int i = 1;i <= n;i ++)
        head[i] = -1;
}
void build(int f,int t)
{
    l[++ tot] = (edge){f,t};
    next[tot] = head[f];
    head[f] = tot;
}
int x;

map <vector<int>,int> id;
int cnt,num[MAXN];
int dfs(int u)
{
    vector <int> p;
    p.clear();
    for(int i = head[u];i != -1;i = next[i])
    {
        int t = l[i].t;
        p.push_back(dfs(t));
    }
    sort(p.begin(),p.end());//vector是有序的 
    if(!id[p])id[p] = ++ cnt;
    return num[u] = id[p];
}
int tmp,ans[MAXN];
bool vis[MAXN];
int main()
{
    freopen("tree.in","r",stdin);
    freopen("tree.out","w",stdout);
    scanf("%d",&n);
    init(n);
    for(int i = 1;i <= n - 1;i ++)
    {
        scanf("%d",&x);
        build(x,i + 1);
    }
    dfs(1);
    for(int i = 1;i <= n;i ++)
    {
        if(!vis[num[i]])
        {
            ans[num[i]] = ++ tmp;
            vis[num[i]] = true;
        }
    }
    for(int i = 1;i <= n;i ++)
    {
        printf("%d ",ans[num[i]]);
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3963次
    • 积分:612
    • 等级:
    • 排名:千里之外
    • 原创:58篇
    • 转载:1篇
    • 译文:0篇
    • 评论:4条