11.16考试
bonita1130 · · 个人记录
T850019 T2
1.题意:让我们去求每个数字在二进制下一的个数总和
2.思路:先把各个数字在二进制下一的个数求出来,再将其所有的数量相加,最后看这个数除以二的余数
3.原来的错误代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std ;
string f(int n)
{
string s ;
while(n > 0)
{
int m = n % 2 ;
n = n / 2 ;
s = char(m + '0') + s ;
}
return s ;
}
int gs(string s)
{
int cnt = 0 ;
for(int i = 0 ; i <= s.size() - 1 ; i++)
{
if(s[i] == '1')cnt++ ;
}
return cnt ;
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) ;
int n , ans = 0 ;
cin >> n ;
for(int i = 1 ; i <= n ; i++)
{
int x ;
cin >> x ;
ans += gs(f(x)) ;
}
cout << ans << " " << ans % 2 ;
return 0 ;
}
4.AC代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std ;
int f(int n)
{
if(n==0) return n;
string s ;
while(n > 0)
{
int m = n % 2 ;
n = n / 2 ;
s = char(m + '0') + s ;
}
int cnt = 0 ;
//s.size()为无符号长整型
//0-1 --> 得到一个超大的数字
for(int i = 0 ; i <= s.size() - 1 ; i++)
{
if(s[i] == '1')cnt++ ;
}
return cnt ;
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) ;
int n , ans = 0 ;
cin >> n ;
for(int i = 1 ; i <= n ; i++)
{
int x ;
cin >> x ;
ans += f(x) ;//0
}
cout << ans << " " << ans % 2 ;
return 0 ;
}
5.错因:如果不特判0的话,会RE,因为如果输入的是0,那你的字符串长的岂不是0了,但你进入循环就变成了——从0开始,到-1结束
T850020 T3
1.题意:简简单单的让我们求从1~n之间有多少个质数
2.思路:我最开始的思路是只用埃氏筛,但没有注意到n取1e8时,内存会超限,导致MLE,经过老师指导后,我知道应该把数组类型开成bool,bool同样能把1和0存入,并且做到内存不超限
3.原来的错误代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std ;
int a[100000010] ;
void f()
{
a[0] = a[1] = 1 ;
for(int i = 2 ; i * i <= 100000000 ; i++)
{
if(a[i] == 0)
{
for(int bs = 2 ; bs * i <= 100000000 ; bs++)
{
a[i * bs] = 1 ;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) ;
f() ;
int n , cnt = 0 ;
cin >> n ;
for(int i = 1 ; i <= n ; i++)
{
if(a[i] == 0)cnt++ ;
}
cout << cnt ;
return 0 ;
}
4.AC代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std ;
bool a[100000010] ;//bool只占一个字节
void f()
{
a[0] = a[1] = 1 ;
for(int i = 2 ; i * i <= 100000000 ; i++)
{
if(a[i] == 0)
{
for(int bs = 2 ; bs * i <= 100000000 ; bs++)
{
a[i * bs] = 1 ;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) ;
f() ;
int n , cnt = 0 ;
cin >> n ;
for(int i = 1 ; i <= n ; i++)
{
if(a[i] == 0)cnt++ ;
}
cout << cnt ;
/*
int 4字节
128mb
8e8字节
8e5 千字节 kb
8e2 兆字节 mb 800mb
*/
return 0 ;
}
5.错因:因为我并没有注意到内存超限这个问题,盲目开了1e8
T850021 T5
1.题意:每次输入x,y 求第x个质数和第y个质数的异或值是否是1
2.思路:我原本的思路是直接求第几个质数是什么,然后再求第x个质数和第y个质数的异或值是否是1,结果还写错了,但老师提醒之后,其实很简单——因为无论怎么算,只有第1个质数和第2个质数(也就是2和3)异或的值才是1,那简单了,只用特判x和y是 1和2 或 2和1 时才输出Yes
3.原来的错误代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std ;
int t[100010] , a[100010] ;
void f()
{
t[0] = t[1] = 1 ;
for(int i = 2 ; i * i <= 100000 ; i++)
{
if(t[i] == 0)
{
for(int bs = 2 ; bs * i <= 100000 ; bs++)
{
t[i * bs] = 1 ;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) ;
f() ;
int g = 1 ;
for(int i = 1 ; i <= 100000 ; i++)
{
if(t[i] == 0)
{
a[g] = i ;
g++ ;
}
}
int n ;
cin >> n ;
for(int i = 1 ; i <= n ; i++)
{
int x , y ;
cin >> x >> y ;
if(a[x] ^ a[y] == 1)cout << "Yes\n" ;
else cout << "No\n" ;
}
return 0 ;
}
4.AC代码:
#include<bits/stdc++.h>
#define int long long
using namespace std ;
signed main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) ;
int n ;
cin >> n ;
for(int i = 1 ; i <= n ; i++)
{
int x , y ;
cin >> x >> y ;
if(x == 1 && y == 2 || x == 2 && y == 1)cout << "Yes\n" ;
else cout << "No\n" ;
}
return 0 ;
}
5.错因:我并没有用心去想这道题的突破口在哪?
6.备注:情况要靠虑完全,x和y分别是1和2 还可以是2和1
T850024 T6
1.题意:求区间段中有多少个本身是指数并且本身+1再/2也要是质数
2.思路:我开始的思路是用埃氏筛,然后再用双层循环枚举,经过老师上课的点拨,这道题要用前缀和,先预处理一下,再用一层循环遍历n次,然后for循环里面再算区间质数总和
3.原来的错误代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std ;
int a[10000010] ;
void f()
{
a[0] = a[1] = 1 ;
for(int i = 2 ; i * i <= 10000000 ; i++)
{
if(a[i] == 0)
{
for(int bs = 2 ; bs * i <= 10000000 ; bs++)
{
a[i * bs] = 1 ;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) ;
f() ;
int n;
cin >> n ;
for(int i = 1 ; i <= n ; i++)
{
int l , r , cnt = 0 ;
cin >> l >> r ;
for(int j = l ; j <= r ; j++)
{
if(a[j] == 0 && a[(j + 1) / 2] == 0)cnt++ ;
}
cout << cnt << "\n" ;
}
return 0 ;
}
4.AC代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std ;
int a[100010] , s[100010] ;
void f()
{
a[0] = a[1] = 1 ;
for(int i = 2 ; i * i <= 100000 ; i++)
{
if(a[i] == 0)
{
for(int bs = 2 ; bs * i <= 100000 ; bs++)
{
a[i * bs] = 1 ;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) ;
f() ;
int n;
cin >> n ;
for(int i = 1 ; i <= 1e5 ; i++)
{
s[i] = s[i - 1] ;
if(a[i] == 0 && a[(i + 1) / 2] == 0)s[i] += 1 ;
}
for(int i = 1 ; i <= n ; i++)
{
int l , r ;
cin >> l >> r ;
cout << s[r] - s[l - 1] << "\n" ;
}
return 0 ;
}
5.错因:盲目套两层循环,并没有考虑会超时