SRM 523 div2

250pt.

一个水题,直接一个dfs就行了,很久没敲代码,比赛的时候写错一个判断条件,卡了很久才发现。。。。。

 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <vector>
using namespace std;

class AlphabetPath
{
public:
	string doesItExist(vector<string> M);
};

int sx[4]={1,-1,0,0};
int sy[4]={0,0,-1,1};
int r=0,c=0;
int istrue=0;
bool dfs(int i,int j,int k,vector<string>M)
{
   if(istrue==1)
		return true;
   if(k==26)
   {
	   istrue=1;
	   return true;
   }
   for(int t=0;t<4;t++)
   {
	   int fx=i+sx[t];
	   int fy=j+sy[t];
	   if(fx>=0 && fx<r && fy>=0 && fy<c)
	   {
	      if(M[fx][fy]==(char)('A'+k))
			  dfs(fx,fy,k+1,M);
	   }
   }
   if(istrue==1)
	   return true;
   return false;
}
string AlphabetPath::doesItExist(vector<string> M)
{
   
	r = M.size();
	c = M[0].size();

	int ok=0;
	int ans=0;
	for(int i=0;i<r;i++)
	{
	   for(int j=0;j<c;j++)
	   {
		   if(M[i][j]=='A')
		   {
			   istrue=0;
		       if(dfs(i,j,1,M)==true)
                   ans=1;
			   ok=1;
			   break;
		   }
	   }
	   if(ok==1)
		   break;
	}
    if(ans==0)
		return "NO";
	else
		return "YES";
}

 

500pt。

第一种数 a+bx 的个数可以直接算出, (m-a)/b+1; 注意a<m的情况。。。。。

再枚举第二种数 c*d^y  。第二种数的个数不会有很多。

判断的时候要注意各种边界。。。。。

 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <vector>
using namespace std;

class CountingSeries
{
  public:
	   long long countThem(long long a, long long b, long long c, long long d, long long m)
	   {
            long long ans = 0;
			if(m>=a)
			   ans = (m-a)/b+1;

			if(c<=m)
			{
               if(c<a)
				   ans+=1;
			   if(c>=a && (c-a)%b!=0)
				   ans+=1;
			}

			if(d==1)
				return ans;

			for(long long t=c*d; ;t*=d)
			{
			   if(t>m)
				   break;
			   if(t<a)
				   ans+=1;
			   if(t>=a && (t-a)%b!=0)
				   ans+=1;
			}
			return ans;
	   }
};

 

 

 

 

 

 

 

 

 

 

 

posted on 2011-11-15 02:28  lwbaptx  阅读(220)  评论(0编辑  收藏  举报

导航