A题
思路:二进制下考虑A和B。
对A和B同一位
如果是 1 0 ,那么x对应位不管取啥那一位最后加起来结果还是1
如果是 0 1,那么x对应位不管取啥那一位最后加起来结果还是1
如果是 0 0,那么x只要取0对应这位加来就是0
如果是 1 1,那么x只要取1对应这位加起来就是0
那么也就是说1 0–>1;0 1—>1;0 0—>0;1 1—>0;那么最终的结果就是a^b。
B题
思路 : 枚举 只需要保证起点和终点相邻的两个格子不同
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
ll n, m, _;
char x[210][210];
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> x[i] + 1;
int
a = x[1][2] - '0', // Sa
b = x[2][1] - '0', // b
c = x[n][n - 1] - '0', // d
d = x[n - 1][n] - '0'; // cF
if (a == b && c == d && a != c)
cout << 0 << endl;
else if (a != b && c != d)
cout << 2 << endl
<< 1 << " " << 2 << endl // 翻转 a
<< (a == c ? n - 1 : n) << " " << (a == c ? n : n - 1) << endl;
// 注意 实际并没有改变a的值 如果c和反转前的a相等 就反转d
// 假设 S1
// 0
// 0
// 1F
else if (a != b)
cout << 1 << endl
<< (a == c ? 1 : 2) << ' ' << (a == c ? 2 : 1) << endl;
else if (c != d)
cout << 1 << endl
<< (a == c ? n : n - 1) << ' ' << (a == c ? n - 1 : n) << endl;
else
cout << 2 << endl << 1 << ' ' << 2 << endl << 2 << " " << 1 << endl;
}
int main() {
cin >> _;
while (_--) solve();
return 0;
}
构造题
往往找到需要自己模拟找到通解 然后直接输出
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1e5+5;
char s[N];
int len;
int main()
{
scanf("%s",s);
len=strlen(s);
puts("4");
puts("L 2");
puts("L 2");
puts("R 2");
printf("R %d\n",2*len+1);
return 0;
}
第二种构造
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string s;
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> s;
cout << "3\n";
cout << "L 2\n";
cout << "R 2\n";
cout << "R " << 2 * s.size() - 1 << endl;
}
H题(HDU 1559)
先用前缀和求一下
然后遍历所有的 x*y 大小的子矩阵即可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int maze[1009][1009];
int t,m,n,x,y;
int main() {
cin >> t;
while(t--)
{
scanf("%d %d %d %d",&m, &n, &x, &y);
memset(maze,0,sizeof(maze));
for(int i=1; i<=m; i++)
{
for(int j=1; j<=n; j++)
{
scanf("%d",&maze[i][j]);
maze[i][j]=maze[i][j]+maze[i-1][j]+maze[i][j-1]-maze[i-1][j-1];
}
}
int cnt=0;
for(int i=x; i<=n; i++)
{
for(int j=y; j<=m; j++)
{
cnt=max(cnt,maze[i][j]+maze[i-x][j-y]-maze[i-x][j]-maze[i][j-y]);
}
}
printf("%d\n",cnt);
}
return 0;
}
F(UVA 10815)
F题
字符串问题
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct z
{
char s[209];
}zm[500009];
int cnt,t;// cnt 计单词 t计字母
char c;
bool cmp(z a, z b)
{
return strcmp(a.s,b.s)<0;
}
int main()
{
while((c=getchar())!=EOF)// 用%s输入不好判定输入时标点的位置
{
if(c>='A'&&c<='Z') c+=32;
if(c>='a'&&c<='z'){
zm[cnt].s[t++]=c;
}
else if(!(c>='a'&&c<='z')&&zm[cnt].s[0]>='a'&&zm[cnt].s[0]<='z'){
zm[cnt].s[t]=0;
cnt++,t=0;
}
}
sort(zm,zm+cnt,cmp);
cout << zm[0].s << endl;
for(int i=1;i<cnt;i++)
{
if(strcmp(zm[i].s,zm[i-1].s)) printf("%s\n",zm[i].s); //去重
}
return 0;
}
K题
一维差分
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,l,r;
int f[100009];
int a[100009];
int main(){
while(scanf("%d",&n),n){
memset(f,0,sizeof(f));
for(int i=1;i<=n;i++){
scanf("%d%d",&l,&r);
f[l]+=1;
f[r+1]-=1;
}
for(int i=1;i<=n;i++)
a[i]=a[i-1]+f[i];
for(int i=1;i<=n;i++)
{
printf("%d",a[i]);
if(i!=n) printf(" ");
}
printf("\n");
}
return 0;
}
M题(HDU 5428)
思路:对于每一个数字,它有用的部分其实只有它的所有质因子(包括相等的)。
求出所有数的所有质因子中最小的两个,相乘就是答案。
如果所有数字的质因子个数不到两个,那么就是无解。
时间复杂度O(n*sqrt(a))O(n∗sqrt(a))
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll Max = 100000;
ll a[110];
ll count1[123456];
int main(){
int t;
scanf("%d",&t);
int n;
while(t--)
{
scanf("%d",&n);
int cnt=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
for(ll j=2; j*j <= a[i]; j++) // 注意取等 或者 j<=sqrt(a[i])
{
while(a[i] % j == 0)
{
count1[cnt++] = j;
a[i] /= j;
}
}
if(a[i]!=1)
count1[cnt++]=a[i];
}
if(cnt < 2) printf("-1\n");
else{
sort(count1,count1+cnt);
printf("%lld\n",count1[0]*count1[1]);
}
}
return 0;
}