Problem Description
In number theory, a prime is a positive integer greater than 1 that has no positive divisors other than 1 and itself. The distance between two positive integers x and y, denoted by d(x, y), is defined as the minimum number of multiplications by a prime or divisions (without a remainder) by a prime one can perform to transform x into y. For example, d(15, 50) = 3, because 50 = 15 * 2 * 5 / 3, and you have to perform two multiplications (*2, *5) and one division (/3) to transform 15 into 50.
For a set S of positive integers, which is initially empty, you are asked to implement the following types of operations on S.
1. I x: Insert x into S. If x is already in S, just ignore this operation.
2. D x: Delete x from S. If x is not in S, just ignore this operation.
3. Q x: Find out a minimum z such that there exists a y in S and d(x, y) = z.
For a set S of positive integers, which is initially empty, you are asked to implement the following types of operations on S.
1. I x: Insert x into S. If x is already in S, just ignore this operation.
2. D x: Delete x from S. If x is not in S, just ignore this operation.
3. Q x: Find out a minimum z such that there exists a y in S and d(x, y) = z.
Input
The input contains multiple test cases. The first line of each case contains an integer Q (1 <= Q <= 50000), indicating the number of operations. The following lines each contain a letter ‘I’, ‘D’ or ‘Q’, and an integer x (1 <= x <= 1000000).
Q = 0 indicates the end of the input.
The total number of operations does not exceed 300000.
Q = 0 indicates the end of the input.
The total number of operations does not exceed 300000.
Output
For each case, output “Case #X:” first, where X is the case number, starting from 1. Then for each ‘Q’ operation, output the result in a line; if S is empty when a ‘Q’ operation is to perform, output -1 instead.
Sample Input
12 I 20 I 15 Q 30 I 30 Q 30 D 10 Q 27 I 15 D 15 D 20 D 30 Q 5 0
Sample Output
Case #1: 1 0 3 -1
学习了nlogn的质因数分解姿势。。不过因为vector太慢而且懒得改就还是在程序中sqrt分解了
首先这题我们可以发现d(x,y)=sum(x/gcd(x,y))+sum(y/gcd(x,y)),其中sum是剩余部分的因数个数
然后我们用C[x][y]来表示,已经加入集合的,减少了y个因数后可以变为x的元素个数
然后因为因数最多20个,所以我们用一个20位的压位数组D[y]来存储y加入若干因数可以变为的数被加入了集合中。
每次找最小的统计即可
ans=min(count[D[y]&(-D[y])]+sum(x/y))
其中count为增加因数个数
#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int fx[1000001],sum[1000001],cou[1000001];
vector<int>s[1000001];
inline void prepare()
{
int p=1,d=0;
while(p<1000000)
{
cou[p]=d;
d++;
p*=2;
}
int i,j;
for(i=1;i<=1000000;i++)
fx[i]=i;
for(i=2;i*i<=1000000;i++)
{
for(j=i*i;j<=1000000;j+=i)
if(fx[j]==j)
fx[j]=i;
}
/*for(i=1;i<=1000000;i++)
for(j=i;j<=1000000;j+=i)
s[j].push_back(i);*/
}
inline int find(int x)
{
if(sum[x]!=0)
return sum[x];
int t=x;
while(t>1)
{
int d=fx[t],sx=0;
while(t>1&&fx[t]==d)
{
t/=fx[t];
sx++;
}
sum[x]+=sx;
}
return sum[x];
}
inline void cale(int x)
{
if(s[x].size()!=0)
return ;
s[x].push_back(1);
s[x].push_back(x);
int i;
for(i=2;i*i<=x;i++)
{
if(x%i==0)
{
s[x].push_back(i);
s[x].push_back(x/i);
}
}
}
bool vx[1000001];
int C[1000001][21];
int D[1000001];
int main()
{
// freopen("1004.in","r",stdin);
// freopen("1004.ans","w",stdout);
prepare();
int n,k=0;
scanf("%d",&n);
while(n!=0)
{
k++;
printf("Case #%d:\n",k);
memset(vx,0,sizeof(vx));
memset(C,0,sizeof(C));
memset(D,0,sizeof(D));
int i,j,xx;
string x;
for(i=1;i<=n;i++)
{
cin>>x;
scanf("%d",&xx);
if(x=="I")
{
if(!vx[xx])
{
cale(xx);
vx[xx]=true;
for(j=0;j<s[xx].size();j++)
{
int t=s[xx][j];
int sx=find(xx/t);
C[t][sx]++;
if(C[t][sx]==1)
D[t]^=(1<<sx);
}
}
}
else if(x=="D")
{
if(vx[xx])
{
vx[xx]=false;
for(j=0;j<s[xx].size();j++)
{
int t=s[xx][j];
int sx=find(xx/t);
C[t][sx]--;
if(C[t][sx]==0)
D[t]^=(1<<sx);
}
}
}
else
{
cale(xx);
int ans=2100000000;
for(j=0;j<s[xx].size();j++)
{
int t=s[xx][j];
if(D[t]==0)
continue;
int sx=cou[D[t]&(-D[t])]+find(xx/t);
ans=min(sx,ans);
}
if(ans==2100000000)
printf("-1\n");
else
printf("%d\n",ans);
}
}
scanf("%d",&n);
}
return 0;
}