2021.01.23 Rating赛
虽然这个题已经过了,但是还是想写一下,因为直接做难了,可能是好久没做题导致思维没有发散开。
下面是我做的代码,感觉很复杂。当时的想法真是琢磨不透。
#include <cstdio> #include <iostream> #include <algorithm> #include <map> using namespace std; struct book{ long long l,r; }g[1007]; int main(){ long long n,l,r; long long root=0; cin>>n; for(long long i =0;i<n;i++){ cin>>l>>r; long long left=0; if((root-l)>0) left=(root-l)/r; else left=0; for(long long j =left;;j++){ if(l+r*j>root){ root=l+r*j; break; } } } cout<<root<<endl; return 0; }
后来看了别人写的解答,恍然大悟,确实,脑子不用是会生锈的。
#include <iostream> using namespace std; int main(){ int n, x, y; while(cin>>n){ int d = 0; for(int i = 0; i < n; i++){ cin>>x>>y; if(d < x) d = x; else{ while(x <= d) x += y; d = x; } } cout<<d<<endl; } return 0; }
位运算:
“&”: 0&0=0,0&1=0,1&0=0,1&1=1;
“|”: 0|0=0,0|1=1,1|0=1,1|1=1;
“^”: 0^0=0,0^1=1,1^0=1,1^1=0。
对于该题,二进制只有01,因此我们需要判断的是0~1023的二进制的所有0和1,这里我们只需要判断0和1023就可以得到答案。
也就是我们要判断每一位是下面的哪一种,然后进行
记映射f可以分解为若干个位(bit)映射,位i的映射为f[i],则:
若f[i]:0->0,1->0,则f[i]=“&0,|0,^0”,即a[i]=0,b[i]=0,c[i]=0;
若f[i]:0->0,1->1,则f[i]=“&1,|0,^0”,即a[i]=1,b[i]=0,c[i]=0;
若f[i]:0->1,1->0,则f[i]=“&1,|0,^1”,即a[i]=1,b[i]=0,c[i]=1;
若f[i]:0->1,1->1,则f[i]=“&1,|1,^0”,即a[i]=1,b[i]=1,c[i]=0。
可以一位位的加在一起。
#include <iostream> #include <algorithm> #include <cstring> #include <map> #include <string> #define MAXN 100007 typedef long long ll; using namespace std; int t, n; int main(){ cin>>t; int x=0,y=1023; for(int i =0;i<t;i++){ char hed[10]; int r; cin>>hed>>r; // cout<<hed<<r<<endl; if(hed[0]=='&'){ x&=r; y&=r; }else if(hed[0]=='|'){ x|=r; y|=r; }else{ x^=r; y^=r; } } int And = 0, Or = 0, Xor = 0; for (int i = 0; i < 10; i++) { int bit = 1 << i; if ((x & bit)) { if (y & bit) Or |= bit; else Xor |= bit; And |= bit; } else { if (y & bit) And |= bit; } } cout<<"3"<<endl; cout<<"| "<<Or<<endl; cout<<"& "<<And<<endl; cout<<"^ "<<Xor<<endl; return 0; }
可以直接构造a、b、c:
a=f(0x0)|f(0x3ff);
b=f(0x0)&f(0x3ff);
c=f(0x0)&(0x3ff^f(0x3ff))。
#include <iostream> #include <algorithm> #include <cstring> #include <map> #include <string> #define MAXN 100007 typedef long long ll; using namespace std; int t, n; int main(){ cin>>t; int x=0,y=1023; for(int i =0;i<t;i++){ char hed[10]; int r; cin>>hed>>r; // cout<<hed<<r<<endl; if(hed[0]=='&'){ x&=r; y&=r; }else if(hed[0]=='|'){ x|=r; y|=r; }else{ x^=r; y^=r; } } int Or=x&y,And=x|y,Xor=x&(0x3ff^y); cout<<"3"<<endl; cout<<"| "<<Or<<endl; cout<<"& "<<And<<endl; cout<<"^ "<<Xor<<endl; return 0; }

浙公网安备 33010602011771号