http://codeforces.com/gym/101463
过了5题:A,B,C,D,K
A题:直接暴力搜索即可,为了方便用Python写的
def sqrt(x):
left=1
right=x
while (left<=right):
mid=(left+right)//2;
if mid*mid<x:
left=mid+1
else:
right=mid-1;
return left
import sys
f=open('a.out','w')
old=sys.stdout
sys.stdout=f
ff=open('a.in','r')
data=ff.readlines()
for line in data:
n,k=line.split()
n=int(n)
k=int(k)
q=sqrt(n*k)
i=max(sqrt(n),q-100000)
while (i<=n and i<=q+100000):
if n%i==0:
p=n//i
if abs(i-k*p)<=100000:
print(p,end='')
print(' * ',end='');
print(i)
break
i=i+1
sys.stdout=old
f.close()
ff.close()
B题:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int v[10005];
char c,a[1000005];
int main()
{
freopen("b.in","r",stdin);
freopen("b.out","w",stdout);
int i,lena,index0,index1,j;
int flag=0;
for(i=0;i<=10000;i++)
v[i]=-1;
v[0]=0;
v[1]=1;
v[2]=2;
v[3]=3;
while(scanf("%c",&c)!=EOF)
{
lena=0;
while(true)
{
if(c=='\n')break;
if(c!=' ' && c!='\r')
{
a[lena]=c;
lena++;
}
if(scanf("%c",&c)==EOF)
{
flag=1;
break;
}
//printf("%d",c);
}
//for(i=0;i<lena;i++)
//printf("%c",a[i]);
index0=index1=0;
i=lena;
i--;
j=1;
while(a[i]>='0' && a[i]<='9')
{
index1=index1+j*(a[i]-'0');
j=j*10;
i--;
}
index1=v[index1];
j=0;
while(a[j]>='0' && a[j]<='9')
{
index0=index0*10+a[j]-'0';
j++;
}
//printf("%d %d\n",index0,index1);
//printf(" %d\n",i);
while(a[i]=='f' || a[i]=='b' || a[i]=='k' || a[i]=='<' || a[i]=='=')
{
if(a[i]=='f')
{
index1=(index1%2==1)?index1+1:index1+3;
}
if(a[i]=='b')index1=index1-2;
if(a[i]=='k')printf("%d\n",index1);
if(a[i]=='<')v[index0]=index1;
if(a[i]=='=')
{
if(v[index0]==index1)printf("=\n");
else printf("#\n");
}
//printf("*%d %c\n",index1,a[i]);
i--;
}
if(flag==1)break;
}
return 0;
}
C题:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 20010;
const int maxm = 5000010;
struct Edge{
int to,next;
}edge[maxm];
int head[maxn],tot;
void init()
{
tot = 0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
int linker[maxn];
bool used[maxn];
int un;
bool dfs(int u)
{
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].to;
if(!used[v])
{
used[v] = true;
if(linker[v] == -1 || dfs(linker[v]))
{
linker[v] = u;
return true;
}
}
}
return false;
}
int hungary()
{
int res = 0;
memset(linker,-1,sizeof(linker));
for(int u = 0; u < un; u++)
{
memset(used,false,sizeof(used));
if(dfs(u)) res++;
}
return res;
}
int n,m;
int main()
{
freopen("c.in","r",stdin);
freopen("c.out","w",stdout);
while(~scanf("%d",&n))
{
un = n;
int a,b;
init();
for(int i = 0; i < n; i++)
{
scanf("%d: (%d)",&a,&m);
for(int i = 0; i < m; i++)
{
scanf("%d",&b);
addedge(a,b);
}
}
int ans = hungary();
printf("%d\n",ans);
}
return 0;
}
D题:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int ans,n,m;
const int maxx = 5010;
const int maxm = 500010;
struct edge{
int to,next;
}edge[maxm];
int tot;
int head[maxx];
int vis[maxx];
int d[maxx];
void addedge(int u,int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void dfs(int back,int u)
{
vis[u] = 1;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(v == back) continue;
if(vis[v])
ans = max(ans,d[u]-d[v]+1);
else{
d[v] = d[u]+1;
dfs(u,v);
}
}
}
int main()
{
freopen("d.in","r",stdin);
freopen("d.out","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
tot = 0;
memset(head,-1,sizeof(head));
memset(d,0,sizeof(d));
memset(vis,0,sizeof(vis));
ans = 0;
int fk = 0;
scanf("%d%d",&n,&m);
for(int i = 0; i < m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
fk = u;
}
dfs(-1,fk);
printf("%d\n",ans);
}
return 0;
}
K题:考虑清楚所有的情况即可,因为漏了一些特殊情况WA了两发
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int t,n,m,k;
int main()
{
freopen("k.in","r",stdin);
freopen("k.out","w",stdout);
scanf("%d",&t);
while (t--)
{
scanf("%d%d%d",&n,&m,&k);
if (n==1)
{
printf("-1\n");
continue;
}
if (n==2)
{
if (m%2==0) printf("-1\n");
else
{
while (k%n==0) k--;
int cnt=m/k;
m-=cnt*k;
if (m==0) printf("%d\n",cnt);
else if (m%2==0) printf("%d\n",(cnt+2)%2==0?cnt+3:cnt+2);
else printf("%d\n",(cnt+1)%2==0?cnt+2:cnt+1);
}
continue;
}
if (k==1)
{
if (m%n==0) printf("-1\n");
else printf("%d\n",m);
continue;
}
while (k%n==0) k--;
int cnt=m/k;
m-=cnt*k;
if (m==0) printf("%d\n",cnt%n==0?cnt+1:cnt);
else if (m%n==0)
{
if (cnt==0) printf("%d\n",(cnt+2)%n==0?cnt+3:cnt+2);
else if ((k-1)%n==0)
{
if (m+2<=k) printf("%d\n",(cnt+1)%n==0?cnt+2:cnt+1);
else printf("%d\n",(cnt+2)%n==0?cnt+3:cnt+2);
}
else printf("%d\n",(cnt+1)%n==0?cnt+2:cnt+1);
}
else printf("%d\n",(cnt+1)%n==0?cnt+2:cnt+1);
}
return 0;
}