A. Shovels and Swords 题目链接
题解:分类讨论,最后一个只需要数学(n+m)/3便可
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
ll t,n,m,k,sum,num;
ll a[105];
int main()
{
cin>>t;
while(t--){
cin>>n>>m;
if(n>m) swap(n,m);
if(m==0||n==0) {cout<<0<<endl;continue;}
if(m>=2*n) {cout<<n<<endl;continue;}
else
cout<<(n+m)/3<<endl;
}
return 0;
}
B. Shuffle 题目链接
题解:令 a=b=x,然后判断a,b与区间(l,r)的交互,ak=1在其交集的区间都可取1,min(b,l),max(a,r)
#include
#define ll long long
#define maxn 2010
#define frj(i,n,k) for(long long i=2;i<=n-k;i++)
#define mc(i,q,k) modC(i,q,k)
using namespace std;
ll n,k,t,m,x,i,a,b,sum,l,r;
int main()
{
cin>>t;
while(t--){
cin>>n>>x>>m;
a=b=x;
for(i=1;i<=m;i++)
{
cin>>l>>r;
if(l>b||a>r) continue;
a=min(a,l);
b=max(b,r);
}
cout<<b-a+1<<endl;
}
return 0;
}
C. Palindromic Paths题目链接
题解:只需将开始的点到某个点的0和1分开记录,取最小的改变值便可。
#include
#include
#include
#include
#include
#include
#define LL long long
#define PI 3.141592653589
using namespace std;
LL n,k,i,j,m,ii,jj;
LL a[75],b[75];
int main()
{
int t;
cin>>t;
while(t--){
cin>>n>>m;
LL sum=0;
memset(a,0,sizeof a);
memset(b,0,sizeof b);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
cin>>k;
if(k) a[i+j]++;
else b[i+j]++;
}
for(i=0,j=n+m-2;i<j;i++,j--)
sum+=min(a[i]+a[j],b[i]+b[j]);
cout<<sum<<endl;
}
return 0;
}
D. Two Divisors题目链接
欧拉筛
#include
using namespace std;
typedef long long LL;
const int maxn = 1e7 + 59;
int prime[maxn];
bool vis[maxn];
int cnt;
int ans[maxn][2];
int res[maxn][2];
void oulasai(int n) //欧拉筛
{
for(int i=2;i<n;i++)
{
if(!vis[i]) {
res[i][0] = i;
prime[cnt++]=i;
}
for(int j=0;j<cnt;j++)
{
if(1LL * i*prime[j]>n) break;
vis[1LL * i*prime[j]]=true;
res[1LL * i*prime[j]][0] = prime[j];
if(i%prime[j]==0) break;
}
}
}
int main()
{
oulasai(maxn);
int n;
cin>>n;
for(int i = 1; i <= n; i++){
int x;
cin>>x;
int p1 = res[x][0];
int xx = x;
while(xx % p1 == 0){
xx /= p1;
}
if(xx == 1) ans[i][0] = ans[i][1] = -1;
else {
ans[i][0] = p1;
ans[i][1] = xx;
}
}
for(int i = 1; i <= n; i++){
cout<<ans[i][0]<<" ";
}
cout<<endl;
for(int i = 1; i <= n; i++){
cout<<ans[i][1]<<" ";
}
cout<<endl;
return 0;
}