A Partial Order Relation

博客介绍了偏序集概念,即集合元素的一种排序关系,并非所有元素对都可比。还定义了最大整除关系,给定数可构建其约数间的该关系图。题目要求根据输入整数计算其最大整除关系图的边数,题解是将数表示为质数之积,统计质数出现个数来计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

In mathematics, a partial order set formalizes and generalizes the intuitive concept of an ordering, sequencing, or arrangement of elements of a set. It is a binary relation indicating that, for certain pairs of elements in a set, one of the elements precedes the other in the ordering but not every pair is comparable. For example, the divisors of 120 form a partial order set. Let the binary relation ≺ be divisibility relation. So, 120 ≺ 24 and 120 ≺ 40 but there is no such relation between 24 and 40. ≺ is a transitive relation such that 120 ≺ 40 and 40 ≺ 8 imply 120 ≺ 8.
Let’s define a more restrictive binary relation called greatest divisibility relation . Given a number a. Let div(a) be a set of all divisors of a minus a. Then we define a b if b is a divisor of a but b cannot divide any other numbers in div(a). For example, div(30) = {1, 2, 3, 5, 6, 10, 15}. 30  6 because 6 cannot be used to divide other elements in the set.
Given a number, you can construct  relation among the its divisors as in Fig. 1 for 120. The depth of the graph from root node (120) to the terminal node (always 1) is 5 and
the number of edges are 28. Given an integer, please compute the number of edges in its  relation graph.

 

 

输入

The input data begins with a number n (n <= 100), which is the number of test cases. Each test case contains only a positive integer r, where 1 < r < 240 . r is the number to build  relation.

 

输出

For each test case, please print the number of edges in its divisibility relation.


样例输入

2
6
120

样例输出

4
28

题解:可以算是一道数论思维题,将给出的数表示成质数之积的形式,然后统计每个质数出现的个数,例如:24=2*2*2*3,2出现了三次,3出现了一次,首先选择2有三种选法,3仅有选或不选两种情况,所以对2有2*3=6种情况,对3的时候选2的一种情况或者不选(即选1)所以有4种共10种情况。有多个质因数的情况可以类比,下面上代码。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inx=1e6+7;
ll a[2000000],b[200000],y[200][2];
void pri()
{
    ll i,j,l=0;
    for(i=2; i<=inx; i++)
    {
        if(a[i]==0)
        {
            b[++l]=i;
            for(j=i+i; j<=inx; j+=i)
            {
                a[j]=1;
            }
        }
    }
}
ll da(ll n)
{
    ll fa=n;
    ll x=0;
    memset(y,0,sizeof(y));
    for(ll i=1; b[i]*b[i]<=fa; i++)
    {
        if(fa%b[i]==0)
        {
            y[x][0]=b[i];
            while(fa%b[i]==0)
            {
                y[x][1]++;
                fa/=b[i];
            }
            x++;
        }
    }
    if(fa!=1)
    {
        y[x][0]=fa;
        y[x][1]++;
        x++;
    }
    return x;
}
int main()
{
    ll i,j,k,l;
    ll n,t;
    scanf("%lld",&t);
    pri();
    while(t--)
    {
        scanf("%lld",&n);
        ll s=da(n);
        ll ans=0;
        ll zz;
        for(i=0; i<s; i++)
        {
            zz=y[i][1];
            for(j=0; j<s; j++)
            {
                if(i!=j)
                {
                    zz=zz*(y[j][1]+1);
                }
            }
            ans+=zz;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值