题目描述
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;
}