@KirinBill
2017-10-25T18:20:17.000000Z
字数 6447
阅读 1094
题解
套题
目录
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
#include <cstring>
const int MAXN=1e6+5,P=1e9+7;
int n;
char s[MAXN];
inline int idx(char c){
if(isdigit(c)) return c^'0';
return 3;
}
inline int DP(){
//0,1,2,3=*,4->1
static int dp[MAXN][5];
if(s[1]=='?') dp[1][0]=dp[1][3]=dp[1][4]=1;
else if(s[1]!='1') dp[1][idx(s[1])]=1;
else dp[1][4]=1;
for(int i=1;i<n;++i){
if(s[i]=='?'){
//3
if(s[i-1]!='0'){
for(int j=1;j<4;++j)
dp[i+1][j]=dp[i][3];
}
if(s[i-1]!='*'){
//0
dp[i+1][0]=(dp[i+1][0]+dp[i][0])%P;
dp[i+1][4]=(dp[i+1][4]+dp[i][0])%P;
//4
dp[i+1][3]=(dp[i+1][3]+dp[i][4])%P;
}
if(s[i-1]=='*' || s[i-1]=='?'){
//1
dp[i+1][0]=(dp[i+1][0]+dp[i][1])%P;
dp[i+1][4]=(dp[i+1][4]+dp[i][1])%P;
//2
dp[i+1][3]=(dp[i+1][3]+dp[i][2])%P;
}
}
else{
switch(idx(s[i])){
case 0: dp[i+1][0]=(dp[i+1][0]+dp[i][0])%P;
dp[i+1][4]=(dp[i+1][4]+dp[i][0])%P;
break;
case 1: dp[i+1][0]=(dp[i+1][0]+dp[i][1])%P;
dp[i+1][4]=(dp[i+1][4]+dp[i][1])%P;
dp[i+1][3]=(dp[i+1][3]+dp[i][4])%P;
break;
case 2: dp[i+1][3]=(dp[i+1][3]+dp[i][2])%P;
break;
case 3: dp[i+1][1]=(dp[i+1][1]+dp[i][3])%P;
dp[i+1][2]=(dp[i+1][2]+dp[i][3])%P;
dp[i+1][3]=(dp[i+1][3]+dp[i][3])%P;
break;
}
}
}
if(s[n]!='?') return dp[n][idx(s[n])];
else{
int ret=0;
if(s[n-1]!='0') ret=(ret+dp[n][3])%P;
if(s[n-1]!='*') ret=(ret+dp[n][0])%P;
if(s[n-1]=='*' || s[n-1]=='?') ret=(ret+dp[n][1])%P;
return ret;
}
}
inline bool jud(){
for(int i=1;i<=n;++i){
if(s[i]=='0' && (s[i-1]=='*' || s[i+1]=='*'))
return false;
if(s[i]=='1' && (s[i-1]!='*' && s[i-1]!='?' && s[i+1]!='*' && s[i+1]!='?'))
return false;
if(s[i]=='2' && ((s[i-1]!='*' && s[i-1]!='?') || (s[i+1]!='*' && s[i+1]!='?')))
return false;
if(s[i]=='*' && (s[i-1]=='0' || s[i+1]=='0'))
return false;
}
return true;
}
int main(){
#ifdef DEBUG
setIO("a");
#endif
scanf("%s",s+1);
n=strlen(s+1);
if(!jud()) putchar('0');
else write(DP());
return 0;
}
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
#include <queue>
#include <algorithm>
#include <cstring>
using std::queue;
using std::max;
const int MAXN=305;
int n,m;
int G[MAXN][MAXN];
int dirx[]={1,0,-1,0},diry[]={0,1,0,-1};
long long dis[MAXN][MAXN];
bool inQ[MAXN][MAXN];
struct node{
int x,y;
node(int x=0,int y=0):x(x),y(y){}
};
queue<node> que;
inline void init(){
for(int i=1;i<=n;++i){
dis[i][1]=max(0,G[i][1]);
que.push(node(i,1));
inQ[i][1]=true;
dis[i][m]=max(0,G[i][m]);
que.push(node(i,m));
inQ[i][m]=true;
}
for(int i=1;i<=m;++i){
dis[1][i]=max(0,G[1][i]);
que.push(node(1,i));
inQ[1][i]=true;
dis[n][i]=max(0,G[n][i]);
que.push(node(n,i));
inQ[n][i]=true;
}
}
inline bool inG(node &u){
return 1<u.x && u.x<n && 1<u.y && u.y<m;
}
inline void SPFA(){
node u,v;
while(que.size()){
u=que.front();
que.pop();
inQ[u.x][u.y]=false;
for(int i=0,d;i<4;++i){
v.x=u.x+dirx[i],v.y=u.y+diry[i];
if(!inG(v)) continue;
d=max((long long)G[v.x][v.y],dis[u.x][u.y]);
if(dis[v.x][v.y]>d){
dis[v.x][v.y]=d;
if(!inQ[v.x][v.y]){
que.push(v);
inQ[v.x][v.y]=true;
}
}
}
}
}
int main(){
#ifdef DEBUG
setIO("b");
#endif
read(n),read(m);
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j)
read(G[i][j]);
}
memset(dis,0x7f,sizeof(dis));
init();
SPFA();
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
write(dis[i][j]-G[i][j]);
if(j!=m) putchar(' ');
}
if(i!=n) putchar('\n');
}
return 0;
}
vector
里,会很方便,而且这也是的;
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
#include <vector>
using std::vector;
const int MAXN=200005,MAXX=500005;
int n;
long long ans;
int x[MAXN],fac[MAXX],prm[MAXX];
bool slc[MAXN];
vector<int> div[MAXX];
inline void Euler_prm(){
static bool notP[MAXX];
notP[1]=true;
for(int i=2;i<MAXX;++i){
if(!notP[i]) prm[++prm[0]]=i;
for(int j=1;j<=prm[0] && i*prm[j]<MAXX;++j){
notP[i*prm[j]]=true;
if(i%prm[j]==0) break;
}
}
}
inline void pre_tab(){
Euler_prm();
for(int i=1;i<=prm[0];++i){
for(int j=prm[i];j<MAXX;j+=prm[i])
div[j].push_back(prm[i]);
}
}
void cal(int x,int cur,int num,int pm,bool add){
if(cur==div[x].size()){
if(!add) --fac[num];
ans+=pm*fac[num];
if(add) ++fac[num];
return;
}
cal(x,cur+1,num,pm,add);
cal(x,cur+1,num*div[x][cur],-pm,add);
}
int main(){
#ifdef DEBUG
setIO("c");
#endif
pre_tab();
int m;
read(n),read(m);
for(int i=1;i<=n;++i)
read(x[i]);
for(int i=1,id;i<=m;++i){
read(id);
if(!slc[id]){
slc[id]=true;
cal(x[id],0,1,1,true);
}
else{
slc[id]=false;
cal(x[id],0,1,-1,false);
}
write(ans,'\n');
}
return 0;
}