20140711 set

题目大意

维护一个可重集,支持:

插入一个正整数

询问一个正整数k,集合中有多少个数是k的倍数

数据范围是40000,时限0.5s

暴力肯定不行,想起这道题叫set,今天中午刚刚看了STL set用法,于是用了一个set来做,想着是logn的复杂度,其实还是n,总的就是n^2..............................................

后面才知道应该将插入的数分解因数,读入一个查询值直接输出即可,O(n*n^0.5)

 1 #include<cstdio>
 2 #include<string.h>
 3 using namespace std;
 4 
 5 int a[40000];
 6 
 7 int n;
 8 
 9 int main()
10 {
11     freopen("set.in","r",stdin);
12     freopen("set.out","w",stdout);
13     int x,y;
14     scanf("%d",&n);
15     int ans=0;
16     memset(a,0,sizeof(a));
17     while (n--)
18     {
19         scanf("%d%d",&x,&y);
20         if (x==1)
21          {
22         for (int i=1;i*i<=y;i++)
23          {
24             if(y%i==0)
25              {
26                 a[i]++;
27                 a[y/i]++;        
28             }
29             if (y==i*i) a[i]--;    
30             }
31         }
32     else
33          {
34              ans=ans xor a[y];            
35         }
36     }
37     printf("%d",ans);
38     return 0;
39 }
View Code
原文地址:https://www.cnblogs.com/woshizyj/p/3838132.html