题意:让你判断n可不可以由k个不同的奇数相加而成
题解:
1.n和k的奇偶性必须相同,因为奇数个奇数相加=奇数,偶数个奇数相加=偶数
2. 1 + 3 + 5 + 7 + … + (2 * k -1)=k ^ 2 (k个奇数等差数列求和) 所以n>=k ^ 2
code:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<vector>
#include<set>
#include<sstream>
using namespace std;
typedef long long ll;
const ll maxn=1e6+10;
const ll inf=0x3f3f3f3f3f3f3f3f;
void io(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
int main()
{
io();
ll t;
cin>>t;
while(t--)
{
ll n,k;
cin>>n>>k;
if(n%2==k%2&&n>=k*k)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
B. Princesses and Princes
题意:n个公主和n个王子匹配,每个公主都有心仪的王子并写在纸上,如果最后全部匹配成功输出OPTIMAL,否则输出IMPROVE 并输出一个可能匹配的公主和王子。
题解:模拟一下就好,注意细节,调了半天…
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<vector>
#include<set>
#include<sstream>
using namespace std;
typedef long long ll;
const ll maxn=1e6+10;
const ll inf=0x3f3f3f3f3f3f3f3f;
void io(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
ll l[maxn],r[maxn];
int main()
{
io();
ll t;
cin>>t;
while(t--)
{
ll n,ans,num,o=0;
cin>>n;
for(int i=1;i<=n;i++)
l[i]=r[i]=-1;
for(int i=1;i<=n;i++)
{
cin>>num;
for(int j=1;j<=num;j++)
{
cin>>ans;
if((r[ans]==-1)&&(l[i]==-1)&&ans)
{
l[i]=1;
r[ans]=1;
o++;
}
}
}
if(o==n)
cout<<"OPTIMAL"<<endl;
else
{
cout<<"IMPROVE"<<endl;
ll ll=1,rr=1;
while(l[ll]==1) ll++;
while(r[rr]==1) rr++;
cout<<ll<<" "<<rr<<endl;
}
}
return 0;
}
C. Game with Chips
题意:一个n*m的棋盘上面分布着有k个芯片,让你找一条路径,芯片可以覆盖,让第k_i个芯片至少访问一次第location_i位置,注意这k个芯片一起动,路径长度不超过2 * n * m。
题解:我们把所有芯片都移动到左上角,从左上角依次遍历到右下角,这种最坏的情况不会超过2 * n * m。
code:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<vector>
#include<set>
#include<sstream>
using namespace std;
typedef long long ll;
const ll maxn=1e6+10;
const ll inf=0x3f3f3f3f3f3f3f3f;
void io(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
string str;
int main()
{
io();
ll n,m,k,x,y;
cin>>n>>m>>k;
for(int i=0;i<k;i++)
cin>>x>>y;
for(int i=0;i<k;i++)
cin>>x>>y;
for(int i=1;i<=n-1;i++) str+='U';
for(int i=1;i<=m-1;i++) str+='L';
for(int i=1;i<=n;i++)
{
if(i%2)
for(int j=1;j<=m-1;j++) str+='R';
else
for(int j=1;j<=m-1;j++) str+='L';
if(i!=n)
str+='D';
}
cout<<str.size()<<endl<<str<<endl;
return 0;
}
E. Count The Blocks
题意:
定义一个块:块的长度=连续序列数都是相同
比如:0001223000 长度为1的2个,长度为2的1个,长度为3的2个。
给一个n,让我们寻找从0~10 ^ n-1 (n位,位数不够补0),让我们求出长度为i 的块的个数,i ∈[1,n] 。
题解:分情况讨论。
1.长度为n的块就是n个数全部相同,就10个。
2.我们考虑长度为i的时候
<1> 第i段在中间 ,第i段有(n-i-1)个位置,它只有10种;,它的左右两边分别9种;其余的每个数都有10种可能,共n-i-2个数 ——> 10 * (n-i-1) * 9 * 9 * 10^(n-i-2)
<2> 第i段在两端,第i段有2个位置,左边或右边位置有9中,其余(n-i-1)个数分别有10种可能。 ——> 2 * 9 * 10^(n-i-1)
注意:我当时考虑这样会不会少,比如 n=10 ,i=2时, 1221221221 ,这个的块数是3,而计算的时候是算了1次。
这样是不会少的,虽然上述算了一次,我们是对于第1组22来说的,我们这3组22都会枚举到,枚举第1组22,我们计算了1次;枚举第2组22,我们计算了1次;枚举第3组22,我们计算了1次;所以1221221221块数是3,没有错误。
code:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<vector>
#include<set>
#include<sstream>
using namespace std;
typedef long long ll;
const ll maxn=1e6+10;
const ll mod=998244353;
const ll inf=0x3f3f3f3f3f3f3f3f;
void io(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
ll quick_pow(ll a,ll b)
{
ll ans=1,temp=a%mod;
while(b>0)
{
if(b&1)
ans=(ans*temp)%mod;
temp=(temp*temp)%mod;
b>>=1;
}
return ans%mod;
}
int main()
{
io();
ll n,sum;
cin>>n;
for(int i=1;i<=n-1;i++)
{
sum=0;
sum=(10*(n-i-1)*9*9*quick_pow(10,n-i-2))%mod;
sum=(sum+2*10*9*quick_pow(10,n-i-1))%mod;
cout<<sum<<" ";
}
cout<<"10"<<endl;
return 0;
}