标签:
A:求区间内素因子个数等于n的数有多少个
解题思路:筛法
解题代码:
1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2015年01月18日 星期日 13时54分20秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include<stack> 11 #include<bitset> 12 #include<algorithm> 13 #include<functional> 14 #include<numeric> 15 #include<utility> 16 #include<sstream> 17 #include<iostream> 18 #include<iomanip> 19 #include<cstdio> 20 #include<cmath> 21 #include<cstdlib> 22 #include<cstring> 23 #include<ctime> 24 #define LL long long 25 #define maxn 10000007 26 using namespace std; 27 int hs[maxn]; 28 int ans[maxn]; 29 void solve() 30 { 31 memset(ans,0,sizeof(ans)); 32 memset(hs,0,sizeof(hs)); 33 for(int i = 2 ;i <= maxn;i ++) 34 { 35 if(hs[i] == 0 ) 36 { 37 int k = i; 38 while(k < maxn-5) 39 { 40 hs[k] = 1; 41 ans[k] ++ ; 42 k += i ; 43 } 44 } 45 } 46 } 47 int main(){ 48 int T; 49 solve(); 50 //freopen("","r",stdin); 51 //freopen("out","w",stdout); 52 scanf("%d",&T); 53 for(int ca = 1; ca <= T; ca ++) 54 { 55 int n , m , t; 56 scanf("%d %d %d",&n,&m,&t); 57 int sum = 0 ; 58 for(int i = n ;i <= m;i ++) 59 { 60 //printf("%d ",ans[i]); 61 if(ans[i] == t ) 62 sum ++ ; 63 } 64 //printf("\n"); 65 printf("Case #%d: %d\n",ca,sum); 66 } 67 return 0; 68 }
B:每输入一个词之前把这个词加入字典,问你最少需要输入多少个字母就能够把所有词语按顺序输出、
解题思路:字典树
解题代码:
1 // File Name: temp.cpp 2 // Author: darkdream 3 // Created Time: 2014年09月11日 星期四 15时18分4秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include<stack> 11 #include<bitset> 12 #include<algorithm> 13 #include<functional> 14 #include<numeric> 15 #include<utility> 16 #include<sstream> 17 #include<iostream> 18 #include<iomanip> 19 #include<cstdio> 20 #include<cmath> 21 #include<cstdlib> 22 #include<cstring> 23 #include<ctime> 24 #include<queue> 25 #define LL long long 26 #define maxn 1001005 27 using namespace std; 28 struct Trie 29 { 30 int next[maxn][26],end[maxn]; 31 int root, L; 32 int newnode() 33 { 34 memset(next[L],-1,sizeof(next[L])); 35 end[L++] = 0 ; 36 return L-1; 37 } 38 void init() 39 { 40 L = 0 ; 41 root = newnode(); 42 } 43 int insert(char buf[]) 44 { 45 int now = root; 46 int len = strlen(buf); 47 int rans = -1; 48 for(int i = 0 ;i < len ;i ++) 49 { 50 if(next[now][buf[i] - ‘a‘] == -1) 51 { 52 next[now][buf[i] - ‘a‘] = newnode(); 53 if(rans == -1) 54 rans = i ; 55 } 56 now = next[now][buf[i]- ‘a‘]; 57 } 58 end[now] = 1; 59 if(rans == -1) 60 return len; 61 else return rans + 1; 62 } 63 }; 64 char buf[1010005]; 65 Trie ac; 66 int main(){ 67 freopen("autocomplete.txt","r",stdin); 68 freopen("out","w",stdout); 69 int T; 70 scanf("%d",&T); 71 for(int ca = 1; ca <= T; ca ++) 72 { 73 int n ; 74 scanf("%d",&n); 75 ac.init(); 76 int ans = 0; 77 for(int i = 1;i <= n;i ++) 78 { 79 scanf("%s",buf); 80 ans += ac.insert(buf); 81 } 82 printf("Case #%d: %d\n",ca,ans); 83 } 84 return 0; 85 }
C: 知道两个队最终的比分,问你求两个队分别始终领先的情况数
解题思路:DP,dp[i][j] = dp[i-1][j] + dp[i-1][j-1] (需要约束条件)
解题代码:
1 // File Name: c.cpp 2 // Author: darkdream 3 // Created Time: 2015年01月18日 星期日 15时19分10秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include<stack> 11 #include<bitset> 12 #include<algorithm> 13 #include<functional> 14 #include<numeric> 15 #include<utility> 16 #include<sstream> 17 #include<iostream> 18 #include<iomanip> 19 #include<cstdio> 20 #include<cmath> 21 #include<cstdlib> 22 #include<cstring> 23 #include<ctime> 24 #define LL long long 25 #define M 1000000007 26 using namespace std; 27 LL dp[4005][2005]; 28 int main(){ 29 int T ; 30 //freopen("out","w",stdout); 31 scanf("%d",&T); 32 for(int ca = 1; ca <= T ; ca ++) 33 { 34 int n , m; 35 scanf("%d-%d",&n,&m); 36 //printf("%d %d\n",n,m); 37 int t = n + m; 38 memset(dp,0,sizeof(dp)); 39 dp[0][0] = 1; 40 dp[1][1] = 1; 41 for(int i = 2;i <= t;i ++) 42 for(int j = 0 ;j <= n ;j ++) 43 { 44 if(i - j < j) 45 { 46 if((i - 1) - j < j) 47 dp[i][j] = (dp[i][j] + dp[i-1][j])%M; 48 if((i-1) - (j-1) < (j-1)) 49 dp[i][j] =(dp[i][j] + dp[i-1][j-1])% M; 50 } 51 } 52 printf("Case #%d: %lld ",ca,dp[t][n]); 53 memset(dp,0,sizeof(dp)); 54 dp[0][0] = 1; 55 dp[1][1] = 1; 56 LL sum =0 ; 57 for(int i = 2;i <= m+m;i ++) 58 for(int j = 0 ;j <= m ;j ++) 59 { 60 if(i - j < j) 61 { 62 if((i - 1) - j < j) 63 dp[i][j] = (dp[i][j] + dp[i-1][j])%M; 64 if((i-1) - (j-1) < (j-1)) 65 dp[i][j] =(dp[i][j] + dp[i-1][j-1])% M; 66 } 67 } 68 for(int i = m;i <= (m == 0?0:m+m-1);i ++) 69 sum = (sum+dp[i][m])%M; 70 printf("%lld\n",sum); 71 } 72 73 return 0; 74 }
D:
标签:
原文地址:http://www.cnblogs.com/zyue/p/4240284.html