Educational Codeforces Round 14
水
1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 201000; 22 const int MOD = 1e9+7; 23 #define LL long long 24 #define mi() (l+r)>>1 25 double const pi = acos(-1); 26 27 void fre() { 28 freopen("in.txt","r",stdin); 29 } 30 31 // inline int r() { 32 // int x=0,f=1;char ch=getchar(); 33 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 34 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 35 // } 36 37 int a[1010]; 38 int main(){ 39 int n; 40 cin>>n; 41 int k=0; 42 for(int i=0;i<n;i++){ 43 cin>>a[i]; 44 if(!a[i]) k++; 45 } 46 if(n==1&&a[0]==0){ 47 puts("NO"); 48 exit(0); 49 } 50 if(n==1&&a[0]==1){ 51 puts("YES"); 52 exit(0); 53 } 54 if(k==1) cout<<"YES"<<endl; 55 else cout<<"NO"<<endl; 56 return 0; 57 }
题意:判断是否是 ‘镜像‘
1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 201000; 22 const int MOD = 1e9+7; 23 #define LL long long 24 #define mi() (l+r)>>1 25 double const pi = acos(-1); 26 27 void fre() { 28 freopen("in.txt","r",stdin); 29 } 30 31 // inline int r() { 32 // int x=0,f=1;char ch=getchar(); 33 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 34 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 35 // } 36 std::pair<char, char> mapP[] ={{'A', 'A'}, {'b', 'd'}, {'d', 'b'}, {'H', 'H'}, {'I', 'I'}, {'M', 'M'}, {'O', 'O'}, {'o', 'o'}, {'p', 'q'}, 37 {'q', 'p'},{'T', 'T'}, {'U', 'U'}, {'V', 'V'}, {'v', 'v'},{'W', 'W'}, {'w', 'w'}, {'X', 'X'}, {'x', 'x'}, {'Y', 'Y'} 38 }; 39 40 bool check(char a, char b){ 41 for(std::pair<char, char> p : mapP){ 42 if(p.first == a && p.second == b){ 43 return true; 44 } 45 } 46 return false; 47 } 48 49 int main(){ 50 string s; 51 cin>>s; 52 int i,j; 53 bool flag=false; 54 for(i=0,j=(int)s.length()-1;i<=j;i++,j--){ 55 if(!check(s[i],s[j])){ 56 flag=1; 57 puts("NIE"); 58 break; 59 } 60 } 61 if(!flag) 62 puts("TAK"); 63 return 0; 64 }
题意:把一个值转换成 表达式a*10^b形式
字符处理
1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 201000; 22 const int MOD = 1e9+7; 23 #define LL long long 24 #define mi() (l+r)>>1 25 double const pi = acos(-1); 26 27 void fre() { 28 freopen("in.txt","r",stdin); 29 } 30 31 // inline int r() { 32 // int x=0,f=1;char ch=getchar(); 33 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 34 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 35 // } 36 37 int main(){ 38 string s; 39 string str=""; 40 int ansb; 41 cin>>s; 42 int p=0; 43 while(p<(int)s.size()&&(s[p]=='0'||s[p]=='.')){ 44 p++; 45 } 46 // cout<<p<<bndl; 47 str.push_back(s[p]); 48 str.push_back('.'); 49 int pos=s.find('.'); 50 if(pos==-1) pos=s.size(); 51 for(int i=++p;i<s.size();i++){ 52 if(s[i]!='.') str.push_back(s[i]); 53 } 54 if(pos==p) ansb=0; 55 else{ 56 if(pos>p) ansb=pos-p; 57 else ansb=pos-p+1; 58 } 59 while(str.back()=='0'||str.back()=='.') str.pop_back(); 60 if(ansb) str+="E"+to_string(ansb); 61 printf("%s\n",str.c_str()); 62 return 0; 63 }
题意:n个数(1,2。。。n) m种操作:交换下标为x y的数, 每种操作可选任意次,输出最大字典序。
思路:并查集。最后把每个位置上同一个块的从大到小输出
1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 1000010; 22 const int MOD = 1e9+7; 23 #define LL long long 24 #define mi() (l+r)>>1 25 double const pi = acos(-1); 26 27 void fre() { 28 freopen("in.txt","r",stdin); 29 } 30 31 // inline int r() { 32 // int x=0,f=1;char ch=getchar(); 33 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 34 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 35 // } 36 int a[N]; 37 int pre[N]; 38 vector<int>ans[N]; 39 vector<int>root; 40 int vis[N]; 41 int pos[N]; 42 int find(int x){ 43 if(x==pre[x]) return x; 44 return pre[x]=find(pre[x]); 45 } 46 int main(){ 47 std::ios::sync_with_stdio(false); 48 int n,m; 49 cin>>n>>m; 50 for(int i=1;i<=n;i++){ 51 cin>>a[i]; 52 pre[i]=i; 53 } 54 while(m--){ 55 int x,y; 56 cin>>x>>y; 57 x=find(x),y=find(y); 58 if(x!=y) pre[x]=y; 59 } 60 for(int i=1;i<=n;i++){ 61 int x=find(i); 62 ans[x].push_back(a[i]); 63 if(!vis[x]){ 64 root.push_back(x); 65 vis[x]=true; 66 } 67 } 68 for(int i=0;i<(int)root.size();i++){ //从大到小排序 69 sort(ans[root[i]].rbegin(),ans[root[i]].rend()); 70 } 71 for(int i=1;i<n;i++){ 72 cout<<ans[find(i)][pos[find(i)]++]<<" "; 73 } 74 cout<<ans[find(n)][pos[find(n)]]<<endl; 75 return 0; 76 }
题意:n个数,选k个出来,形成一个新的序列,该序列相邻两个数异或后数的二进制中1的个数是3的倍数。问有几个序列
思路:每个数转化成一个点,两个数满足 异或后数的二进制1的个数是3倍数的则两点之间建一条边。转化成求边的条数。
矩阵快速幂
M(i,j):(ai^aj)%3==0 则最后答案是M^(k-1)的元素和
借鉴了Q神的模板
1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 1000010; 22 const int MOD = 1e9+7; 23 #define LL long long 24 #define mi() (l+r)>>1 25 double const pi = acos(-1); 26 27 void fre() { 28 freopen("in.txt","r",stdin); 29 } 30 31 // inline int r() { 32 // int x=0,f=1;char ch=getchar(); 33 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 34 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 35 // } 36 37 38 struct Matrix{ 39 LL a[110][110]; 40 Matrix(){ 41 clc(a,0); 42 } 43 void init(){ 44 for(int i=0;i<100;i++) a[i][i]=1; 45 } 46 Matrix operator * (const Matrix &B)const{ 47 Matrix C; 48 for(int i=0;i<100;i++){ 49 for(int k=0;k<100;k++){ 50 for(int j=0;j<100;j++){ 51 (C.a[i][j]+=a[i][k]*B.a[k][j])%=MOD; 52 } 53 } 54 } 55 return C; 56 } 57 Matrix operator ^ (const LL &t) const{ 58 Matrix A= (*this),res; 59 res.init(); 60 LL p=t; 61 while(p){ 62 if(p&1) res=res*A; 63 A=A*A; 64 p>>=1; 65 } 66 return res; 67 } 68 }; 69 LL a[110]; 70 int main(){ 71 LL n,k; 72 scanf("%I64d%I64d",&n,&k); 73 for(int i=0;i<n;i++){ 74 scanf("%I64d",&a[i]); 75 } 76 Matrix A; 77 for(int i=0;i<n;i++){ 78 for(int j=0;j<n;j++){ 79 if(__builtin_popcountll(a[i]^a[j])%3==0) 80 A.a[i][j]=1; 81 } 82 } 83 LL ans=0; 84 A=A^(k-1); 85 for(int i=0;i<n;i++){ 86 for(int j=0;j<n;j++){ 87 (ans+=A.a[i][j])%=MOD; 88 } 89 } 90 printf("%I64d\n",ans); 91 return 0; 92 }
题意:n个数,任选两个相乘,q次询问,每次输出不小于x的乘积对数
思路:总的方案数-小于x
预处理每种面积的方案数
1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 3*1e6+10; 22 const int MOD = 1e9+7; 23 #define LL long long 24 #define mi() (l+r)>>1 25 double const pi = acos(-1); 26 27 void fre() { 28 freopen("in.txt","r",stdin); 29 } 30 31 // inline int r() { 32 // int x=0,f=1;char ch=getchar(); 33 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 34 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 35 // } 36 37 int cnt[N]; 38 int maxn=3*1e6; 39 LL a[N]; 40 LL sum[N]; 41 void init(){ 42 for(int i=1;i<=maxn;i++){ 43 for(int j=1;j*i<=maxn;j++){ 44 if(i==j) a[i*j]+=(LL)cnt[i]*(cnt[i]-1); 45 else a[i*j]+=(LL)cnt[i]*cnt[j]; 46 } 47 } 48 for(int i=1;i<=maxn;i++){ 49 sum[i]=sum[i-1]+a[i]; 50 } 51 } 52 int main(){ 53 int n; 54 scanf("%d",&n); 55 for(int i=0;i<n;i++){ 56 int x; 57 scanf("%d",&x); 58 if(x<=maxn) 59 cnt[x]++; 60 } 61 init(); 62 int q; 63 scanf("%d",&q); 64 while(q--){ 65 int x; 66 scanf("%d",&x); 67 LL ans=(LL)n*(n-1)-sum[x-1]; 68 printf("%I64d\n",ans); 69 } 70 return 0; 71 }