本次比赛难度感觉适中,题目容易理解,但是不好操作,坑有些多,多加练习。明显感觉代码能敲,算法也不是很难,但是情况太多,容易漏测试点。建议从题目中补充算法。
c题:CodeForces 151C
c题当时没时间做了,看看题意,比较别扭,读题这一的没完全过去,敲的也不自信,题意:题中两个人玩游戏,给定一个数n,每次找非平凡数(即不是一也不是它本身的因子),要求判断谁赢,如果一赢了要输出第一个非平凡数。一开始用正向思维,直接写函数,从数中找因子,结果发现很麻烦,而且过不去,后来直接从因子开始,要想一赢,那么一定存在一个非平凡数,其后的非平凡数只有一个。那么题目关键是要找到这样的非平凡数,那就从2开始找,每次n/i,只要找到两个就行。(其实就是找两个最小因子的乘积即可,两个因子可以相等)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll nl=1e5+5;
ll a[nl];
int main(){
ll n;
cin>>n;
ll i,j;
ll f=0;
for(i=2;i<=sqrt(n);i++){
if(n%i==0){
f=1;
break;
}
}
if(n==1||f==0){当n为1和素数时没有非平凡数
cout<<"1"<<endl;
cout<<"0";
}else{
j=0;
ll m=n;//不可以改变n的值
for(i=2;i<=sqrt(n);i++){
if(m%i==0){
while(m%i==0){找到最小的n的因子(含有非平凡数的)
a[j]=i;
j++;
m/=i;
if(j>=2){
break;
}
}
}
if(j>=2){
break;
}
}
if(j>=2&&a[0]*a[1]!=n){//注意判断不能等于n本身
cout<<"1"<<endl;
cout<<a[0]*a[1];
}else{
cout<<"2";
}
}
}
D题:CodeForces - 151D
咋说呢,这个题不难,就是不简单。。。题意很简单,就是情况多。注意不能再最后取模,要每次操作都要取模。不过那个k>n的情况我是真的迷惑,上代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll nl=1e9+7;
ll num=1;
ll find(ll x,ll y){//这里不能直接用pow函数,否则会超出ll
num=x;
for(ll i=1;i<y;i++){
num*=x;
num%=nl;//每次都要取模
}
return num;
}
int main(){
ll n,m,k;
ll i,j;
cin>>n>>m>>k;
ll sum;
if(k==1){
sum=find(m,n);
cout<<sum;
}else{
if(k>n){//这个点一直过不了,没想到k还能大于n
sum=find(m,n);
cout<<sum;
}else if(k<n){
if(k%2==0){
cout<<m%nl;
}else{
sum=find(m,2);//这也是易错点,这种情况包含全是一样的字母的字符串和只有两种不一样的字母的字符串(类似ababa)
cout<<sum;
}
}else{
if(k%2==0){
sum=find(m,n/2);//只考虑前一半,后面的对称就好了。
cout<<sum;
}else{
sum=find(m,n/2+1);
cout<<sum;
}
}
}
}
b题,虽然我过了,但是仍然很痛苦,这个题好折磨。。。多练习吧。有一说一,不难,但是在比赛环境中很难找到简单思路,容易敲乱。
CodeForces - 151B
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll nl=1e5+5;
struct node{
ll a,b,c;
ll ad;
string s;
}no[nl];
bool cmp1(node al,node bl){
if(al.a!=bl.a){
return al.a>bl.a;
}else{
return al.ad<bl.ad;
}
}
bool cmp2(node al,node bl){
if(al.b!=bl.b){
return al.b>bl.b;
}else{
return al.ad<bl.ad;
}
}
bool cmp3(node al,node bl){
if(al.c!=bl.c){
return al.c>bl.c;
}else{
return al.ad<bl.ad;
}
}
int main(){
ll t;
cin>>t;
ll i,j;
for(i=0;i<t;i++){
ll n;
cin>>n;
no[i].ad=i;
cin>>no[i].s;
while(n--){
string sl;
cin>>sl;
if(sl[0]==sl[1]&&sl[0]==sl[3]&&sl[0]==sl[4]&&sl[0]==sl[6]&&sl[0]==sl[7]){
no[i].a++;
}else if(sl[0]>sl[1]&&sl[1]>sl[3]&&sl[3]>sl[4]&&sl[4]>sl[6]&&sl[6]>sl[7]){
no[i].b++;
}else{
no[i].c++;
}
}
}
sort(no,no+t,cmp1);
cout<<"If you want to call a taxi, you should call: ";
cout<<no[0].s;
for(i=1;i<t;i++){
if(no[i].a==no[0].a){
cout<<", "<<no[i].s;
}
}
cout<<"."<<endl;
sort(no,no+t,cmp2);
cout<<"If you want to order a pizza, you should call: ";
cout<<no[0].s;
for(i=1;i<t;i++){
if(no[i].b==no[0].b){
cout<<", "<<no[i].s;
}
}
cout<<"."<<endl;
sort(no,no+t,cmp3);
cout<<"If you want to go to a cafe with a wonderful girl, you should call: ";
cout<<no[0].s;
for(i=1;i<t;i++){
if(no[i].c==no[0].c){
cout<<", "<<no[i].s;
}
}
cout<<"."<<endl;
}