标签:技术分享 输出 i++ input 显示屏 gif div 数据包 背包
3 3 2 6 33
Yes Yes No
直接用set保存下,查询就从set里查询就行。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <set> 6 #define ll long long 7 using namespace std; 8 const int MAX = 1e4+10; 9 const int MOD = 1e9+7; 10 int vis[10010],b[MAX*10],k; 11 void init() { 12 for(int i = 2; i < 10000; i ++) { 13 if(!vis[i]){ 14 b[++k] = i; 15 for(int j = i+i; j < 10000; j += i) 16 vis[j] = 1; 17 } 18 } 19 } 20 int main() { 21 int n,q; 22 std::ios::sync_with_stdio(false); 23 init(); 24 cin>>n>>q; 25 set<ll> st; 26 for(int i = 1; i <= n; i ++) { 27 ll ans = 1; 28 for(int j = 1; j <= i; j ++) { 29 ans = 1LL*b[j]*ans%MOD; 30 } 31 st.insert(ans); 32 } 33 ll num; 34 while(q--) { 35 cin>>num; 36 if(st.count(num)) printf("Yes\n"); 37 else printf("No\n"); 38 } 39 return 0; 40 }
4 5 0
3 8 Hint:对于第一个样例,当n为4的时候,我们满足条件的有 UUUU LUUU UUUL 三种情况
思路:递推吧,因为每一个情况都是由前一个情况转变过来的,所以用一个dp数组去存每个情况相应的值,每一层的意思如下:(i为当前序列的长度)
dp[i][0]没有三个连续U的序列最右边为L
dp[i][1]没有三个连续U的序列最右边有一个U
dp[i][2]没有三个连续U的序列最右边有两个连续的U
dp[i][3]有三个连续的U的序列
结合每个情况可以发现
1 #include <iost100000ream> 2 #include <stdio.h> 3 #include <string.h> 4 #define ll long long 5 using namespace std; 6 ll dp[33][4]; 7 int main() { 8 dp[1][0] = dp[1][1] = 1; 9 dp[1][2] = dp[1][3] = 0; 10 for(int i = 2; i < 33; i ++) { 11 dp[i][0] = dp[i-1][0] + dp[i-1][1] + dp[i-1][2]; 12 dp[i][1] = dp[i-1][0]; 13 dp[i][2] = dp[i-1][1]; 14 dp[i][3] = dp[i-1][3]*2 + dp[i-1][2]; 15 } 16 int n; 17 while(scanf("%d",&n)&&n) { 18 cout << dp[n][3] << endl; 19 } 20 return 0; 21 }
3 1 2 3 2 2 4 1 3 4
62 0
其实就是求(a1+b1)*(a2+b2)*....*(an+bn)-a1*a2...*an-b1*b2*...*bn。答案取摸就行,但是我的就不咋的就是过不去。
下面是我的代码。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #define ll long long 6 using namespace std; 7 const ll Mod = 1e9+7; 8 const int MAX = 100010; 9 ll a[MAX], b[MAX]; 10 11 ll fun(ll x, ll n) { 12 if(x >= 0){ 13 ll y = x/n; 14 return x - y*n; 15 }else { 16 return x+n; 17 } 18 } 19 int main() { 20 std::ios::sync_with_stdio(false); 21 int n; 22 while(scanf("%d",&n)!=EOF) { 23 for(int i = 1; i <= n; i ++) cin>>a[i]; 24 for(int i = 1; i <= n; i ++) cin>>b[i]; 25 ll ans = 1; 26 for(int i = 1; i <= n; i ++) { 27 ans = ans*(a[i]+b[i])%Mod; 28 } 29 30 ll x = 1, y = 1; 31 for(int i = 1; i <= n; i ++) { 32 x = x*a[i]%Mod; 33 } 34 for(int i = 1; i <= n; i ++) { 35 y = y*b[i]%Mod; 36 } 37 cout << (ans-x-y+Mod)%Mod << endl; 38 } 39 return 0; 40 }
下面的答案
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #define ll long long 5 using namespace std; 6 const int MAX = 100010; 7 const int mod = 1e9+7; 8 ll a[MAX], b[MAX]; 9 int main() { 10 int n; 11 std::ios::sync_with_stdio(false); 12 while(cin>>n) { 13 ll ans = 1; 14 for(int i = 1; i <= n; i ++) { 15 cin >> a[i]; 16 ans *= a[i]; 17 ans %= mod; 18 } 19 ll cnt = 1; 20 for(int i = 1; i <= n; i ++) { 21 cin >> b[i]; 22 cnt *= b[i]; 23 cnt %= mod; 24 } 25 ll sum = 1; 26 for(int i = 1; i <= n; i ++) { 27 sum *= (a[i]+b[i]); 28 sum %= mod; 29 } 30 cout << (sum+mod-ans+mod-cnt)%mod; 31 } 32 return 0; 33 }
3 1 5 3 2 5 2 4 2 1 2 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3
6 1
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <set> 6 using namespace std; 7 int n,m,W; 8 int w[1010], b[1010],fa[1010], dp[1010]; 9 struct Nod{ 10 int w, b; 11 Nod(){ 12 w = b = 0; 13 } 14 }nod[1010]; 15 int find(int x) { 16 return fa[x] = (x==fa[x])?x:find(fa[x]); 17 } 18 void unite(int x, int y) { 19 x = find(x); 20 y = find(y); 21 if(x < y) fa[y] = x; 22 else fa[x] = y; 23 } 24 int main() { 25 std::ios::sync_with_stdio(false); 26 while(cin>>n>>m>>W) { 27 for(int i = 1; i <= n; i ++) cin>>w[i],fa[i] = i; 28 for(int i = 1; i <= n; i ++) cin>>b[i]; 29 for(int i = 1; i <= m; i ++) { 30 int x, y; 31 cin>>x>>y; 32 unite(x,y); 33 } 34 for(int i = 1; i <= n; i ++) { 35 int x = find(i); 36 nod[x].w += w[i]; 37 nod[x].b += b[i]; 38 } 39 for(int i = 1; i <= n; i ++) { 40 if(nod[i].b != 0 && nod[i].w != 0) { 41 for(int j = W; j >= nod[i].w; j --) { 42 dp[j] = max(dp[j],dp[j-nod[i].w]+nod[i].b); 43 } 44 } 45 } 46 printf("%d\n",dp[W]); 47 for(int i = 1; i <= n; i ++){ 48 nod[i].b = nod[i].w = 0; 49 } 50 memset(dp,0,sizeof(dp)); 51 } 52 return 0; 53 }
0 65
+-----+ | E| | | | | | | | | +-----+ +-----+ |- 3G| |-- | |--- | | | | | +-----+
直接判断下就行了。
1 #include <bits/stdc++.h> 2 using namespace std; 3 int main(){ 4 int d; 5 while(cin>>d){ 6 if(d < 20){ 7 cout << "+-----+\n| E|\n| |\n| |\n| |\n| |\n+-----+\n"; 8 }else if(d < 40){ 9 cout << "+-----+\n|- E|\n| |\n| |\n| |\n| |\n+-----+\n"; 10 }else if(d < 60){ 11 cout << "+-----+\n|- E|\n|-- |\n| |\n| |\n| |\n+-----+\n"; 12 }else if(d < 80){ 13 cout << "+-----+\n|- 3G|\n|-- |\n|--- |\n| |\n| |\n+-----+\n"; 14 }else if(d < 90){ 15 cout << "+-----+\n|- 3G|\n|-- |\n|--- |\n|---- |\n| |\n+-----+\n"; 16 }else if(d < 100){ 17 cout << "+-----+\n|- 4G|\n|-- |\n|--- |\n|---- |\n| |\n+-----+\n"; 18 }else if(d == 100){ 19 cout << "+-----+\n|- 4G|\n|-- |\n|--- |\n|---- |\n|-----|\n+-----+\n"; 20 } 21 } 22 return 0; 23 }
5 4 10 9 12 9 7 14 1 2 4 5 3 4 1 4
7
这题也挺气的,线段树、树状数组和前缀和都做了,就是过不去。
下面是我的一些代码。
线段树:
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #define ll long long 5 #define lson l,m,rt<<1 6 #define rson m+1,r,rt<<1|1 7 using namespace std; 8 const int MAX = 10010; 9 int n,m,str,x; 10 int tree[MAX<<2]; 11 void Push(int rt) { 12 tree[rt] = tree[rt<<1] + tree[rt<<1|1]; 13 } 14 void build(int l, int r, int rt) { 15 if(l == r) { 16 scanf("%d",&x); 17 tree[rt] = str-x; 18 return ; 19 } 20 int m = (l+r)>>1; 21 build(lson); 22 build(rson); 23 Push(rt); 24 } 25 ll query(int l, int r, int rt, int LL, int RR) { 26 if(LL <= l && r <= RR) { 27 return tree[rt]; 28 } 29 ll sum = 0; 30 int m = (l+r)>>1; 31 if(m >= LL) sum += 1LL*query(lson,LL,RR); 32 if(m < RR) sum += 1LL*query(rson,LL,RR); 33 return sum; 34 } 35 int main() { 36 std::ios::sync_with_stdio(false); 37 while(cin>>n>>m>>str) { 38 memset(tree,0,sizeof(tree)); 39 build(1,n,1); 40 ll ans = 0; 41 for(int i = 1; i <= m; i ++) { 42 int L, R; 43 scanf("%d%d",&L,&R); 44 ll xx = query(1,n,1,L,R); 45 if(xx > 0) ans += xx; 46 } 47 printf("%lld\n",ans); 48 } 49 return 0; 50 }
前缀和
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <set> 5 #define ll long long 6 using namespace std; 7 int n, m; 8 const int MAX = 20010; 9 ll sum[MAX], str; 10 11 int main() { 12 std::ios::sync_with_stdio(false); 13 while(cin>>n>>m>>str){ 14 memset(sum,0,sizeof(sum)); 15 set<pair<int,int> > st; 16 ll x; 17 for(ll i = 1; i <= n; i ++) { 18 cin>>x; 19 sum[i] += sum[i-1] + str - x; 20 } 21 ll ans = 0; 22 while(m--) { 23 int l, r; 24 cin>>l>>r; 25 if(st.count(make_pair(l,r)))continue; 26 st.insert(make_pair(l,r)); 27 ll x = sum[r] - sum[l-1]; 28 if(x > 0LL) ans += x; 29 } 30 cout << ans << endl; 31 st.clear(); 32 } 33 return 0; 34 }
树状数组
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <set> 5 #define ll long long 6 #define lowbit(x) x&(-x) 7 using namespace std; 8 const int MAX = 10010; 9 int n, m, str; 10 ll sum[MAX]; 11 void add(int x, ll q){ 12 while(x < MAX) { 13 sum[x] += q; 14 x += lowbit(x); 15 } 16 } 17 ll query(int x) { 18 ll sum1 = 0; 19 while(x > 0) { 20 sum1 += sum[x]; 21 x-=lowbit(x); 22 } 23 return sum1; 24 } 25 int main() { 26 while(scanf("%d%d%d",&n,&m,&str) != EOF) { 27 ll x; 28 set<pair<int,int> >st; 29 st.clear(); 30 memset(sum,0,sizeof(sum)); 31 for(int i = 1; i <= n; i ++) { 32 scanf("%lld",&x); 33 add(i,1LL*str-x); 34 } 35 ll ans = 0; 36 while(m--) { 37 int l, r; 38 scanf("%d%d",&l,&r); 39 if(st.count(make_pair(l,r)))continue; 40 ll xx = query(r) - query(l-1); 41 if(xx > 0LL) ans += xx; 42 } 43 printf("%lld\n",ans); 44 } 45 return 0; 46 }
答案
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <queue> 5 #include <vector> 6 #include <map> 7 #include <set> 8 #include <stack> 9 #include <cmath> 10 #include <cstdio> 11 #include <algorithm> 12 #define LL long long 13 #define N 11000 14 #define M 50010 15 #define inf 0x3f3f3f3f3f3f3f3f 16 using namespace std; 17 const LL mod = 1e9 + 7; 18 const double eps = 1e-9; 19 int num[N]; 20 int main() { 21 cin.sync_with_stdio(false); 22 int n, m; 23 int strong; 24 int a, b, c; 25 while (cin >> n >> m >> strong) { 26 num[0] = 0; 27 for (int i = 1; i <= n; i++) { 28 cin >> num[i]; 29 num[i] = strong - num[i]; 30 num[i] += num[i - 1]; 31 } 32 int sum = 0; 33 for (int i = 0; i < m; i++) { 34 cin >> a >> b; 35 c = num[b] - num[a - 1]; 36 if (c > 0) { 37 sum += c; 38 } 39 } 40 cout << sum << endl; 41 } 42 return 0; 43 }
2 3 2 6 4 3 4 6 9
Yes No
语文水平有待提高了,竟然没看懂题意,写了几次都是靠猜的,赛后猜知道要全部的最大公约数大于等于2就行。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #define ll long long 6 using namespace std; 7 const int MAX = 1e5; 8 ll a[MAX]; 9 ll gcd(ll a, ll b){ 10 return b?gcd(b,a%b):a; 11 } 12 int main() { 13 std::ios::sync_with_stdio(false); 14 int t, n; 15 cin>>t; 16 while(t--) { 17 cin>>n; 18 for(int i = 1; i <= n; i ++) { 19 cin >> a[i]; 20 } 21 if(n == 1){ 22 if(a[0] >= 2) puts("Yes"); 23 else puts("No"); 24 }else { 25 ll ans = gcd(a[0],a[1]); 26 for(int i = 2; i <= n; i ++) ans = gcd(ans,a[i]); 27 if(ans >= 2)puts("Yes"); 28 else puts("No"); 29 } 30 } 31 return 0; 32 }
标签:技术分享 输出 i++ input 显示屏 gif div 数据包 背包
原文地址:http://www.cnblogs.com/xingkongyihao/p/7257372.html