Codeforces 155D Colliders【思维+模拟】
2017-10-14 15:59
597 查看
D. Colliders
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
By 2312 there were n Large Hadron Colliders in the inhabited part of the universe. Each of them corresponded to a single natural number
from 1 to n.
However, scientists did not know what activating several colliders simultaneously could cause, so the colliders were deactivated.
In 2312 there was a startling discovery: a collider's activity is safe if and only if all numbers of activated colliders are pairwise relatively prime to each other (two numbers are relatively prime if their greatest common divisor equals 1)!
If two colliders with relatively nonprime numbers are activated, it will cause a global collapse.
Upon learning this, physicists rushed to turn the colliders on and off and carry out all sorts of experiments. To make sure than the scientists' quickness doesn't end with big trouble, the Large Hadron Colliders' Large Remote Control was created. You are commissioned
to write the software for the remote (well, you do not expect anybody to operate it manually, do you?).
Initially, all colliders are deactivated. Your program receives multiple requests of the form "activate/deactivate the i-th collider".
The program should handle requests in the order of receiving them. The program should print the processed results in the format described below.
To the request of "+ i" (that is, to activate the i-th
collider), the program should print exactly one of the following responses:
"Success" if the activation was successful.
"Already on", if the i-th collider
was already activated before the request.
"Conflict with j", if there is a conflict with the j-th
collider (that is, the j-th collider is on, and numbers i and j are
not relatively prime). In this case, the i-th collider shouldn't be activated. If a conflict occurs with several colliders simultaneously,
you should print the number of any of them.
The request of "- i" (that is, to deactivate the i-th
collider), should receive one of the following responses from the program:
"Success", if the deactivation was successful.
"Already off", if the i-th collider
was already deactivated before the request.
You don't need to print quotes in the output of the responses to the requests.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105)
— the number of colliders and the number of requests, correspondingly.
Next m lines contain numbers of requests, one per line, in the form of either "+
i" (without the quotes) — activate the i-th collider, or "- i"
(without the quotes) — deactivate the i-th collider (1 ≤ i ≤ n).
Output
Print m lines — the results of executing requests in the above given format. The requests should be processed in the order, in which
they are given in the input. Don't forget that the responses to the requests should be printed without quotes.
Examples
input
output
Note
Note that in the sample the colliders don't turn on after the second and ninth requests. The ninth request could also receive response "Conflict with 3".
题目大意:
有【1~n】这些数,现在有m个操作。
+x表示向机器中放入一个数x
-x表示在机器中拿出一个数x
这个机器整个过程都需要保证,在机器中任取两个数的gcd都是1、对于每一个操作,输出一个结果
思路:
我们分类讨论即可:
①当对于+x操作,如果机器中如果存在x这个数了,直接输出。否则判断能否放进去,判断放进去的条件我们O(sqrt(x))去判定就行。
如果可以放进去,我们放进去x的因子就行了。
对于整体时间复杂度为O(nlogn);
②当对于-x操作,如果机器中不存在这个数,直接输出。否则将其因子全部踢出 。
过程用vis【i】判定i这个数是否在机器中。
过程用s【i】=x表示i这个因子是属于数字x的。
那么如果我们加入一个新的数,其因子temp,s【temp】>0的话,就说明机器中有和其冲突的数字存在。
细节稍微有些多,谨慎一点就行了。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
using namespace std;
map<int,int>s;
int vis[150000];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
s.clear();
memset(vis,0,sizeof(vis));
while(m--)
{
char op[15];scanf("%s",op);
int x;scanf("%d",&x);
if(op[0]=='+')
{
if(vis[x]==1)
{
printf("Already on\n");
}
else
{
int temp=0;
for(int i=1;i<=sqrt(x);i++)
{
if(x%i==0)
{
if(s[i]>0)temp=s[i];
if(s[x/i]>0)temp=s[x/i];
}
}
if(temp>0)
{
printf("Conflict with %d\n",temp);
}
else
{
vis[x]=1;
printf("Success\n");
for(int i=2;i<=sqrt(x);i++)
{
if(x%i==0)
{
s[i]=x;
s[x/i]=x;
}
}
if(x!=1)s[x]=x;
}
}
}
else
{
if(vis[x]==0)
{
printf("Already off\n");
}
else
{
vis[x]=0;
printf("Success\n");
for(int i=2;i<=sqrt(x);i++)
{
if(x%i==0)s[i]=0,s[x/i]=0;
}
s[x]=0;
}
}
}
}
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
By 2312 there were n Large Hadron Colliders in the inhabited part of the universe. Each of them corresponded to a single natural number
from 1 to n.
However, scientists did not know what activating several colliders simultaneously could cause, so the colliders were deactivated.
In 2312 there was a startling discovery: a collider's activity is safe if and only if all numbers of activated colliders are pairwise relatively prime to each other (two numbers are relatively prime if their greatest common divisor equals 1)!
If two colliders with relatively nonprime numbers are activated, it will cause a global collapse.
Upon learning this, physicists rushed to turn the colliders on and off and carry out all sorts of experiments. To make sure than the scientists' quickness doesn't end with big trouble, the Large Hadron Colliders' Large Remote Control was created. You are commissioned
to write the software for the remote (well, you do not expect anybody to operate it manually, do you?).
Initially, all colliders are deactivated. Your program receives multiple requests of the form "activate/deactivate the i-th collider".
The program should handle requests in the order of receiving them. The program should print the processed results in the format described below.
To the request of "+ i" (that is, to activate the i-th
collider), the program should print exactly one of the following responses:
"Success" if the activation was successful.
"Already on", if the i-th collider
was already activated before the request.
"Conflict with j", if there is a conflict with the j-th
collider (that is, the j-th collider is on, and numbers i and j are
not relatively prime). In this case, the i-th collider shouldn't be activated. If a conflict occurs with several colliders simultaneously,
you should print the number of any of them.
The request of "- i" (that is, to deactivate the i-th
collider), should receive one of the following responses from the program:
"Success", if the deactivation was successful.
"Already off", if the i-th collider
was already deactivated before the request.
You don't need to print quotes in the output of the responses to the requests.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105)
— the number of colliders and the number of requests, correspondingly.
Next m lines contain numbers of requests, one per line, in the form of either "+
i" (without the quotes) — activate the i-th collider, or "- i"
(without the quotes) — deactivate the i-th collider (1 ≤ i ≤ n).
Output
Print m lines — the results of executing requests in the above given format. The requests should be processed in the order, in which
they are given in the input. Don't forget that the responses to the requests should be printed without quotes.
Examples
input
10 10 + 6 + 10 + 5 - 10 - 5 - 6 + 10 + 3 + 6 + 3
output
Success Conflict with 6 Success Already off Success Success Success Success Conflict with 10 Already on
Note
Note that in the sample the colliders don't turn on after the second and ninth requests. The ninth request could also receive response "Conflict with 3".
题目大意:
有【1~n】这些数,现在有m个操作。
+x表示向机器中放入一个数x
-x表示在机器中拿出一个数x
这个机器整个过程都需要保证,在机器中任取两个数的gcd都是1、对于每一个操作,输出一个结果
思路:
我们分类讨论即可:
①当对于+x操作,如果机器中如果存在x这个数了,直接输出。否则判断能否放进去,判断放进去的条件我们O(sqrt(x))去判定就行。
如果可以放进去,我们放进去x的因子就行了。
对于整体时间复杂度为O(nlogn);
②当对于-x操作,如果机器中不存在这个数,直接输出。否则将其因子全部踢出 。
过程用vis【i】判定i这个数是否在机器中。
过程用s【i】=x表示i这个因子是属于数字x的。
那么如果我们加入一个新的数,其因子temp,s【temp】>0的话,就说明机器中有和其冲突的数字存在。
细节稍微有些多,谨慎一点就行了。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
using namespace std;
map<int,int>s;
int vis[150000];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
s.clear();
memset(vis,0,sizeof(vis));
while(m--)
{
char op[15];scanf("%s",op);
int x;scanf("%d",&x);
if(op[0]=='+')
{
if(vis[x]==1)
{
printf("Already on\n");
}
else
{
int temp=0;
for(int i=1;i<=sqrt(x);i++)
{
if(x%i==0)
{
if(s[i]>0)temp=s[i];
if(s[x/i]>0)temp=s[x/i];
}
}
if(temp>0)
{
printf("Conflict with %d\n",temp);
}
else
{
vis[x]=1;
printf("Success\n");
for(int i=2;i<=sqrt(x);i++)
{
if(x%i==0)
{
s[i]=x;
s[x/i]=x;
}
}
if(x!=1)s[x]=x;
}
}
}
else
{
if(vis[x]==0)
{
printf("Already off\n");
}
else
{
vis[x]=0;
printf("Success\n");
for(int i=2;i<=sqrt(x);i++)
{
if(x%i==0)s[i]=0,s[x/i]=0;
}
s[x]=0;
}
}
}
}
}
相关文章推荐
- Codeforces 527C Glass Carving【思维+模拟】
- Codeforces 669B Little Artem and Grasshopper【思维+模拟】
- codeforces 954C. Matrix Walk(思维模拟)
- Codeforces 821C Okabe and Boxes 思维模拟
- Codeforces 272D Dima and Two Sequences【思维+模拟】
- 【Codeforces】-632B-Alice, Bob, Two Teams(模拟,思维)
- codeforces 691C Exponential notation(思维 + 比较精妙的模拟)
- Codeforces 518C Anya and Smartphone【思维+模拟】
- Codeforces 322C Ciel and Robot【思维+模拟】细节很多= =
- 【CodeForces - 834】 A【模拟】 B【模拟】C【思维】
- codeforces-746【思维】【模拟】
- codeforces 496C Removing Columns (模拟,思维)
- CodeForces 808A——Lucky Year——思维,模拟
- codeforces 845D Driving Test (模拟+思维)
- Codeforces 469C 24 Game【思维+模拟】
- Codeforces 492D Vanya and Computer Game【模拟+循环节+思维】好题~
- Codeforces 954C - Matrix Walk(思维 + 模拟)
- CodeForces 811B——Vladik and Complicated Book ——思维,模拟
- CodeForces - 876D Sorting the Coins 模拟+思维
- Codeforces 746C Tram 模拟+思维