A Partial Order Relation(思維)

A Partial Order Relation

時間限制: 1 Sec  內存限制: 128 MB
提交: 97  解決: 26
[提交] [狀態] [討論版] [命題人:admin]

題目描述

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

題目大意是說給你一個數,這個數會和這個數的因子集合中所有的x(x滿足這個集合內除了本身沒有數能整除他)相連,並一直連下去,連到1爲止。

觀察計算可以發現,對一個數,設它有n個因子,m個不同的質因子(出現的次數分別爲k1,k2...ki...kn),則第i個因子對答案的貢獻是(n - n / (ki + 1)),最後求和就行了。

#include <bits/stdc++.h>
#define maxn 1100005
using namespace std;
typedef long long ll;
bool vis[maxn];
int prime[maxn/10];
int cnt,cntt,cnttt;
ll divv[maxn];
ll temp[maxn];
void primejudge()
{
    int i,j;
    vis[1]=true;
    for(i=2;i<maxn;i++)
    {
        if(!vis[i])
        {
            prime[cnt++]=i;
        }
        for(j=0;j<cnt&&prime[j]*i<=maxn;j++)
        {
            vis[i*prime[j]]=true;
            if(i%prime[j]==0) break;
        }
    }
}
int p[100];
void get_div(ll n)
{
    for(int i=0;i<cnt;i++)
    {
        if(n%prime[i]==0)
        {
            temp[cnttt++]=prime[i];
        }
        while(n%prime[i]==0)
        {
            n/=prime[i];
            p[cnttt-1]++;
        }
        if(n==1) break;
    }
    if(n>1) temp[cnttt++]=n,p[cnttt-1]++;
}
int main()
{
//    freopen("in.txt", "r", stdin);
    int t;
    ll n;
    scanf("%d", &t);
    primejudge();
    while(t--)
    {
        cntt = cnttt = 0;
        cntt=1;
        memset(p, 0, sizeof(p));
        scanf("%lld", &n);
        get_div(n);
        for(int i=0;i<cnttt;i++)
            cntt*=(p[i]+1);
        int ans = 0;
        for (int i = 0; i < cnttt; i++)
        {
            int tmp = cntt / (p[i] + 1);
            ans += cntt - tmp;
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章