UVALive_6843_Volume Control(暴力+标记)
2015-05-07 13:17
204 查看
传送门:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=77250#problem/A
题型:枚举标记
题意:
两个一样的音量调节器,N个档位,组合使用将形成不同的音量,如N=4
1 2 3 4
1 2 3 4
--------> 0 1 2 3 4 6 8 9 12 16 ------> 10个
N范围30000,10000组数据,时限7s。
分析:
考虑数据范围和时间,其实是可以暴力枚举,然后标记所有可能的数然后存起来,最好O(1)查询答案。
然而,组合相乘的最大值为9e+8,不用说int标记了,直接开bool数组也是不允许的,所以需要的技巧就是如何处理空间开销。
在C++中,使用vector开bool,可以使得bool长度为1位,如此就将开销节省了8倍。
也可以开(1/8)*(9e+8)的char数组,char长度为1字节,即8位,那么对此可以模拟记录八个数,用位运算进行查找和存取。
By the way,OJ服务器的计算速度还是比自己电脑快的多的~
代码:
Vector<bool>:
Char[]:
题型:枚举标记
题意:
两个一样的音量调节器,N个档位,组合使用将形成不同的音量,如N=4
1 2 3 4
1 2 3 4
--------> 0 1 2 3 4 6 8 9 12 16 ------> 10个
N范围30000,10000组数据,时限7s。
分析:
考虑数据范围和时间,其实是可以暴力枚举,然后标记所有可能的数然后存起来,最好O(1)查询答案。
然而,组合相乘的最大值为9e+8,不用说int标记了,直接开bool数组也是不允许的,所以需要的技巧就是如何处理空间开销。
在C++中,使用vector开bool,可以使得bool长度为1位,如此就将开销节省了8倍。
也可以开(1/8)*(9e+8)的char数组,char长度为1字节,即8位,那么对此可以模拟记录八个数,用位运算进行查找和存取。
By the way,OJ服务器的计算速度还是比自己电脑快的多的~
代码:
Vector<bool>:
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<vector> using namespace std; const int M = 30010; vector<bool> vis(900000010); int ans[M]; void init(){ vis.clear(); int cnt = 1; for(int i=1;i<=30000;i++){ int ii = i*i; for(int j=1;j<=i;j++){ int mul = i*j; if(!vis[mul]){ vis[mul] = true; cnt++; } } ans[i] = cnt; } // puts("hrhehe"); } int main(){ init(); int _; int n; while(~scanf("%d",&_)){ while(_--){ scanf("%d",&n); printf("%d\n",ans ); } } return 0; }
Char[]:
#include<stdio.h> #include<math.h> #include<stdlib.h> #include<string.h> #include<iostream> using namespace std; long gmax; typedef unsigned long long ULL; const ULL MAX=900000000ll; unsigned char seive[MAX/8+100]; ULL answers[30001]; void set_pos(ULL val) { unsigned char v=1; v=v<<(val & 7); seive[val>>3]|=v; } ULL get_pos(ULL val) { unsigned char v=1; v=v<<(val & 7); return seive[val>>3] & v; } int main() { ULL i,j,k,ans,max,t; int T,N; set_pos(0); ans=1; for(i=1; i<=30000; i++) { for(j=i,max=i*i; j<=max; j+=i) { t=get_pos(j); if(t==0) { ans++; set_pos(j); } } answers[i]=ans; } scanf("%d",&T); for(i=0; i<T; i++) { scanf("%d",&N); printf("%u\n",answers ); } return 0; }
相关文章推荐
- UVALive 6843 Volume Control(暴力压位)
- UVALive 6843 Volume Control
- uvalive 7365 Composition 暴力解法
- UVALive 7077 - Little Zu Chongzhi's Triangles(暴力)
- 【暴力预处理+剪枝/bitset】Golf Bot UVALive - 6886
- 【dp/二分+暴力】MAGRID UVALive - 5983
- UVALive 4868 Palindrometer 暴力
- UVaLive 6854 City (暴力)
- 【UVALive】2037 - Digital Rivers(找规律,暴力)
- 例题1.8 彩色立方体 Colored Cubes UVALive - 3401 暴力打表+暴力搜索+贪心
- UVALive 6895 Deduction(暴力)
- Gym 100299C && UVaLive 6582 Magical GCD (暴力+数论)
- UVALive 5908 Tracking RFIDs(计算几何+数据结构,暴力也可过)
- UVALive 4423 String LD 暴力
- POJ1964/UVALive 3029 -City Game -求最大子矩形-(暴力枚举/dp)
- UVALive - 2963 Hypertransmission 暴力
- UVALive - 5908(UVA1517)Tracking RFIDs(暴力)
- UVALive 6089 Nine (暴力模拟)
- UVALive-7279 - Sheldon Numbers【暴力】
- [uvalive 7263] Today Is a Rainy Day(暴力,BFS,dp)