parent
9da6cdd5bd
commit
1d53c6ce3d
|
@ -0,0 +1,42 @@
|
|||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
const int N=100001;
|
||||
int a, n, f[N];
|
||||
__int64 ans, tmp, tmp1;
|
||||
void insert(int i)
|
||||
{
|
||||
for(; i<=n; i+=i&(-i))
|
||||
f[i] += 1;
|
||||
}
|
||||
int query(int i)
|
||||
{
|
||||
int tmp=0;
|
||||
for(; i>0; i-=i&(-i))
|
||||
tmp += f[i];
|
||||
return tmp;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i, cas, cas1=1;
|
||||
scanf("%d", &cas);
|
||||
while(cas--)
|
||||
{
|
||||
scanf("%d", &n);
|
||||
ans = 0;
|
||||
for(i=1; i<=n; i++)
|
||||
f[i] = 0;
|
||||
for(i=1; i<=n; i++)
|
||||
{
|
||||
scanf("%d", &a);
|
||||
insert(a);
|
||||
tmp = query(a-1);
|
||||
tmp1 = (n-a)-(i-1-tmp);
|
||||
ans -= tmp*tmp1;
|
||||
if(tmp1>=2)
|
||||
ans += tmp1*(tmp1-1)/2;
|
||||
}
|
||||
printf("Case #%d: %I64d\n", cas1++, ans%100000007);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<string>
|
||||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#include<cmath>
|
||||
using namespace std;
|
||||
struct block
|
||||
{
|
||||
int x,y,z,d;
|
||||
}b[1005];
|
||||
long long dp[1005];
|
||||
int n;
|
||||
bool cmp(block a,block b)
|
||||
{
|
||||
if(a.x!=b.x)return a.x<b.x;
|
||||
if(a.y!=b.y)return a.y<b.y;
|
||||
return a.d>b.d;
|
||||
}
|
||||
void DP()
|
||||
{
|
||||
long long ans=b[0].z;
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
dp[i]=b[i].z;
|
||||
ans=max(ans,dp[i]);
|
||||
}
|
||||
for(int i=1;i<n;i++)
|
||||
{
|
||||
if(b[i].d==0)
|
||||
{
|
||||
for(int j=0;j<i;j++)
|
||||
{
|
||||
if(b[j].x<=b[i].x&&b[j].y<=b[i].y)
|
||||
dp[i]=max(dp[i],dp[j]+b[i].z);
|
||||
}
|
||||
}
|
||||
if(b[i].d==1)
|
||||
{
|
||||
for(int j=0;j<i;j++)
|
||||
{
|
||||
if(b[j].x<=b[i].x&&b[i].y>=b[j].y&&(b[i].y*b[j].y||b[i].x>b[j].x))
|
||||
dp[i]=max(dp[i],dp[j]+b[i].z);
|
||||
}
|
||||
}
|
||||
if(b[i].d==2)
|
||||
{
|
||||
for(int j=0;j<i;j++)
|
||||
{
|
||||
if(b[i].x>b[j].x&&b[i].y>b[j].y)
|
||||
{
|
||||
dp[i]=max(dp[i],dp[j]+b[i].z);
|
||||
}
|
||||
}
|
||||
}
|
||||
ans=max(ans,dp[i]);
|
||||
}
|
||||
cout<<ans<<'\n';
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
scanf("%d",&n);
|
||||
if(n==0)break;
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
scanf("%d%d%d%d",&b[i].x,&b[i].y,&b[i].z,&b[i].d);
|
||||
if(b[i].x<b[i].y)
|
||||
swap(b[i].x,b[i].y);
|
||||
}
|
||||
sort(b,b+n,cmp);
|
||||
DP();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
char tb[][150] = {
|
||||
"2",
|
||||
"6",
|
||||
"30",
|
||||
"210",
|
||||
"2310",
|
||||
"30030",
|
||||
"510510",
|
||||
"9699690",
|
||||
"223092870",
|
||||
"6469693230",
|
||||
"200560490130",
|
||||
"7420738134810",
|
||||
"304250263527210",
|
||||
"13082761331670030",
|
||||
"614889782588491410",
|
||||
"32589158477190044730",
|
||||
"1922760350154212639070",
|
||||
"117288381359406970983270",
|
||||
"7858321551080267055879090",
|
||||
"557940830126698960967415390",
|
||||
"40729680599249024150621323470",
|
||||
"3217644767340672907899084554130",
|
||||
"267064515689275851355624017992790",
|
||||
"23768741896345550770650537601358310",
|
||||
"2305567963945518424753102147331756070",
|
||||
"232862364358497360900063316880507363070",
|
||||
"23984823528925228172706521638692258396210",
|
||||
"2566376117594999414479597815340071648394470",
|
||||
"279734996817854936178276161872067809674997230",
|
||||
"31610054640417607788145206291543662493274686990",
|
||||
"4014476939333036189094441199026045136645885247730",
|
||||
"525896479052627740771371797072411912900610967452630",
|
||||
"72047817630210000485677936198920432067383702541010310",
|
||||
"10014646650599190067509233131649940057366334653200433090",
|
||||
"1492182350939279320058875736615841068547583863326864530410",
|
||||
"225319534991831177328890236228992001350685163362356544091910",
|
||||
"35375166993717494840635767087951744212057570647889977422429870",
|
||||
"5766152219975951659023630035336134306565384015606066319856068810",
|
||||
"962947420735983927056946215901134429196419130606213075415963491270",
|
||||
"166589903787325219380851695350896256250980509594874862046961683989710",
|
||||
"29819592777931214269172453467810429868925511217482600306406141434158090",
|
||||
"5397346292805549782720214077673687806275517530364350655459511599582614290",
|
||||
"1030893141925860008499560888835674370998623848299590975192766715520279329390",
|
||||
"198962376391690981640415251545285153602734402721821058212203976095413910572270",
|
||||
"39195588149163123383161804554421175259738677336198748467804183290796540382737190",
|
||||
"7799922041683461553249199106329813876687996789903550945093032474868511536164700810",
|
||||
"1645783550795210387735581011435590727981167322669649249414629852197255934130751870910",
|
||||
"367009731827331916465034565550136732339800312955331782619462457039988073311157667212930",
|
||||
"83311209124804345037562846379881038241134671040860314654617977748077292641632790457335110",
|
||||
"19078266889580195013601891820992757757219839668357012055907516904309700014933909014729740190",
|
||||
"4445236185272185438169240794291312557432222642727183809026451438704160103479600800432029464270",
|
||||
"1062411448280052319722448549835623701226301211611796930357321893850294264731624591303255041960530",
|
||||
"256041159035492609053110100510385311995538591998443060216114576417920917800321526504084465112487730",
|
||||
"64266330917908644872330635228106713310880186591609208114244758680898150367880703152525200743234420230"
|
||||
};
|
||||
int main()
|
||||
{
|
||||
char str[150];
|
||||
int t;
|
||||
cin>>t;
|
||||
while(t--){
|
||||
scanf("%s",str);
|
||||
int i = 0;
|
||||
for(;;i++){
|
||||
if(strlen(str)>strlen(tb[i])) continue;
|
||||
if(strlen(str)<strlen(tb[i])) break;
|
||||
if(strcmp(str,tb[i])<0) break;
|
||||
}
|
||||
cout<<tb[i-1]<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
#include<stdio.h>
|
||||
#include<algorithm>
|
||||
#include<string.h>
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
const int MAXN=10010;
|
||||
struct Node
|
||||
{
|
||||
int to;
|
||||
int next;
|
||||
int cost;
|
||||
}edge[MAXN*2];
|
||||
int head[MAXN];
|
||||
int tol;
|
||||
int dp[MAXN][11];
|
||||
void init()
|
||||
{
|
||||
memset(head,-1,sizeof(head));
|
||||
tol=0;
|
||||
memset(dp,0,sizeof(dp));
|
||||
}
|
||||
void add(int a,int b,int val)
|
||||
{
|
||||
edge[tol].to=b;
|
||||
edge[tol].cost=val;
|
||||
edge[tol].next=head[a];
|
||||
head[a]=tol++;
|
||||
edge[tol].to=a;
|
||||
edge[tol].cost=val;
|
||||
edge[tol].next=head[b];
|
||||
head[b]=tol++;
|
||||
}
|
||||
int N,K;
|
||||
void dfs(int u,int pre)
|
||||
{
|
||||
for(int i=head[u];i!=-1;i=edge[i].next)
|
||||
{
|
||||
int v=edge[i].to;
|
||||
if(v==pre)continue;
|
||||
dfs(v,u);
|
||||
for(int k=K;k>=0;k--)
|
||||
{
|
||||
dp[u][k]+=dp[v][0]+2*edge[i].cost;
|
||||
for(int j=1;j<=k;j++)
|
||||
dp[u][k]=min(dp[u][k],dp[u][k-j]+dp[v][j]+j*edge[i].cost);
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int S;
|
||||
int a,b,val;
|
||||
while(scanf("%d%d%d",&N,&S,&K)!=EOF)
|
||||
{
|
||||
init();
|
||||
for(int i=1;i<N;i++)
|
||||
{
|
||||
scanf("%d%d%d",&a,&b,&val);
|
||||
add(a,b,val);
|
||||
}
|
||||
dfs(S,-1);
|
||||
printf("%d\n",dp[S][K]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
#include<iostream>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
int a[500005];
|
||||
int l,n,m;
|
||||
int solve(int step){
|
||||
int s=0,count=0,i=0;
|
||||
while(s<l){
|
||||
s+=step;
|
||||
while(a[i]<=s)
|
||||
i++;
|
||||
s=a[i-1];
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
int binary_search(int low,int high){
|
||||
int left=low,right=high,mid;
|
||||
while(left<right){
|
||||
mid=(left+right)/2;
|
||||
if(solve(mid)<=m){
|
||||
right=mid;
|
||||
}
|
||||
else{
|
||||
left=mid+1;
|
||||
}
|
||||
}
|
||||
return left;
|
||||
}
|
||||
int main(){
|
||||
while(cin>>l>>n>>m){
|
||||
int ans,i;
|
||||
for(i=0;i<n;i++)
|
||||
scanf("%d",&a[i]);
|
||||
sort(a,a+n);
|
||||
a[n]=l; a[n+1]=l*2;
|
||||
ans=a[0];
|
||||
for(i=1;i<=n;i++)
|
||||
if(a[i]-a[i-1]>ans)
|
||||
ans=a[i]-a[i-1];
|
||||
ans=binary_search(ans,l);
|
||||
cout<<ans<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,115 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<vector>
|
||||
#include<algorithm>
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
const int maxn = 10010;
|
||||
int tot,n,m,ans;
|
||||
const int inf = 999999999;
|
||||
struct Edge
|
||||
{
|
||||
int t,w;
|
||||
int next;
|
||||
int vis;
|
||||
}edge[1000005];
|
||||
int head[maxn],E;
|
||||
void add(int s,int t,int w)
|
||||
{
|
||||
edge[E].t=t;
|
||||
edge[E].w=w;
|
||||
edge[E].vis=0;
|
||||
edge[E].next=head[s];
|
||||
head[s]=E++;
|
||||
}
|
||||
int Btype,Time,N,M;
|
||||
int dfn[maxn],low[maxn],belong[maxn];
|
||||
int st[maxn],Top;
|
||||
int tt[100010][3],cnt;
|
||||
inline int min(int a,int b){return a<b?a:b;}
|
||||
void dfs(int s)
|
||||
{
|
||||
int i,t;
|
||||
st[++Top]=s;
|
||||
dfn[s]=low[s]=++Time;
|
||||
for (i=head[s];i!=-1;i=edge[i].next)
|
||||
{
|
||||
if(edge[i].vis)continue;
|
||||
edge[i].vis=edge[i^1].vis=1;
|
||||
t=edge[i].t;
|
||||
if (!dfn[t])
|
||||
{
|
||||
dfs(t);
|
||||
low[s]=min(low[s],low[t]);
|
||||
if(dfn[s]<low[t])
|
||||
{
|
||||
tt[++cnt][0]=s,tt[cnt][1]=t,tt[cnt][2]=edge[i].w;
|
||||
}
|
||||
}
|
||||
else low[s]=min(low[s],dfn[t]);
|
||||
}
|
||||
if(dfn[s]==low[s])
|
||||
{
|
||||
Btype++;
|
||||
do{
|
||||
t=st[Top--];
|
||||
belong[t]=Btype;
|
||||
}while(t!=s);
|
||||
}
|
||||
}
|
||||
void SCC(int n)
|
||||
{
|
||||
int i;
|
||||
Time=0;Btype=0;Top=0;
|
||||
memset(dfn,0,sizeof(int)*(n+1));
|
||||
for(i=1;i<=n;i++)if(!dfn[i])
|
||||
dfs(i);
|
||||
}
|
||||
int find(int s,int t)
|
||||
{
|
||||
int i;
|
||||
int Min=inf,vice_Min=inf,rr=inf;
|
||||
for(i=head[s];i!=-1;i=edge[i].next)
|
||||
{
|
||||
int v=edge[i].t;
|
||||
if(v==t) continue;
|
||||
int w=find(v,s);
|
||||
if(w<vice_Min) vice_Min=w;
|
||||
if(edge[i].w<vice_Min) vice_Min=edge[i].w;
|
||||
if(Min>vice_Min) swap(vice_Min,Min);
|
||||
if(Min<rr) rr=Min;
|
||||
}
|
||||
if(ans>vice_Min) ans=vice_Min;
|
||||
return rr;
|
||||
}
|
||||
int a1,a2,flag;
|
||||
int main()
|
||||
{
|
||||
int i,a,b,w;
|
||||
while(scanf("%d%d",&n,&m)!=EOF)
|
||||
{
|
||||
memset(head,-1,sizeof(head));E=0;
|
||||
for(i=0;i<m;i++)
|
||||
{
|
||||
scanf("%d%d%d",&a,&b,&w);
|
||||
add(a,b,w);
|
||||
add(b,a,w);
|
||||
}
|
||||
cnt=0;
|
||||
SCC(n);
|
||||
memset(head,-1,sizeof(head));E=0;
|
||||
int C=inf;
|
||||
for(i=1;i<=cnt;i++)
|
||||
{
|
||||
add(belong[tt[i][0]],belong[tt[i][1]],tt[i][2]);
|
||||
add(belong[tt[i][1]],belong[tt[i][0]],tt[i][2]);
|
||||
if(tt[i][2]<C){C=tt[i][2]; a=belong[tt[i][0]],b=belong[tt[i][1]];}
|
||||
}
|
||||
ans=inf;
|
||||
find(a,b);
|
||||
find(b,a);
|
||||
if(ans==inf) printf("-1\n");
|
||||
else printf("%d\n",ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
#include<queue>
|
||||
#include<iostream>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
struct mycmp
|
||||
{
|
||||
bool operator()(const int &a,const int &b)
|
||||
{
|
||||
return a>b;
|
||||
}
|
||||
};
|
||||
int main()
|
||||
{
|
||||
int n,k,val;
|
||||
char str[5];
|
||||
int count;
|
||||
while(scanf("%d%d",&n,&k)!=EOF)
|
||||
{
|
||||
priority_queue<int,vector<int>,mycmp> pq;
|
||||
while(n--)
|
||||
{
|
||||
scanf("%s",str);
|
||||
if(str[0]=='I')
|
||||
{
|
||||
scanf("%d",&val);
|
||||
pq.push(val);
|
||||
while(pq.size()>k)
|
||||
pq.pop();
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%d\n",pq.top());
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<string>
|
||||
#include<queue>
|
||||
#include<iostream>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
const int INF=1000100000;
|
||||
struct node
|
||||
{
|
||||
int x,y;
|
||||
}n[1010];
|
||||
bool cmp(node a,node b)
|
||||
{
|
||||
return a.x<b.x;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int p,r,xmax,xmin,ymax,ymin,y[1010];
|
||||
while(scanf("%d%d",&p,&r)!=EOF)
|
||||
{
|
||||
ymax=xmax=-INF;ymin=xmin=INF;
|
||||
for(int i=0;i<p;i++)
|
||||
{
|
||||
scanf("%d%d",&n[i].x,&n[i].y);
|
||||
if(ymax<n[i].y)ymax=n[i].y;
|
||||
if(ymin>n[i].y)ymin=n[i].y;
|
||||
if(xmax<n[i].x)xmax=n[i].x;
|
||||
if(xmin>n[i].x)xmin=n[i].x;
|
||||
}
|
||||
if((ymax-ymin<=r)&&(xmax-xmin<=r))
|
||||
{
|
||||
printf("%d\n",p);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
sort(n,n+p,cmp);
|
||||
int ans=0;
|
||||
for(int i=0;i<p;i++)
|
||||
{
|
||||
int k=0;
|
||||
for(int j=i;n[j].x<=n[i].x+r&&j<p;j++)
|
||||
{
|
||||
y[k++]=n[j].y;
|
||||
}
|
||||
sort(y,y+k);
|
||||
int count=0,tem=0;
|
||||
for(int j=0;j<k&&tem<k;j++)
|
||||
{
|
||||
while(y[tem]-y[j]<=r&&tem<k)tem++;
|
||||
if(count<tem-j)count=tem-j;
|
||||
}
|
||||
if(ans<count)ans=count;
|
||||
}
|
||||
printf("%d\n",ans);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
const int MAXN = 100000 + 1234;
|
||||
struct EDGE
|
||||
{
|
||||
int u, v;
|
||||
} edge[MAXN * 2];
|
||||
int cnt;
|
||||
int minchild[MAXN][2];
|
||||
int mindown[MAXN];
|
||||
int fa[MAXN][20];
|
||||
int head[MAXN];
|
||||
int n;
|
||||
int depth[MAXN], maxdepth, topk;
|
||||
bool cmp(EDGE a, EDGE b)
|
||||
{
|
||||
if(a. u != b.u) return a.u < b.u;
|
||||
return a.v < b.v;
|
||||
}
|
||||
void insert(int *s, int x)
|
||||
{
|
||||
if(x < s[0])
|
||||
{
|
||||
s[1] = s[0];
|
||||
s[0] = x;
|
||||
}
|
||||
else if(x < s[1]) s[1] = x;
|
||||
}
|
||||
void dfs(int root)
|
||||
{
|
||||
minchild[root][0] = minchild[root][1] = mindown[root] = n + 1;
|
||||
for(int p = head[root]; p < cnt && edge[p].u == root; p++)
|
||||
{
|
||||
int v = edge[p].v;
|
||||
if(v == fa[root][0]) continue;
|
||||
fa[v][0] = root;
|
||||
insert(minchild[root], v);
|
||||
depth[v] = depth[root] + 1;
|
||||
dfs(v);
|
||||
mindown[root] = min(mindown[root], mindown[v]);
|
||||
}
|
||||
maxdepth = max(maxdepth, depth[root]);
|
||||
mindown[root] = min(mindown[root], minchild[root][0]);
|
||||
}
|
||||
int findfather(int x, int step)
|
||||
{
|
||||
int k = topk - 1;
|
||||
while(step > 0)
|
||||
{
|
||||
while((1 << k) > step) k--;
|
||||
x = fa[x][k];
|
||||
step -= (1 << k);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
int q, x, y;
|
||||
scanf("%d", &T);
|
||||
while(T--)
|
||||
{
|
||||
scanf("%d%d", &n, &q);
|
||||
cnt = n - 1;
|
||||
for(int i = 0; i < cnt; i++)
|
||||
{
|
||||
scanf("%d%d", &edge[i].u, &edge[i].v);
|
||||
edge[i + cnt].u = edge[i].v;
|
||||
edge[i + cnt].v = edge[i].u;
|
||||
}
|
||||
cnt = cnt + cnt;
|
||||
sort(edge, edge + cnt, cmp);
|
||||
head[1] = 0;
|
||||
for(int i = 1; i < cnt; i++)
|
||||
if(edge[i].u != edge[i-1].u) head[edge[i].u] = i;
|
||||
fa[1][0] = 0;
|
||||
depth[1] = maxdepth = 0;
|
||||
dfs(1);
|
||||
topk = 0;
|
||||
while((1 << topk) <= maxdepth) topk++;
|
||||
for(int k = 1; k < topk; k++)
|
||||
for(int i = 1; i <= n; i++)
|
||||
if(fa[i][k - 1] != 0) fa[i][k] = fa[fa[i][k-1]][k-1];
|
||||
int mindown1[2];
|
||||
mindown1[0] = mindown1[1] = n + 1;
|
||||
for(int p = head[1]; p < cnt && edge[p].u == 1; p++)
|
||||
{
|
||||
int v = edge[p].v;
|
||||
int rv = min(mindown[v], v);
|
||||
insert(mindown1, rv);
|
||||
}
|
||||
while(q--)
|
||||
{
|
||||
scanf("%d%d", &x, &y);
|
||||
int ans1 = n + 1, ans2 =n + 1;
|
||||
if(y == 1)
|
||||
{
|
||||
int z = findfather(x, depth[x] - depth[y] - 1);
|
||||
int rz = min(z, mindown[z]);
|
||||
ans1 = (z == minchild[1][0] ? minchild[1][1] : minchild[1][0]);
|
||||
ans2 = (rz == mindown1[0] ? mindown1[1] : mindown1[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
bool flag = false;
|
||||
int z;
|
||||
if(depth[y] < depth[x])
|
||||
{
|
||||
z = findfather(x, depth[x] - depth[y] - 1);
|
||||
if(fa[z][0] == y) flag = true;
|
||||
}
|
||||
if(flag)
|
||||
{
|
||||
ans1 = (z == minchild[y][0] ? min(minchild[y][1], fa[y][0]) : min(minchild[y][0], fa[y][0]));
|
||||
ans2 = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
ans1 = minchild[y][0];
|
||||
ans2 = mindown[y];
|
||||
}
|
||||
}
|
||||
if (ans1 > n) printf("no answers!\n");
|
||||
else printf("%d %d\n", ans1, ans2);
|
||||
}
|
||||
puts("");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,122 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<string>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
#define INF 0x3FFFFFF
|
||||
#define MAXN 2222
|
||||
struct edge
|
||||
{
|
||||
int u,v,w;
|
||||
}e[99999999];
|
||||
int a,b,c;
|
||||
int n,en;
|
||||
int pre[MAXN],in[MAXN],id[MAXN],vis[MAXN];
|
||||
int x[MAXN],y[MAXN],z[MAXN];
|
||||
int getdis(int i,int j)
|
||||
{
|
||||
return abs(x[i]-x[j])+abs(y[i]-y[j])+abs(z[i]-z[j]);
|
||||
}
|
||||
void add(int u,int v,int w)
|
||||
{
|
||||
e[en].u=u;
|
||||
e[en].v=v;
|
||||
e[en++].w=w;
|
||||
}
|
||||
int zl(int root ,int vn)
|
||||
{
|
||||
int ans=0;
|
||||
int cnt;
|
||||
while(1)
|
||||
{
|
||||
for(int i=0;i<vn;i++)
|
||||
in[i]=INF,id[i]=-1,vis[i]=-1;
|
||||
for(int i=0;i<en;i++)
|
||||
{
|
||||
if(in[e[i].v]>e[i].w && e[i].u!=e[i].v)
|
||||
{
|
||||
pre[e[i].v]=e[i].u;
|
||||
in[e[i].v]=e[i].w;
|
||||
}
|
||||
}
|
||||
in[root]=0;
|
||||
pre[root]=root;
|
||||
for(int i=0;i<vn;i++)
|
||||
{
|
||||
ans+=in[i];
|
||||
if(in[i]==INF)
|
||||
return -1;
|
||||
}
|
||||
cnt=0;
|
||||
for(int i=0;i<vn;i++)
|
||||
{
|
||||
if(vis[i]==-1)
|
||||
{
|
||||
int t=i;
|
||||
while(vis[t]==-1)
|
||||
{
|
||||
vis[t]=i;
|
||||
t=pre[t];
|
||||
}
|
||||
if(vis[t]!=i || t==root) continue;
|
||||
for(int j=pre[t];j!=t;j=pre[j])
|
||||
id[j]=cnt;
|
||||
id[t]=cnt++;
|
||||
}
|
||||
}
|
||||
if(cnt==0) break;
|
||||
for(int i=0;i<vn;i++)
|
||||
if(id[i]==-1)
|
||||
id[i]=cnt++;
|
||||
for(int i=0;i<en;i++)
|
||||
{
|
||||
int u,v;
|
||||
u=e[i].u;
|
||||
v=e[i].v;
|
||||
e[i].u=id[u];
|
||||
e[i].v=id[v];
|
||||
e[i].w-=in[v];
|
||||
}
|
||||
vn=cnt;
|
||||
root=id[root];
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int cnt,j;
|
||||
en=0;
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
scanf("%d%d%d",&x[i],&y[i],&z[i]);
|
||||
add(0,i,z[i]*a);
|
||||
}
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
scanf("%d",&cnt);
|
||||
for(int k=1;k<=cnt;k++)
|
||||
{
|
||||
scanf("%d",&j);
|
||||
if(i==j)
|
||||
continue;
|
||||
if(z[i]>=z[j])
|
||||
add(i,j,getdis(i,j)*b);
|
||||
else
|
||||
add(i,j,getdis(i,j)*b+c);
|
||||
}
|
||||
}
|
||||
int ans=zl(0,n+1);
|
||||
if(ans==-1)
|
||||
{
|
||||
printf("poor XiaoA\n");
|
||||
return;
|
||||
}
|
||||
printf("%d\n",ans);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(scanf("%d%d%d%d",&n,&a,&b,&c)!=EOF && n+a+b+c)
|
||||
solve();
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,252 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
using namespace std;
|
||||
const int MAXN = 300010;
|
||||
int ch[MAXN][2],pre[MAXN],key[MAXN];
|
||||
int add[MAXN],rev[MAXN],Max[MAXN];
|
||||
bool rt[MAXN];
|
||||
void Update_Add(int r,int d)
|
||||
{
|
||||
if(!r)return;
|
||||
key[r] += d;
|
||||
add[r] += d;
|
||||
Max[r] += d;
|
||||
}
|
||||
void Update_Rev(int r)
|
||||
{
|
||||
if(!r)return;
|
||||
swap(ch[r][0],ch[r][1]);
|
||||
rev[r] ^= 1;
|
||||
}
|
||||
void push_down(int r)
|
||||
{
|
||||
if(add[r])
|
||||
{
|
||||
Update_Add(ch[r][0],add[r]);
|
||||
Update_Add(ch[r][1],add[r]);
|
||||
add[r] = 0;
|
||||
}
|
||||
if(rev[r])
|
||||
{
|
||||
Update_Rev(ch[r][0]);
|
||||
Update_Rev(ch[r][1]);
|
||||
rev[r] = 0;
|
||||
}
|
||||
}
|
||||
void push_up(int r)
|
||||
{
|
||||
Max[r] = max(max(Max[ch[r][0]],Max[ch[r][1]]),key[r]);
|
||||
}
|
||||
void Rotate(int x)
|
||||
{
|
||||
int y = pre[x], kind = ch[y][1]==x;
|
||||
ch[y][kind] = ch[x][!kind];
|
||||
pre[ch[y][kind]] = y;
|
||||
pre[x] = pre[y];
|
||||
pre[y] = x;
|
||||
ch[x][!kind] = y;
|
||||
if(rt[y])
|
||||
rt[y] = false, rt[x] = true;
|
||||
else
|
||||
ch[pre[x]][ch[pre[x]][1]==y] = x;
|
||||
push_up(y);
|
||||
}
|
||||
void P(int r)
|
||||
{
|
||||
if(!rt[r])P(pre[r]);
|
||||
push_down(r);
|
||||
}
|
||||
void Splay(int r)
|
||||
{
|
||||
P(r);
|
||||
while( !rt[r] )
|
||||
{
|
||||
int f = pre[r], ff = pre[f];
|
||||
if(rt[f])
|
||||
Rotate(r);
|
||||
else if( (ch[ff][1]==f)==(ch[f][1]==r) )
|
||||
Rotate(f), Rotate(r);
|
||||
else
|
||||
Rotate(r), Rotate(r);
|
||||
}
|
||||
push_up(r);
|
||||
}
|
||||
int Access(int x)
|
||||
{
|
||||
int y = 0;
|
||||
for( ; x ; x = pre[y=x])
|
||||
{
|
||||
Splay(x);
|
||||
rt[ch[x][1]] = true, rt[ch[x][1]=y] = false;
|
||||
push_up(x);
|
||||
}
|
||||
return y;
|
||||
}
|
||||
bool judge(int u,int v)
|
||||
{
|
||||
while(pre[u]) u = pre[u];
|
||||
while(pre[v]) v = pre[v];
|
||||
return u == v;
|
||||
}
|
||||
void mroot(int r)
|
||||
{
|
||||
Access(r);
|
||||
Splay(r);
|
||||
Update_Rev(r);
|
||||
}
|
||||
void lca(int &u,int &v)
|
||||
{
|
||||
Access(v), v = 0;
|
||||
while(u)
|
||||
{
|
||||
Splay(u);
|
||||
if(!pre[u])return;
|
||||
rt[ch[u][1]] = true;
|
||||
rt[ch[u][1]=v] = false;
|
||||
push_up(u);
|
||||
u = pre[v = u];
|
||||
}
|
||||
}
|
||||
void link(int u,int v)
|
||||
{
|
||||
if(judge(u,v))
|
||||
{
|
||||
puts("-1");
|
||||
return;
|
||||
}
|
||||
mroot(u);
|
||||
pre[u] = v;
|
||||
}
|
||||
void cut(int u,int v)
|
||||
{
|
||||
if(u == v || !judge(u,v))
|
||||
{
|
||||
puts("-1");
|
||||
return;
|
||||
}
|
||||
mroot(u);
|
||||
Splay(v);
|
||||
pre[ch[v][0]] = pre[v];
|
||||
pre[v] = 0;
|
||||
rt[ch[v][0]] = true;
|
||||
ch[v][0] = 0;
|
||||
push_up(v);
|
||||
}
|
||||
void ADD(int u,int v,int w)
|
||||
{
|
||||
if(!judge(u,v))
|
||||
{
|
||||
puts("-1");
|
||||
return;
|
||||
}
|
||||
lca(u,v);
|
||||
Update_Add(ch[u][1],w);
|
||||
Update_Add(v,w);
|
||||
key[u] += w;
|
||||
push_up(u);
|
||||
}
|
||||
void query(int u,int v)
|
||||
{
|
||||
if(!judge(u,v))
|
||||
{
|
||||
puts("-1");
|
||||
return;
|
||||
}
|
||||
lca(u,v);
|
||||
printf("%d\n",max(max(Max[v],Max[ch[u][1]]),key[u]));
|
||||
}
|
||||
struct Edge
|
||||
{
|
||||
int to,next;
|
||||
}edge[MAXN*2];
|
||||
int head[MAXN],tot;
|
||||
void addedge(int u,int v)
|
||||
{
|
||||
edge[tot].to = v;
|
||||
edge[tot].next = head[u];
|
||||
head[u] = tot++;
|
||||
}
|
||||
void dfs(int u)
|
||||
{
|
||||
for(int i = head[u];i != -1; i = edge[i].next)
|
||||
{
|
||||
int v = edge[i].to;
|
||||
if(pre[v] != 0)continue;
|
||||
pre[v] = u;
|
||||
dfs(v);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n,q,u,v;
|
||||
while(scanf("%d",&n) == 1)
|
||||
{
|
||||
tot = 0;
|
||||
for(int i = 0;i <= n;i++)
|
||||
{
|
||||
head[i] = -1;
|
||||
pre[i] = 0;
|
||||
ch[i][0] = ch[i][1] = 0;
|
||||
rev[i] = 0;
|
||||
add[i] = 0;
|
||||
rt[i] = true;
|
||||
}
|
||||
Max[0] = -2000000000;
|
||||
for(int i = 1;i < n;i++)
|
||||
{
|
||||
scanf("%d%d",&u,&v);
|
||||
addedge(u,v);
|
||||
addedge(v,u);
|
||||
}
|
||||
for(int i = 1;i <= n;i++)
|
||||
{
|
||||
scanf("%d",&key[i]);
|
||||
Max[i] = key[i];
|
||||
}
|
||||
scanf("%d",&q);
|
||||
pre[1] = -1;
|
||||
dfs(1);
|
||||
pre[1] = 0;
|
||||
int op;
|
||||
while(q--)
|
||||
{
|
||||
scanf("%d",&op);
|
||||
if(op == 1)
|
||||
{
|
||||
int x,y;
|
||||
scanf("%d%d",&x,&y);
|
||||
link(x,y);
|
||||
}
|
||||
else if(op == 2)
|
||||
{
|
||||
int x,y;
|
||||
scanf("%d%d",&x,&y);
|
||||
cut(x,y);
|
||||
}
|
||||
else if(op == 3)
|
||||
{
|
||||
int w,x,y;
|
||||
scanf("%d%d%d",&w,&x,&y);
|
||||
ADD(x,y,w);
|
||||
}
|
||||
else
|
||||
{
|
||||
int x,y;
|
||||
scanf("%d%d",&x,&y);
|
||||
query(x,y);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
using namespace std;
|
||||
#define min(a,b) ((a)<(b)?(a):(b))
|
||||
const long N=100010;
|
||||
__int64 a[N],aa,minx;
|
||||
int main()
|
||||
{
|
||||
int n,t,b;
|
||||
int i,j,k;
|
||||
while (cin>>t)
|
||||
{
|
||||
minx=k=0;
|
||||
while (t--)
|
||||
{
|
||||
scanf("%d%I64d%d",&n,&aa,&b);
|
||||
minx=n*b+2*aa;
|
||||
scanf("%I64d",&a[0]);
|
||||
for (i=1;i<n;i++)
|
||||
{
|
||||
scanf("%I64d",&a[i]);
|
||||
if(a[i]-1==a[i-1]) continue;
|
||||
minx+=min((a[i]-a[i-1]-1)*b,2*aa);
|
||||
}
|
||||
printf("Case #%d: %I64d\n",++k,minx);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
int n;
|
||||
char maze[20];
|
||||
bool vis[1<<16];
|
||||
int bfs(){
|
||||
queue<pair<int,int> > que;
|
||||
que.push(make_pair(0,0));
|
||||
memset(vis,false,sizeof(vis));
|
||||
vis[0]=true;
|
||||
while(!que.empty()){
|
||||
int u=que.front().first;
|
||||
int d=que.front().second;
|
||||
que.pop();
|
||||
for(int i=0;i<(n<<1);i++){
|
||||
if(u&(1<<i)) continue;
|
||||
int v=0;
|
||||
for(int j=i;j<(i/n+1)*n;j++){
|
||||
if(u&(1<<j)) break;
|
||||
if(maze[j]==maze[i]) v|=(1<<j);
|
||||
}
|
||||
for(int j=i-1;j>=(i/n)*n;j--){
|
||||
if(u&(1<<j)) break;
|
||||
if(maze[j]==maze[i]) v|=(1<<j);
|
||||
}
|
||||
if((u|v)==(1<<(n<<1))-1) return d+1;
|
||||
for(int j=v;j;j=v&(j-1)){
|
||||
if(vis[j|u]) continue;
|
||||
vis[j|u]=true;
|
||||
que.push(make_pair(j|u,d+1));
|
||||
}
|
||||
if(i/n) continue;
|
||||
if(u&(1<<(i+n))) continue;
|
||||
v=0;
|
||||
for(int j=i;j<n;j++){
|
||||
if((u&(1<<j))|(u&(1<<(j+n)))) break;
|
||||
if(maze[j]==maze[i]) v|=1<<j;
|
||||
if(maze[j+n]==maze[i]) v|=1<<(j+n);
|
||||
}
|
||||
for(int j=i-1;j>=0;j--){
|
||||
if((u&(1<<j))|(u&(1<<(j+n)))) break;
|
||||
if(maze[j]==maze[i]) v|=1<<j;
|
||||
if(maze[j+n]==maze[i]) v|=1<<(j+n);
|
||||
}
|
||||
if((u|v)==(1<<(n<<1))-1) return d+1;
|
||||
for(int j=v;j;j=v&(j-1)){
|
||||
if(vis[j|u]) continue;
|
||||
vis[j|u]=true;
|
||||
que.push(make_pair(j|u,d+1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main(){
|
||||
int t,cas=0;
|
||||
scanf("%d",&t);
|
||||
while(t--){
|
||||
scanf("%d",&n);
|
||||
scanf("%s",maze);
|
||||
scanf("%s",maze+n);
|
||||
int ans=bfs();
|
||||
printf("Case #%d: %d\n",++cas,ans);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#define N 20
|
||||
using namespace std;
|
||||
struct trie
|
||||
{
|
||||
bool cover;
|
||||
int son[2];
|
||||
void init()
|
||||
{
|
||||
cover = false;
|
||||
memset (son,0,sizeof (son));
|
||||
}
|
||||
}tt[70000];
|
||||
vector <int> w[N];
|
||||
bool vis[N];
|
||||
int index;
|
||||
bool insert (string s)
|
||||
{
|
||||
int len = s.size (),p = 0;
|
||||
for (int i = 0;i < len;i ++)
|
||||
{
|
||||
if (tt[p].son[s[i] - '0'] == 0)
|
||||
{
|
||||
tt[index].init();
|
||||
tt[p].son[s[i] - '0'] = index ++;
|
||||
}
|
||||
p = tt[p].son[s[i] - '0'];
|
||||
}
|
||||
if (tt[p].cover) return false;
|
||||
tt[p].cover = true;
|
||||
return true;
|
||||
}
|
||||
string dfs (int u,int fa)
|
||||
{
|
||||
string s = "0";
|
||||
vector<string> q;
|
||||
q.clear ();
|
||||
int len = w[u].size ();
|
||||
for (int i = 0;i < len;i ++)
|
||||
{
|
||||
int v = w[u][i];
|
||||
if (vis[v] && v != fa) q.push_back ( dfs(v,u) );
|
||||
}
|
||||
sort (q.begin(),q.end());
|
||||
for (int i = 0;i < q.size ();i ++) s += q[i];
|
||||
s += "1";
|
||||
return s;
|
||||
}
|
||||
int main ()
|
||||
{
|
||||
int cas,c = 0,n,x,y,ans;
|
||||
scanf ("%d",&cas);
|
||||
while (cas --)
|
||||
{
|
||||
index = 1;
|
||||
ans = 0;
|
||||
tt[0].init();
|
||||
scanf ("%d",&n);
|
||||
int len = 1 << n;
|
||||
for (int i = 0;i < n;i ++) w[i].clear ();
|
||||
for (int i = 1;i < n;i ++)
|
||||
{
|
||||
scanf ("%d%d",&x,&y);
|
||||
w[x - 1].push_back(y - 1);
|
||||
w[y - 1].push_back(x - 1);
|
||||
}
|
||||
int i,j;
|
||||
for (i = 1;i < len;i ++)
|
||||
{
|
||||
memset (vis,false,sizeof (vis));
|
||||
for (j = 0;j < n;j ++)
|
||||
if (i & (1 << j)) vis[j] = true;
|
||||
int k = 0;
|
||||
for (j = 0;j < n;j ++)
|
||||
{
|
||||
if (vis[j])
|
||||
{
|
||||
string s = dfs (j,j);
|
||||
if (insert(s)) k = 1;
|
||||
if (k == 0) break;
|
||||
}
|
||||
}
|
||||
if (j == n) ans ++;
|
||||
}
|
||||
printf ("Case #%d: %d\n",++c,ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
#include<iostream>
|
||||
#include<stdio.h>
|
||||
#include<vector>
|
||||
#include<string.h>
|
||||
#include<algorithm>
|
||||
using namespace std ;
|
||||
const int N = 100010;
|
||||
const int M = 500010;
|
||||
const long long K = 100000;
|
||||
int n , m ;
|
||||
struct node {
|
||||
int u , v ;
|
||||
long long cnt ;
|
||||
bool operator < ( const node &a ) const {
|
||||
if( u != a.u ) return u < a.u ;
|
||||
else return v < a.v ;
|
||||
}
|
||||
} e[M] ;
|
||||
int tot ;
|
||||
int in[N] ;
|
||||
const int HASH = 300007;
|
||||
struct HASHMAP {
|
||||
long long key[N] , f[N] ;
|
||||
int head[HASH] , next[N] , size ;
|
||||
void init() {
|
||||
memset( head , -1 , sizeof head ) ;
|
||||
size = 0 ;
|
||||
}
|
||||
void insert( int u , int v , long long ans ) {
|
||||
long long KEY = K * u + v ;
|
||||
int c = KEY % HASH ;
|
||||
f[size] = ans ;
|
||||
key[size] = KEY ;
|
||||
next[size] = head[c] ;
|
||||
head[c] = size++;
|
||||
}
|
||||
long long find( int u , int v ) {
|
||||
long long KEY = K * u + v ;
|
||||
int c = KEY % HASH ;
|
||||
for( int i = head[c] ; ~i ; i = next[i] ) {
|
||||
if( key[i] == KEY ) return f[i] ;
|
||||
}
|
||||
return -1 ;
|
||||
}
|
||||
} mp ;
|
||||
long long find( int u , int v ) {
|
||||
int l = 0 , r = m - 1 ;
|
||||
if( l > r ) return -1 ;
|
||||
if( u > e[r].u || ( u == e[r].u && v > e[r].v ) ) return -1 ;
|
||||
if( v < e[l].v || ( v == e[l].v && u < e[l].u ) ) return -1 ;
|
||||
while( l <= r ) {
|
||||
int mid = (l+r)>>1;
|
||||
if( u < e[mid].u ) {
|
||||
r = mid - 1 ;
|
||||
}
|
||||
else if( u == e[mid].u ) {
|
||||
if( v < e[mid].v ) r = mid - 1 ;
|
||||
else if( v > e[mid].v ) l = mid + 1 ;
|
||||
else return e[mid].cnt ;
|
||||
}
|
||||
else {
|
||||
l = mid + 1 ;
|
||||
}
|
||||
}
|
||||
return -1 ;
|
||||
}
|
||||
vector< pair<int,long long> >g[N];
|
||||
void Work() {
|
||||
mp.init();
|
||||
int q ; cin >> q ;
|
||||
while( q-- ) {
|
||||
int u , v ; cin >> u >> v ;
|
||||
u-- , v-- ;
|
||||
if( u > v ) swap( u , v ) ;
|
||||
else if( u == v ) { cout << "0 1"<< endl ; continue ; }
|
||||
long long ans = find( u , v ) ;
|
||||
if( ans != -1 ) {
|
||||
cout << "1 " << ans << endl ;
|
||||
} else {
|
||||
ans = mp.find( u , v ) ;
|
||||
if( ans == -1 ) {
|
||||
ans = 0 ;
|
||||
if( in[u] > in[v] ) swap( u , v ) ;
|
||||
for( int i = 0 ; i < g[u].size() ; ++i ) {
|
||||
int mid = g[u][i].first ;
|
||||
long long cnt1 = g[u][i].second , cnt2 = find( min(mid,v) , max(mid,v) );
|
||||
if( cnt2 != -1 ) ans += cnt1 * cnt2 ;
|
||||
}
|
||||
if( u > v ) swap( u , v ) ;
|
||||
mp.insert( u , v , ans ) ;
|
||||
}
|
||||
if( ans ) cout << "2 " << ans << endl ;
|
||||
else cout << "The pair of cities are not connected or too far away." << endl ;
|
||||
}
|
||||
}
|
||||
}
|
||||
void Gao() {
|
||||
memset( in , 0 , sizeof in );
|
||||
for( int i = 0 ; i < n ; ++i ) g[i].clear();
|
||||
cin >> n >> m ;
|
||||
if( !m ) return ;
|
||||
for( int i = 0 ; i < m ; ++i ) {
|
||||
int x , y ; cin >> x >> y ;
|
||||
x-- , y-- ;
|
||||
if( x > y ) swap( x , y ) ;
|
||||
e[i].u = x , e[i].v = y , e[i].cnt = 1 ;
|
||||
in[x]++ , in[y]++ ;
|
||||
}
|
||||
sort( e , e + m ) ;
|
||||
tot = 1 ;
|
||||
for( int i = 1 ; i < m ; ++i ) {
|
||||
if( e[tot-1].u == e[i].u && e[tot-1].v == e[i].v ) {
|
||||
e[tot-1].cnt++ ;
|
||||
} else {
|
||||
e[tot++] = e[i] ;
|
||||
}
|
||||
}
|
||||
m = tot ;
|
||||
for( int i = 0 ; i < m ; ++i ) {
|
||||
g[e[i].u].push_back( make_pair( e[i].v,e[i].cnt) ) ;
|
||||
g[e[i].v].push_back( make_pair( e[i].u,e[i].cnt) ) ;
|
||||
}
|
||||
}
|
||||
int Run() {
|
||||
int _ , cas = 1 ; cin >> _ ;
|
||||
while( _-- ) {
|
||||
cout << "Case #" << cas++ << ":" << endl ;
|
||||
Gao(); Work();
|
||||
}
|
||||
return 0 ;
|
||||
}
|
||||
int main() {
|
||||
ios::sync_with_stdio(0);
|
||||
return Run();
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
#include<stdio.h>
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
int m,k;
|
||||
int iCase=0;
|
||||
scanf("%d",&T);
|
||||
while(T--)
|
||||
{
|
||||
iCase++;
|
||||
scanf("%d%d",&m,&k);
|
||||
printf("Case #%d: %.8lf\n",iCase,(double)1/(k+m*k+1));
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#define N 45
|
||||
using namespace std;
|
||||
__int64 MAX=0x7fffffffffffffff;
|
||||
__int64 a[N],sum[N],ans;
|
||||
int k,n;
|
||||
void dfs(int t,int d,__int64 num)
|
||||
{
|
||||
if(ans>num) ans=num;
|
||||
if(t==n+1||d==k) return;
|
||||
__int64 s=num;
|
||||
s &= sum[t];
|
||||
if(s>=ans)return;
|
||||
dfs(t+1,d+1,num&a[t]);
|
||||
dfs(t+1,d,num);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int cas,T=0;
|
||||
cin>>cas;
|
||||
while(cas--)
|
||||
{
|
||||
scanf("%d %d",&n,&k);
|
||||
for(int i=1;i<=n;i++)
|
||||
scanf("%I64d",&a[i]);
|
||||
sort(a+1,a+n+1);
|
||||
sum[n]=a[n];
|
||||
for(int i=n-1;i>=1;i--)
|
||||
sum[i]=a[i]&sum[i+1];
|
||||
ans=MAX;
|
||||
dfs(1,0,MAX);
|
||||
printf("Case #%d: ",++T);
|
||||
printf("%I64d\n",ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
char str[2000];
|
||||
char buf[2000];
|
||||
int t;
|
||||
scanf("%d",&t);
|
||||
for (int i=1;i<=t;i++)
|
||||
{
|
||||
printf("Case #%d: ",i);
|
||||
cin>>str;
|
||||
sscanf(str,"%*[^/]//%[^/:]",buf);
|
||||
cout<<buf<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<stdlib.h>
|
||||
int N,M,Q,U,C,L;
|
||||
int name[500110],r[500110],q;
|
||||
double click[500010],len[500110];
|
||||
double ans[500110],res[500110];
|
||||
int hash[100110];
|
||||
int cmp(const void *_p,const void *_q)
|
||||
{
|
||||
int *a=(int *)_p;
|
||||
int *b=(int *)_q;
|
||||
return click[*a]>click[*b]?-1:1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,j,k,n,t,tt,cur;
|
||||
scanf("%d",&t);
|
||||
for(tt=0;tt<t;tt++)
|
||||
{
|
||||
scanf("%d%d%d",&N,&M,&Q);
|
||||
for(i=0;i<M;i++)
|
||||
{
|
||||
scanf("%d%lf%lf",&name[i],&click[i],&len[i]);
|
||||
r[i]=i;
|
||||
}
|
||||
qsort(r,M,sizeof(r[0]),cmp);
|
||||
for(i=1;i<=N;i++)
|
||||
hash[i]=0;
|
||||
for(i=1;i<=M;i++)
|
||||
ans[i]=0.0;
|
||||
for(i=0;i<M;i++)
|
||||
{
|
||||
hash[name[r[i]]]++;
|
||||
ans[hash[name[r[i]]]]+=len[r[i]];
|
||||
}
|
||||
res[0]=0;
|
||||
for(i=1;i<=M;i++)
|
||||
{
|
||||
res[i]=ans[i]+res[i-1];
|
||||
}
|
||||
printf("Case #%d:\n",tt+1);
|
||||
for(i=0;i<Q;i++)
|
||||
{
|
||||
scanf("%d",&q);
|
||||
if(q>M)
|
||||
printf("%.0f\n",res[M]);
|
||||
else
|
||||
printf("%.0f\n",res[q]);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
#include<stdio.h>
|
||||
#include<iostream>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
int a[24];
|
||||
int b[24];
|
||||
int c[24];
|
||||
int d[24];
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
scanf("%d",&T);
|
||||
while(T--)
|
||||
{
|
||||
for(int i=0;i<24;i++)
|
||||
scanf("%d",&a[i]);
|
||||
for(int i=0;i<24;i++)
|
||||
scanf("%d",&b[i]);
|
||||
if(a[0]==0)swap(a[0],a[3]);
|
||||
if(a[1]==0)swap(a[1],a[6]);
|
||||
if(a[2]==0)swap(a[2],a[3]);
|
||||
if(a[7]==0)swap(a[7],a[6]);
|
||||
if(a[16]==0)swap(a[16],a[17]);
|
||||
if(a[21]==0)swap(a[20],a[21]);
|
||||
if(a[22]==0)swap(a[17],a[22]);
|
||||
if(a[23]==0)swap(a[20],a[23]);
|
||||
if(b[0]==0)swap(b[0],b[3]);
|
||||
if(b[1]==0)swap(b[1],b[6]);
|
||||
if(b[2]==0)swap(b[2],b[3]);
|
||||
if(b[7]==0)swap(b[7],b[6]);
|
||||
if(b[16]==0)swap(b[16],b[17]);
|
||||
if(b[21]==0)swap(b[20],b[21]);
|
||||
if(b[22]==0)swap(b[17],b[22]);
|
||||
if(b[23]==0)swap(b[20],b[23]);
|
||||
bool flag=true;
|
||||
if(a[0]!=b[0])flag=false;
|
||||
if(a[1]!=b[1])flag=false;
|
||||
if(a[2]!=b[2])flag=false;
|
||||
if(a[7]!=b[7])flag=false;
|
||||
if(a[16]!=b[16])flag=false;
|
||||
if(a[21]!=b[21])flag=false;
|
||||
if(a[22]!=b[22])flag=false;
|
||||
if(a[23]!=b[23])flag=false;
|
||||
if(flag==false)
|
||||
{
|
||||
printf("Y\n");
|
||||
continue;
|
||||
}
|
||||
for(int i=0;i<4;i++)
|
||||
{
|
||||
c[i]=a[i+3];
|
||||
d[i]=b[i+3];
|
||||
}
|
||||
for(int i=4;i<12;i++)
|
||||
{
|
||||
c[i]=a[i+4];
|
||||
d[i]=b[i+4];
|
||||
}
|
||||
for(int i=12;i<16;i++)
|
||||
{
|
||||
c[i]=a[i+5];
|
||||
d[i]=b[i+5];
|
||||
}
|
||||
int cnt1=0;
|
||||
int cnt2=0;
|
||||
int m1=0,m2=0;
|
||||
for(int i=1;i<16;i++)
|
||||
{
|
||||
if(c[i]==0)
|
||||
{
|
||||
m1=i;
|
||||
continue;
|
||||
}
|
||||
for(int j=0;j<i;j++)
|
||||
{
|
||||
if(c[i]<c[j])cnt1++;
|
||||
}
|
||||
}
|
||||
for(int i=1;i<16;i++)
|
||||
{
|
||||
if(d[i]==0)
|
||||
{
|
||||
m2=i;
|
||||
continue;
|
||||
}
|
||||
for(int j=0;j<i;j++)
|
||||
{
|
||||
if(d[i]<d[j])cnt2++;
|
||||
}
|
||||
}
|
||||
m1/=4;
|
||||
m2/=4;
|
||||
int m=abs(m1-m2);
|
||||
if(((cnt1+m)%2)!=(cnt2%2))flag=false;
|
||||
if(flag)printf("N\n");
|
||||
else printf("Y\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<string>
|
||||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#include<cmath>
|
||||
#include<map>
|
||||
#include<set>
|
||||
#include<iterator>
|
||||
using namespace std;
|
||||
typedef map<int,multiset<int> > def;
|
||||
void pop(def &a,def &b,int k)
|
||||
{
|
||||
printf("%d\n",a[k].size());
|
||||
for(multiset<int>::iterator i=a[k].begin();i!=a[k].end();i++)
|
||||
b[*i].erase(k);
|
||||
a[k].clear();
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n,m,x,y;
|
||||
while(1)
|
||||
{
|
||||
scanf("%d%d",&n,&m);
|
||||
if(n==0&&m==0)return 0;
|
||||
def h;def l;
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
scanf("%d%d",&x,&y);
|
||||
h[x].insert(y);
|
||||
l[y].insert(x);
|
||||
}
|
||||
for(int j=0;j<m;j++)
|
||||
{
|
||||
scanf("%d%d",&x,&y);
|
||||
if(x==0)
|
||||
pop(h,l,y);
|
||||
else
|
||||
pop(l,h,y);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,167 @@
|
|||
#include<stdio.h>
|
||||
#include<algorithm>
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int a[16];
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
int iCase=0;
|
||||
int now;
|
||||
int Alice,Bob;
|
||||
scanf("%d",&T);
|
||||
while(T--)
|
||||
{
|
||||
iCase++;
|
||||
printf("Case #%d: ",iCase);
|
||||
for(int i=1;i<=15;i++)
|
||||
scanf("%d",&a[i]);
|
||||
Alice=Bob=0;
|
||||
now=1;
|
||||
if(a[15]%2==0)
|
||||
{
|
||||
Alice+=a[15]/2;
|
||||
Bob+=a[15]/2;
|
||||
}
|
||||
else
|
||||
{
|
||||
Alice+=a[15]/2+1;
|
||||
Bob+=a[15]/2;
|
||||
now=2;
|
||||
}
|
||||
int tempa=a[5]+a[6];
|
||||
int tempb=a[3]+a[4];
|
||||
if(tempa==tempb)
|
||||
{
|
||||
Alice+=tempa;
|
||||
Bob+=tempb;
|
||||
}
|
||||
else if(tempa<tempb)
|
||||
{
|
||||
Alice+=tempa;
|
||||
Bob+=tempa;
|
||||
tempb-=tempa;
|
||||
if(tempb%2==0)
|
||||
{
|
||||
Bob+=tempb/2;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(now==1)
|
||||
{
|
||||
now=2;
|
||||
Bob+=tempb/2;
|
||||
}
|
||||
else
|
||||
{
|
||||
now=1;
|
||||
Bob+=tempb/2+1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Alice+=tempb;
|
||||
Bob+=tempb;
|
||||
tempa-=tempb;
|
||||
if(tempa%2==0)
|
||||
{
|
||||
Alice+=tempa/2;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(now==1)
|
||||
{
|
||||
now=2;
|
||||
Alice+=tempa/2+1;
|
||||
}
|
||||
else
|
||||
{
|
||||
now=1;
|
||||
Alice+=tempa/2;
|
||||
}
|
||||
}
|
||||
}
|
||||
int temp=a[11]+a[12]+a[13]+a[14];
|
||||
if(temp%2==0)
|
||||
{
|
||||
Alice+=0;
|
||||
Bob+=0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(now==1)
|
||||
{
|
||||
now=2;
|
||||
}
|
||||
else
|
||||
{
|
||||
now=1;
|
||||
}
|
||||
}
|
||||
tempa=a[7]+a[8];
|
||||
tempb=a[9]+a[10];
|
||||
if(tempa==tempb)
|
||||
{
|
||||
Alice+=0;
|
||||
Bob+=0;
|
||||
}
|
||||
else if(tempa<tempb)
|
||||
{
|
||||
Alice+=0;
|
||||
Bob+=0;
|
||||
tempb-=tempa;
|
||||
if(tempb%2==0)
|
||||
{
|
||||
Bob+=tempb/2;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(now==1)
|
||||
{
|
||||
now=2;
|
||||
Bob+=tempb/2+1;
|
||||
}
|
||||
else
|
||||
{
|
||||
now=1;
|
||||
Bob+=tempb/2;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tempa-=tempb;
|
||||
if(tempa%2==0)
|
||||
{
|
||||
Alice+=tempa/2;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(now==1)
|
||||
{
|
||||
now=2;
|
||||
Alice+=tempa/2;
|
||||
}
|
||||
else
|
||||
{
|
||||
now=1;
|
||||
Alice+=tempa/2+1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Alice+=2*a[1];
|
||||
Bob+=2*a[2];
|
||||
if(now==1)
|
||||
{
|
||||
if(Bob>=Alice)printf("Bob\n");
|
||||
else printf("Alice\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
if(Alice>=Bob)printf("Alice\n");
|
||||
else printf("Bob\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
#include<stdio.h>
|
||||
#include<iostream>
|
||||
#include<math.h>
|
||||
#include<string.h>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
double x1,y1,x2,y2,Lx,Ly,vD,vB,L;
|
||||
while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&Lx,&Ly,&vD,&vB,&L))
|
||||
{
|
||||
if(x1==0&&y1==0&&x2==0&&y2==0&&Lx==0&&Ly==0&&vD==0&&vB==0&&L==0)break;
|
||||
double a=vD*vD-Lx*Lx-Ly*Ly;
|
||||
double b=2*L*vD-2*Lx*(x1-x2+L*Lx/vB)-2*Ly*(y1-y2+L*Ly/vB);
|
||||
double c=L*L-(x1-x2+L*Lx/vB)*(x1-x2+L*Lx/vB)-(y1-y2+L*Ly/vB)*(y1-y2+L*Ly/vB);
|
||||
double s1=(-b-sqrt(b*b-4*a*c))/(2*a);
|
||||
double s2=(-b+sqrt(b*b-4*a*c))/(2*a);
|
||||
b=-2*L*vD-2*Lx*(x1-x2+L*Lx/vB)-2*Ly*(y1-y2+L*Ly/vB);
|
||||
double s3=(-b-sqrt(b*b-4*a*c))/(2*a);
|
||||
double s4=(-b+sqrt(b*b-4*a*c))/(2*a);
|
||||
printf("%.3lf ",L);
|
||||
if(s1<0)s1=10000000000.0;
|
||||
if(s2<0)s2=10000000000.0;
|
||||
if(s3<0)s3=10000000000.0;
|
||||
if(s4<0)s4=10000000000.0;
|
||||
printf("%.3lf\n",min(s1,min(s2,min(s3,s4)))+L/vB);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
struct Set{
|
||||
int k, num[5];
|
||||
};
|
||||
int ans_count, a1, a2, n, m;
|
||||
int a[31][23];
|
||||
Set s[23];
|
||||
__int64 ans[(1<<22) + 2];
|
||||
__int64 sum[(1<<22)+2];
|
||||
__int64 final_ans;
|
||||
int find(__int64 v)
|
||||
{
|
||||
int left = 0, right = ans_count-1, mid;
|
||||
if (ans_count == 0) return -1;
|
||||
while (left < right-1) {
|
||||
mid = left + (right - left) / 2;
|
||||
if (v <= ans[mid]) right = mid;
|
||||
else left = mid;
|
||||
}
|
||||
if (v == ans[left]) return left;
|
||||
if (v == ans[right]) return right;
|
||||
return -1;
|
||||
}
|
||||
void dfs_x1(int p, __int64 now)
|
||||
{
|
||||
int i, j, x;
|
||||
__int64 tmp;
|
||||
if (p >= a2+1) {
|
||||
ans[ans_count++] = now;
|
||||
return;
|
||||
}
|
||||
for (i = 0; i < s[p].k; i++) {
|
||||
x = s[p].num[i];
|
||||
tmp = now;
|
||||
for (j = 1; j <= n; j++) tmp ^= ((__int64)(a[j][p] * x)) << (2*(j-1));
|
||||
dfs_x1(p+1, tmp);
|
||||
}
|
||||
}
|
||||
void dfs_x2(int p, __int64 now)
|
||||
{
|
||||
int i, j, x, ret;
|
||||
__int64 tmp;
|
||||
if (p >= a2+1) {
|
||||
ret = find(now);
|
||||
if (ret != -1) {
|
||||
final_ans += sum[ret];
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (i = 0; i < s[p].k; i++) {
|
||||
x = s[p].num[i];
|
||||
tmp = now;
|
||||
for (j = 1; j <= n; j++) tmp ^= ((__int64)(a[j][p] * x)) << (2*(j-1));
|
||||
dfs_x2(p+1, tmp);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int cs, tt, i, j, ss, n_count;
|
||||
scanf("%d", &tt);
|
||||
for (cs = 1; cs <= tt; cs++) {
|
||||
scanf("%d %d", &n, &m);
|
||||
for (i = 1; i <= n; i++) {
|
||||
for (j = 1; j <= m; j++) {
|
||||
scanf("%d", &a[i][j]);
|
||||
}
|
||||
}
|
||||
for (i = 1; i <= m; i++) {
|
||||
scanf("%d", &s[i].k);
|
||||
n_count = 0;
|
||||
ss = 0;
|
||||
for (j = 0; j < s[i].k; j++) {
|
||||
scanf("%d", &s[i].num[n_count]);
|
||||
if (ss & (1<<s[i].num[n_count])) continue;
|
||||
n_count++;
|
||||
}
|
||||
s[i].k = n_count;
|
||||
}
|
||||
ans_count = 0;
|
||||
a1 = 1;
|
||||
a2 = (m/2);
|
||||
dfs_x1(a1, 0LL);
|
||||
sort(ans, ans+ans_count);
|
||||
j = 0;
|
||||
sum[j] = 1;
|
||||
for (i = 1; i < ans_count; i++) {
|
||||
if (ans[j] == ans[i]) sum[j]++;
|
||||
else {
|
||||
ans[++j] = ans[i];
|
||||
sum[j] = 1;
|
||||
}
|
||||
}
|
||||
ans_count = j+1;
|
||||
final_ans = 0LL;
|
||||
a1 = a2 + 1;
|
||||
a2 = m;
|
||||
dfs_x2(a1, 0LL);
|
||||
printf("%I64d\n", final_ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<string>
|
||||
#include<iostream>
|
||||
#include<cmath>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
struct node
|
||||
{
|
||||
int x,y;
|
||||
}nd[16];
|
||||
long long dp[1<<16][16];
|
||||
int n,m,gp[6][6],pos,gd[6][6],mp[6][6];
|
||||
int gcd(int x,int y)
|
||||
{
|
||||
if (y == 0)
|
||||
return x;
|
||||
return gcd(y, x % y);
|
||||
}
|
||||
bool ok(int x,int y,int sta)
|
||||
{
|
||||
int x1=nd[x].x,x2=nd[y].x,y1=nd[x].y,y2=nd[y].y;
|
||||
int dx=x2-x1;
|
||||
int dy=y2-y1;
|
||||
int c=gd[abs(dx)][abs(dy)];
|
||||
dx=dx/c;dy=dy/c;
|
||||
for(int i=0;i<=5;i++)
|
||||
{
|
||||
x1+=dx;y1+=dy;
|
||||
if(gp[x1][y1]==1)
|
||||
return false;
|
||||
else if(x1==x2&&y1==y2)
|
||||
return true;
|
||||
else if(gp[x1][y1]==0&&(sta&(1<<(mp[x1][y1])))==0)
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
long long DP(int st,int k)
|
||||
{
|
||||
if(dp[st][k]!=-1)return dp[st][k];
|
||||
if(st==(1<<k)) dp[st][k]=1;
|
||||
else
|
||||
{
|
||||
dp[st][k]=0;
|
||||
for(int j=0;j<pos;j++)
|
||||
{
|
||||
if(((1<<j)&st)==0||j==k)continue;
|
||||
if(ok(j,k,st)==0)continue;
|
||||
dp[st][k]+=DP(st-(1<<k),j);
|
||||
}
|
||||
}
|
||||
return dp[st][k];
|
||||
}
|
||||
void work()
|
||||
{
|
||||
long long ans=0;
|
||||
memset(dp,-1,sizeof(dp));
|
||||
for(int i=0;i<pos;i++)
|
||||
DP((1<<pos)-1,i);
|
||||
for(int i=0;i<pos;i++)
|
||||
{
|
||||
ans+=dp[(1<<pos)-1][i];
|
||||
}
|
||||
cout<<ans<<'\n';
|
||||
}
|
||||
void init()
|
||||
{
|
||||
for(int i=0;i<6;i++)
|
||||
for(int j=0;j<6;j++)
|
||||
gd[i][j]=gcd(i,j);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
while(scanf("%d%d",&n,&m)!=EOF)
|
||||
{
|
||||
pos=0;
|
||||
for(int i=0;i<n;i++)
|
||||
for(int j=0;j<m;j++)
|
||||
{
|
||||
scanf("%d",&gp[i][j]);
|
||||
if(gp[i][j]==0)
|
||||
{
|
||||
nd[pos].x=i;
|
||||
nd[pos++].y=j;
|
||||
mp[i][j]=pos-1;
|
||||
}
|
||||
}
|
||||
work();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,120 @@
|
|||
#include <queue>
|
||||
#include <stack>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <stdlib.h>
|
||||
#include <iostream>
|
||||
#include <limits.h>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <algorithm>
|
||||
#define LL __int64
|
||||
using namespace std;
|
||||
const int N=100010;
|
||||
struct Node
|
||||
{
|
||||
int L,R;
|
||||
LL len;
|
||||
LL sum;
|
||||
} t[N*4];
|
||||
LL data[N];
|
||||
int n,q;
|
||||
void up(int fa){
|
||||
t[fa].sum=t[fa<<1].sum+t[fa<<1|1].sum;
|
||||
}
|
||||
void down(int l,int r,int fa)
|
||||
{
|
||||
if(t[fa].L==t[fa].R)
|
||||
{
|
||||
t[fa].sum=(LL)(sqrt(t[fa].sum));
|
||||
return;
|
||||
}
|
||||
int ls=fa<<1;
|
||||
int rs=fa<<1|1;
|
||||
int mid=(t[fa].L+t[fa].R)/2;
|
||||
down(l,mid,ls);
|
||||
down(mid+1,r,rs);
|
||||
up(fa);
|
||||
}
|
||||
void built(int l,int r,int fa)
|
||||
{
|
||||
t[fa].L=l;
|
||||
t[fa].R=r;
|
||||
t[fa].len=r-l+1;
|
||||
t[fa].sum=0LL;
|
||||
if(l==r)
|
||||
{
|
||||
t[fa].sum=data[l];
|
||||
return;
|
||||
}
|
||||
int mid=(l+r)/2;
|
||||
built(l,mid,fa<<1);
|
||||
built(mid+1,r,fa<<1|1);
|
||||
up(fa);
|
||||
}
|
||||
void update(int l,int r,int fa)
|
||||
{
|
||||
int ls=fa<<1;
|
||||
int rs=fa<<1|1;
|
||||
int mid=(t[fa].L+t[fa].R)/2;
|
||||
if(t[fa].L==l&&t[fa].R==r)
|
||||
{
|
||||
if(t[fa].sum==t[fa].len)
|
||||
return ;
|
||||
down(l,r,fa);
|
||||
return ;
|
||||
}
|
||||
if(r<=mid)
|
||||
update(l,r,ls);
|
||||
else if(l>mid)
|
||||
update(l,r,rs);
|
||||
else
|
||||
{
|
||||
update(l,mid,ls);
|
||||
update(mid+1,r,rs);
|
||||
}
|
||||
up(fa);
|
||||
}
|
||||
LL query(int l,int r,int fa)
|
||||
{
|
||||
int ls=fa<<1;
|
||||
int rs=fa<<1|1;
|
||||
int mid=(t[fa].L+t[fa].R)/2;
|
||||
if(t[fa].L>=l&&t[fa].R<=r)
|
||||
return t[fa].sum;
|
||||
if(r<=mid)
|
||||
return query(l,r,ls);
|
||||
else if(l>mid)
|
||||
return query(l,r,rs);
|
||||
else
|
||||
return query(l,mid,ls)+query(mid+1,r,rs);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t=1;
|
||||
while(scanf("%d",&n)!=EOF)
|
||||
{
|
||||
for(int i=1; i<=n; i++)
|
||||
scanf("%I64d",&data[i]);
|
||||
built(1,n,1);
|
||||
int a,b,c;
|
||||
printf("Case #%d:\n",t++);
|
||||
cin>>q;
|
||||
while(q--)
|
||||
{
|
||||
scanf("%d%d%d",&a,&b,&c);
|
||||
if(b>c)swap(b,c);
|
||||
if(a)
|
||||
cout<<query(b,c,1)<<endl;
|
||||
else
|
||||
update(b,c,1);
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<string>
|
||||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#include<cmath>
|
||||
#include<map>
|
||||
#define LL long long
|
||||
using namespace std;
|
||||
LL m;int n;
|
||||
typedef map <LL,LL> mp;
|
||||
mp dp[45];
|
||||
LL gcd(LL a,LL b)
|
||||
{
|
||||
return b==0?a:gcd(b,a%b);
|
||||
}
|
||||
LL lcm(LL a,LL b)
|
||||
{
|
||||
return a*b/gcd(a,b);
|
||||
}
|
||||
void DP()
|
||||
{
|
||||
dp[1][1]=1;
|
||||
for(int i=2;i<=40;i++)
|
||||
{
|
||||
dp[i]=dp[i-1];
|
||||
dp[i][i]+=1;
|
||||
for(mp::iterator j=dp[i-1].begin();j!=dp[i-1].end();j++)
|
||||
{
|
||||
LL lc=lcm(i,j->first);
|
||||
dp[i][lc]+=j->second;
|
||||
}
|
||||
}
|
||||
}
|
||||
void ANS()
|
||||
{
|
||||
LL ans=0;
|
||||
for(mp::iterator i=dp[n].begin();i!=dp[n].end();i++)
|
||||
if(i->first>=m)ans+=i->second;
|
||||
cout<<ans<<'\n';
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T,t;
|
||||
DP();
|
||||
scanf("%d",&T);
|
||||
for(t=1;t<=T;t++)
|
||||
{
|
||||
cin>>n>>m;
|
||||
printf("Case #%d: ",t);
|
||||
ANS();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<map>
|
||||
#include<cstring>
|
||||
#include<cmath>
|
||||
#include<vector>
|
||||
#include<queue>
|
||||
#include<algorithm>
|
||||
#include<set>
|
||||
#define inf (1ull<<63)-1
|
||||
#define N 130
|
||||
#define maxn 100005
|
||||
#define Min(a,b) ((a)<(b)?(a):(b))
|
||||
#define Max(a,b) ((a)>(b)?(a):(b))
|
||||
#define pb(a) push_back(a)
|
||||
#define mem(a,b) memset(a,b,sizeof(a))
|
||||
#define eps 1e-9
|
||||
#define zero(a) fabs(a)<eps
|
||||
#define ULL unsigned long long
|
||||
#define lson (step<<1)
|
||||
#define rson (step<<1|1)
|
||||
#define MOD 1000000007
|
||||
#define mp(a,b) make_pair(a,b)
|
||||
using namespace std;
|
||||
const ULL hash1=26;
|
||||
struct HASH{
|
||||
ULL h1;
|
||||
HASH(){}
|
||||
HASH(ULL _h1):h1(_h1){}
|
||||
}h[N][N];
|
||||
int wa[maxn],wb[maxn],wv[maxn],Ws[maxn];
|
||||
int cmp(int *r,int a,int b,int l)
|
||||
{return r[a]==r[b]&&r[a+l]==r[b+l];}
|
||||
void da(const int *r,int *sa,int n,int m){
|
||||
int i,j,p,*x=wa,*y=wb,*t;
|
||||
for(i=0;i<m;i++) Ws[i]=0;
|
||||
for(i=0;i<n;i++) Ws[x[i]=r[i]]++;
|
||||
for(i=1;i<m;i++) Ws[i]+=Ws[i-1];
|
||||
for(i=n-1;i>=0;i--) sa[--Ws[x[i]]]=i;
|
||||
for(j=1,p=1;p<n;j*=2,m=p){
|
||||
for(p=0,i=n-j;i<n;i++) y[p++]=i;
|
||||
for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
|
||||
for(i=0;i<n;i++) wv[i]=x[y[i]];
|
||||
for(i=0;i<m;i++) Ws[i]=0;
|
||||
for(i=0;i<n;i++) Ws[wv[i]]++;
|
||||
for(i=1;i<m;i++) Ws[i]+=Ws[i-1];
|
||||
for(i=n-1;i>=0;i--) sa[--Ws[wv[i]]]=y[i];
|
||||
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
|
||||
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
|
||||
}
|
||||
return;
|
||||
}
|
||||
int sa[maxn],Rank[maxn],height[maxn];
|
||||
void calheight(const int *r,int *sa,int n){
|
||||
int i,j,k=0;
|
||||
for(i=1;i<=n;i++) Rank[sa[i]]=i;
|
||||
for(i=0;i<n;height[Rank[i++]]=k)
|
||||
for(k?k--:0,j=sa[Rank[i]-1];r[i+k]==r[j+k];k++);
|
||||
return;
|
||||
}
|
||||
int tot,r[maxn],cnt;
|
||||
char str[N][N];
|
||||
int main(){
|
||||
int t,cas=0,n,m;
|
||||
scanf("%d",&t);
|
||||
while(t--){
|
||||
scanf("%d%d",&n,&m);
|
||||
for(int i=0;i<n;i++) scanf("%s",str[i]);
|
||||
mem(h,0);
|
||||
ULL ans=0;
|
||||
for(int w=1;w<=m;w++){
|
||||
tot=1;cnt=0;
|
||||
map<ULL,int> map;
|
||||
map.clear();
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=0;j+w-1<m;j++){
|
||||
h[i][j].h1=h[i][j].h1*hash1+str[i][j+w-1]-'A';
|
||||
if(map.find(h[i][j].h1)==map.end())
|
||||
map.insert(mp(h[i][j].h1,tot++));
|
||||
}
|
||||
}
|
||||
for(int j=0;j+w-1<m;j++){
|
||||
for(int i=0;i<n;i++){
|
||||
r[cnt++]=map[h[i][j].h1];
|
||||
}
|
||||
r[cnt++]=tot++;
|
||||
}
|
||||
r[cnt]=0;
|
||||
da(r,sa,cnt+1,tot);
|
||||
calheight(r,sa,cnt);
|
||||
ULL tmp=(n*(n+1)/2)*(m-w+1);
|
||||
for(int i=1;i<=cnt;i++){tmp-=height[i];}
|
||||
ans+=tmp;
|
||||
}
|
||||
printf("Case #%d: %I64u\n",++cas,ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,139 @@
|
|||
#include<cstdio>
|
||||
#include<cstdlib>
|
||||
#include<cstring>
|
||||
#include<cmath>
|
||||
#include<map>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
typedef long long ll;
|
||||
const int maxn = 105;
|
||||
const int forever = -1;
|
||||
const int never = -2;
|
||||
struct point
|
||||
{
|
||||
ll x, y;
|
||||
} p[maxn], v[maxn];
|
||||
map <ll, ll> Map;
|
||||
ll cti[maxn][maxn];
|
||||
void initial(int n)
|
||||
{
|
||||
int i, j, t;
|
||||
for(i=0; i<n; i++)
|
||||
for(j=i+1; j<n; j++)
|
||||
{
|
||||
if(p[i].x==p[j].x && p[i].y==p[j].y && v[i].x==v[j].x && v[i].y==v[j].y)
|
||||
cti[i][j]=cti[j][i]=forever;
|
||||
else
|
||||
{
|
||||
cti[i][j]=cti[j][i]=never;
|
||||
if(v[i].x != v[j].x)
|
||||
{
|
||||
if((p[i].x-p[j].x)%(v[j].x-v[i].x) != 0)continue;
|
||||
t=(p[i].x-p[j].x)/(v[j].x-v[i].x);
|
||||
if(t<0 || (p[i].y+v[i].y*t != p[j].y+v[j].y*t))continue;
|
||||
}
|
||||
else if(v[i].y != v[j].y)
|
||||
{
|
||||
if((p[i].y-p[j].y)%(v[j].y-v[i].y) != 0)continue;
|
||||
t=(p[i].y-p[j].y)/(v[j].y-v[i].y);
|
||||
if(t<0 || (p[i].x+v[i].x*t != p[j].x+v[j].x*t))continue;
|
||||
}
|
||||
else continue;
|
||||
cti[i][j]=cti[j][i]=t;
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i, j, k, n, tmp, cnt, res, ans;
|
||||
ll a, b, c, t;
|
||||
while(~scanf("%d", &n) && n)
|
||||
{
|
||||
for(i = 0; i < n; i++)scanf("%I64d%I64d%I64d%I64d", &p[i].x, &p[i].y, &v[i].x, &v[i].y);
|
||||
if(n == 1 || n == 2)
|
||||
{
|
||||
printf("%d\n", n);
|
||||
continue;
|
||||
}
|
||||
initial(n);
|
||||
ans = 0;
|
||||
for(i = 0; i < n; i++)
|
||||
for(j = i+1; j < n; j++)
|
||||
{
|
||||
Map.clear();
|
||||
cnt = 0;
|
||||
res = 0;
|
||||
for(k = 0; k < n; k++)
|
||||
{
|
||||
if(k==i || k==j)continue;
|
||||
a = (v[i].x-v[j].x)*(v[i].y-v[k].y) - (v[i].x-v[k].x)*(v[i].y-v[j].y);
|
||||
b = (p[i].x-p[j].x)*(v[i].y-v[k].y) + (v[i].x-v[j].x)*(p[i].y-p[k].y)
|
||||
- (p[i].y-p[j].y)*(v[i].x-v[k].x) - (v[i].y-v[j].y)*(p[i].x-p[k].x);
|
||||
c = (p[i].x-p[j].x)*(p[i].y-p[k].y) - (p[i].y-p[j].y)*(p[i].x-p[k].x);
|
||||
if(a==0 && b==0 && c==0)cnt++;
|
||||
else if(a==0)
|
||||
{
|
||||
if(c==0 || b==0)continue;
|
||||
else if(c%b==0)
|
||||
{
|
||||
t=-c/b;
|
||||
if(t>0 && t!=cti[i][j])
|
||||
{
|
||||
tmp = ++Map[t];
|
||||
res = max(tmp, res);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ll d = b*b-4*a*c;
|
||||
ll t1, t2;
|
||||
if(d<0) continue;
|
||||
else
|
||||
{
|
||||
ll sqd = sqrt(d+0.0);
|
||||
if(sqd*sqd == d)
|
||||
{
|
||||
t1= -b+sqd;
|
||||
if(t1%(2*a)==0)
|
||||
{
|
||||
t1/=(2*a);
|
||||
if(t1>0 && t1!=cti[i][j])
|
||||
{
|
||||
tmp = ++Map[t1];
|
||||
res = max(tmp, res);
|
||||
}
|
||||
}
|
||||
if(sqd!=0)
|
||||
{
|
||||
t2= -b-sqd;
|
||||
if(t2%(2*a)==0)
|
||||
{
|
||||
t2/=(2*a);
|
||||
if(t2>0 && t2!=cti[i][j])
|
||||
{
|
||||
tmp = ++Map[t2];
|
||||
res = max(tmp, res);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ans = max(res+2+cnt, ans);
|
||||
if(cti[i][j]!=never && cti[i][j]!=0)
|
||||
{
|
||||
res=0;
|
||||
for(k=0; k<n; k++)
|
||||
{
|
||||
if(k==i || k==j)continue;
|
||||
if(cti[i][j] == cti[i][k])res++;
|
||||
}
|
||||
ans=max(res+2, ans);
|
||||
}
|
||||
}
|
||||
printf("%d\n", ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
#include <iostream>
|
||||
#include <string.h>
|
||||
using namespace std;
|
||||
const int MAX = 20000 + 100;
|
||||
int C[MAX];
|
||||
int N;
|
||||
struct Node
|
||||
{
|
||||
int l;
|
||||
int r;
|
||||
};
|
||||
int LowBit(int x)
|
||||
{
|
||||
return x & (-x);
|
||||
}
|
||||
void Update(int num,int key)
|
||||
{
|
||||
while(num <= N)
|
||||
{
|
||||
C[num] += key;
|
||||
num += LowBit(num);
|
||||
}
|
||||
}
|
||||
int CalSum(int num)
|
||||
{
|
||||
int sum = 0;
|
||||
while(num > 0)
|
||||
{
|
||||
sum += C[num];
|
||||
num -= LowBit(num);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
cin>>T;
|
||||
int Case = 0;
|
||||
while(T--)
|
||||
{
|
||||
int Q;
|
||||
int CD;
|
||||
int time = 0;
|
||||
int used_time[MAX] = {0};
|
||||
int porected[MAX] = {0};
|
||||
Node attack[MAX] = {0};
|
||||
memset(C,0,sizeof(C));
|
||||
Case++;
|
||||
cout<<"Case "<<Case<<":"<<endl;
|
||||
scanf("%d%d%d",&N,&Q,&CD);
|
||||
int i;
|
||||
for(i=0;i<Q;i++)
|
||||
{
|
||||
char str[20];
|
||||
scanf("%s",str);
|
||||
if(str[0] == 'A')
|
||||
{
|
||||
scanf("%d%d",&attack[time].l,&attack[time].r);
|
||||
Update(attack[time].l,1);
|
||||
Update(attack[time].r+1,-1);
|
||||
time++;
|
||||
}
|
||||
else
|
||||
{
|
||||
int num;
|
||||
scanf("%d",&num);
|
||||
int i;
|
||||
for(i = used_time[num] ; i< time ;i++)
|
||||
{
|
||||
if(attack[i].l <=num && num <= attack[i].r )
|
||||
{
|
||||
porected[num]++;
|
||||
used_time[num] = i + CD;
|
||||
i = i + CD - 1;
|
||||
}
|
||||
}
|
||||
printf("%d\n",CalSum(num)-porected[num]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,166 @@
|
|||
#include<iostream>
|
||||
#include<cstring>
|
||||
using namespace std;
|
||||
char translate[16] = "3456789TJQKA2BR";
|
||||
char tot[16] = "444444444444411";
|
||||
int a[3][16];
|
||||
int b[15];
|
||||
int limit[5] = {0, 5, 3, 0, 0};
|
||||
int big[5][16];
|
||||
bool rocket;
|
||||
int attach[16][3];
|
||||
int get(char c){
|
||||
for (int i = 0; i < 15; ++i)
|
||||
if (translate[i] == c) return i;
|
||||
}
|
||||
void init(int i, string s){
|
||||
memset(a[i], 0, sizeof(a[i]));
|
||||
for (int j = 0; j < s.size(); ++j)
|
||||
++a[i][get(s[j])];
|
||||
}
|
||||
void init(int index){
|
||||
if (!index){
|
||||
memset(big, 255, sizeof(big));
|
||||
memset(attach, 255, sizeof(attach));
|
||||
rocket = false;
|
||||
return;
|
||||
}
|
||||
if (a[index][14]&&a[index][13]) rocket = true;
|
||||
int b[3] = {0, 0, 0};
|
||||
for (int i = 0; i < 15; ++i)
|
||||
for (int k = 1; k < 3; ++k)
|
||||
if (a[index][i] >= k) ++b[k];
|
||||
for (int i = 0; i < 15; ++i)
|
||||
for (int cardnumber = 1; cardnumber < 5; ++cardnumber)
|
||||
for (int len = 1; i + len <= 15; ++len){
|
||||
if ((len > 1)&&(i + len > 12)) break;
|
||||
int ok = true;
|
||||
if (a[index][i + len - 1] >= cardnumber){
|
||||
big[cardnumber][len] = max(big[cardnumber][len], i);
|
||||
if ((cardnumber == 3)&&(b[1] >= 2 * len))
|
||||
attach[len][1] = max(attach[len][1], i);
|
||||
if ((cardnumber == 3)&&(b[2] >= 2 * len))
|
||||
attach[len][2] = max(attach[len][2], i);
|
||||
}
|
||||
else break;
|
||||
}
|
||||
}
|
||||
bool dfs(int remain, int level, bool fail);
|
||||
bool dfs(int cardnumber, int remain, int i, int len, int index, int begin, int fail){
|
||||
if (index == len){
|
||||
if ((len >= 2)&&(b[len - 1] + b[len - 2] == 27)) return false;
|
||||
return dfs(remain - (3 + cardnumber) * len, 4000 + i + len, fail);
|
||||
}
|
||||
for (int j = begin; j < 15; ++j){
|
||||
if (j == i) j = i + len;
|
||||
if (a[0][j] >= cardnumber){
|
||||
a[0][j] -= cardnumber;
|
||||
b[index] = j;
|
||||
if (dfs(cardnumber, remain, i, len, index + 1, j + 1,fail)) return true;
|
||||
a[0][j] += cardnumber;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool dfs(int remain, int level, bool fail){
|
||||
if (remain == 0) return true;
|
||||
if (level == 0){
|
||||
if (a[0][14]&&a[0][13]){
|
||||
--a[0][14];
|
||||
--a[0][13];
|
||||
if (dfs(remain - 2, 1000, fail)) return true;
|
||||
++a[0][14];
|
||||
++a[0][13];
|
||||
}
|
||||
return dfs(remain, 1000, fail);
|
||||
}
|
||||
if (level / 1000 == 1){
|
||||
for (int i = level % 1000; i < 15; ++i)
|
||||
if (a[0][i] == 4){
|
||||
if (fail&&(i < big[4][1])) continue;
|
||||
a[0][i] -= 4;
|
||||
if (dfs(remain - 4, 1000 + i + 1, fail||(i < big[4][1]))) return true;
|
||||
a[0][i] += 4;
|
||||
}
|
||||
return dfs(remain, 2000, fail);
|
||||
}
|
||||
if ((big[4][1] > -1)&&(fail)) return false;
|
||||
if (level / 1000 == 2){
|
||||
for (int i = level % 1000; i < 15; ++i)
|
||||
if (a[0][i] == 4){
|
||||
a[0][i] -= 4;
|
||||
for (int p = 1; p <= 2; ++p)
|
||||
for (int j = 0; j < 13; ++j)
|
||||
if (a[0][j] >= p)
|
||||
for (int k = j + 1; k < 15; ++k)
|
||||
if (a[0][k] >= p){
|
||||
a[0][j] -= p;
|
||||
a[0][k] -= p;
|
||||
if (dfs(remain - 2 * p - 4, 2000 + i + 1, fail|(big[4][1] >= 0))) return true;
|
||||
a[0][k] += p;
|
||||
a[0][j] += p;
|
||||
}
|
||||
a[0][i] += 4;
|
||||
}
|
||||
return dfs(remain, 3000, fail);
|
||||
}
|
||||
if (level / 1000 == 3){
|
||||
for (int i = level % 100; i < 15; ++i)
|
||||
for (int cardnumber = 1; cardnumber < 4; ++cardnumber)
|
||||
for (int len = 1; len + i <= 16; ++len){
|
||||
if ((!((len > 1)&&(i + len > 12)))&&(a[0][i + len - 1] >= cardnumber)){
|
||||
a[0][i + len - 1] -= cardnumber;
|
||||
if (((!(big[cardnumber][len] > i))||(!fail))&&((len >= limit[cardnumber])||(len == 1)))
|
||||
if (dfs(remain - cardnumber * len, 3000 + i, fail||(big[cardnumber][len] > i)))
|
||||
return true;
|
||||
}else{
|
||||
for (int j = 0; j < len - 1; ++j)
|
||||
a[0][i + j] += cardnumber;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return dfs(remain, 4000, fail);
|
||||
}
|
||||
for (int i = level % 1000; i < 15; ++i)
|
||||
for (int cardnumber = 1; cardnumber < 3; ++cardnumber)
|
||||
for (int len = 1; len + i <= 16; ++len){
|
||||
if ((!((len > 1)&&(i + len > 12)))&&(a[0][i + len - 1] >= 3)){
|
||||
a[0][i + len - 1] -= 3;
|
||||
if ((!(attach[len][cardnumber] > i))||(!fail)){
|
||||
if (dfs(cardnumber, remain, i, len, 0, 0, (attach[len][cardnumber] > i)||fail)) return true;
|
||||
}
|
||||
}else{
|
||||
for (int j = 0; j < len - 1; ++j)
|
||||
a[0][i + j] += 3;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
int main(){
|
||||
int t, _t;
|
||||
for (scanf("%d ", &_t), t = 0; t < _t; ++t){
|
||||
for (int i = 0; i < 3; ++i){
|
||||
string s;
|
||||
cin>>s;
|
||||
init(i, s);
|
||||
}
|
||||
for (int i = 0; i < 3; ++i)
|
||||
init(i);
|
||||
for (int i = 0; i < 15; ++i){
|
||||
if (a[0][i] + a[1][i] + a[2][i] != tot[i] - '0') a[0][i] = a[0][i] / (a[0][i] - a[0][i]);
|
||||
}
|
||||
if (rocket) big[4][1] = 100;
|
||||
if (big[4][1] >= 0){
|
||||
for( int i = 1; i <= 3; ++i)
|
||||
for (int j = 1; j < 16; ++j)
|
||||
big[i][j] = 100;
|
||||
for (int i = 1; i <= 2; ++i)
|
||||
for (int j = 0; j < 16; ++j)
|
||||
attach[j][i] = 100;
|
||||
}
|
||||
if (dfs(20, 0, 0)){
|
||||
printf("Case %d: Yes\n", t + 1);
|
||||
}else printf("Case %d: No\n", t + 1);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
double ch[200];
|
||||
const double PI = 4 * atan( double( 1 ) );
|
||||
//const double PI = 3.1415926;
|
||||
int num;
|
||||
double is_triangle( double a, double b, double c )
|
||||
{
|
||||
if( c > a + b )
|
||||
return 100;
|
||||
else if( c < fabs( a - b ) )
|
||||
return -100;
|
||||
else
|
||||
return acos( ( a * a + b * b - c * c ) / ( 2 * a * b ) );
|
||||
}
|
||||
double Acos( double a, double b, double c )
|
||||
{
|
||||
return is_triangle( a, b, c );
|
||||
}
|
||||
double total_angle( double mid )
|
||||
{
|
||||
double ans = 0;
|
||||
for( int i = 2; i <= num; ++i )
|
||||
{
|
||||
double t = Acos( ch[i], ch[i-1], mid );
|
||||
if( fabs( t ) == 100 )
|
||||
return t;
|
||||
else
|
||||
ans += Acos( ch[i], ch[i-1], mid );
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
double Bsearch( double ss, double ee )
|
||||
{
|
||||
double left = ss, right = ee, mid, sum_angle;
|
||||
while( right - left >= 1e-9 )
|
||||
{
|
||||
mid = ( left + right ) / 2.0;
|
||||
double t = total_angle( mid );
|
||||
if( fabs( t ) == 100 )
|
||||
{
|
||||
if( t > 0 )
|
||||
right = mid;
|
||||
else
|
||||
left = mid;
|
||||
}
|
||||
else
|
||||
{
|
||||
sum_angle = t + Acos( ch[1] , ch[num], mid );
|
||||
if( sum_angle - 2 * PI > 1e-9 )
|
||||
right = mid;
|
||||
else if( sum_angle - 2 * PI < -1e-9 )
|
||||
left = mid;
|
||||
else
|
||||
return mid;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int main( )
|
||||
{
|
||||
int i,j,k,T;
|
||||
cin>>T;
|
||||
for(i = 1; i <= T; ++i )
|
||||
{
|
||||
cin>>num;
|
||||
for(j = 1; j <= num; ++j )
|
||||
cin>>ch[j];
|
||||
double ans = Bsearch( fabs( ch[num] - ch[1] ), ch[1] + ch[num] );
|
||||
printf( "Case %d: ", i );
|
||||
if( ans == 0 )
|
||||
puts( "impossible" );
|
||||
else
|
||||
printf( "%.3lf\n", ans );
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
#include<iostream>
|
||||
#include<cstdlib>
|
||||
#include<stdio.h>
|
||||
#include<memory.h>
|
||||
using namespace std;
|
||||
int map[110][110];
|
||||
int visit[110][110];
|
||||
int n;
|
||||
int dist[110][110];
|
||||
void floyd() {
|
||||
int i,j,k;
|
||||
for(i=1;i<=n;i++)
|
||||
for(j=1;j<=n;j++)
|
||||
for(k=1;k<=n;k++)
|
||||
{
|
||||
if(j!=i&&i!=k&&dist[j][k]>=dist[j][i]+dist[i][k])
|
||||
{
|
||||
visit[j][k]=1;
|
||||
dist[j][k]=dist[j][i]+dist[i][k];
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
int count=1;
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
scanf("%d",&n);
|
||||
memset(map,0,sizeof(map));
|
||||
memset(dist,0,sizeof(dist));
|
||||
for(int i=1;i<=n;i++)
|
||||
for(int j=1;j<=n;j++)
|
||||
{scanf("%d",&dist[i][j]);
|
||||
map[i][j]=dist[i][j];}
|
||||
memset(visit,0,sizeof(visit));
|
||||
floyd();
|
||||
int ans=0;
|
||||
bool flag=true;
|
||||
for(int i=1;i<=n;i++)
|
||||
for(int j=1;j<=n;j++)
|
||||
{
|
||||
if(visit[i][j]==1&&dist[i][j]==map[i][j])
|
||||
{
|
||||
ans++;
|
||||
}
|
||||
if(dist[i][j]<map[i][j])
|
||||
{
|
||||
flag=false;
|
||||
}
|
||||
}
|
||||
int m=n*(n-1)-ans;
|
||||
if(!flag||m<n-1)
|
||||
printf("Case %d: impossible\n",count++);
|
||||
else
|
||||
printf("Case %d: %d\n",count++,m);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<algorithm>
|
||||
#include<iostream>
|
||||
#include<math.h>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
const int MAXN=10010;
|
||||
const double eps=1e-9;
|
||||
double k[MAXN],e[MAXN];
|
||||
double A[MAXN],B[MAXN],C[MAXN];
|
||||
vector<int>vec[MAXN];
|
||||
bool dfs(int t,int pre)
|
||||
{
|
||||
int m=vec[t].size();
|
||||
A[t]=k[t];
|
||||
B[t]=(1-k[t]-e[t])/m;
|
||||
C[t]=1-k[t]-e[t];
|
||||
double tmp=0;
|
||||
for(int i=0;i<m;i++)
|
||||
{
|
||||
int v=vec[t][i];
|
||||
if(v==pre)continue;
|
||||
if(!dfs(v,t))return false;
|
||||
A[t]+=(1-k[t]-e[t])/m*A[v];
|
||||
C[t]+=(1-k[t]-e[t])/m*C[v];
|
||||
tmp+=(1-k[t]-e[t])/m*B[v];
|
||||
}
|
||||
if(fabs(tmp-1)<eps)return false;
|
||||
A[t]/=(1-tmp);
|
||||
B[t]/=(1-tmp);
|
||||
C[t]/=(1-tmp);
|
||||
return true;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
int n;
|
||||
int u,v;
|
||||
int iCase=0;
|
||||
scanf("%d",&T);
|
||||
while(T--)
|
||||
{
|
||||
iCase++;
|
||||
scanf("%d",&n);
|
||||
for(int i=1;i<=n;i++)vec[i].clear();
|
||||
for(int i=1;i<n;i++)
|
||||
{
|
||||
scanf("%d%d",&u,&v);
|
||||
vec[u].push_back(v);
|
||||
vec[v].push_back(u);
|
||||
}
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
scanf("%lf%lf",&k[i],&e[i]);
|
||||
k[i]/=100;
|
||||
e[i]/=100;
|
||||
}
|
||||
printf("Case %d: ",iCase);
|
||||
if(dfs(1,-1)&&fabs(1-A[1])>eps)
|
||||
{
|
||||
printf("%.6lf\n",C[1]/(1-A[1]));
|
||||
}
|
||||
else printf("impossible\n");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
typedef struct _point
|
||||
{
|
||||
double x,y,v;
|
||||
double HP;
|
||||
}point;
|
||||
point Poin[ 1005 ];
|
||||
point Tudo[ 1005 ];
|
||||
int search( double X, int H )
|
||||
{
|
||||
int m,l = 1,h = H;
|
||||
while ( l < h ) {
|
||||
m = (l+h+1)>>1;
|
||||
if ( Poin[ m ].x > X )
|
||||
h = m-1;
|
||||
else l = m;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T,N,M,W;
|
||||
scanf("%d",&T);
|
||||
for ( int t = 1 ; t <= T ; ++ t ) {
|
||||
scanf("%d%d%d",&N,&M,&W);
|
||||
for ( int i = 1 ; i <= N ; ++ i )
|
||||
scanf("%lf%lf",&Poin[ i ].x,&Poin[ i ].y);
|
||||
for ( int i = 1 ; i <= M ; ++ i )
|
||||
scanf("%lf%lf%d",&Tudo[ i ].x,&Tudo[ i ].v,&W);
|
||||
for ( int i = 1 ; i <= M ; ++ i ) {
|
||||
int s = search( Tudo[ i ].x, N );
|
||||
if ( Tudo[ i ].x == Poin[ s ].x )
|
||||
Tudo[ i ].y = Poin[ s ].y;
|
||||
else
|
||||
Tudo[ i ].y = Poin[ s ].y + (Poin[ s+1 ].y-Poin[ s ].y)/(Poin[ s+1 ].x-Poin[ s ].x)*(Tudo[ i ].x-Poin[ s ].x);
|
||||
Tudo[ i ].HP = 40.0*(Tudo[ i ].y-Poin[ 1 ].y) + Tudo[ i ].v*Tudo[ i ].v;
|
||||
}
|
||||
double Max = 0;
|
||||
for ( int i = 1 ; i <= M ; ++ i )
|
||||
if ( Max < Tudo[ i ].HP )
|
||||
Max = Tudo[ i ].HP;
|
||||
for ( int i = 2 ; i <= N ; ++ i ) {
|
||||
double HP = 40.0*(Poin[ i ].y-Poin[ 1 ].y);
|
||||
if ( Max < HP ) Max = HP;
|
||||
}
|
||||
printf("Case %d: %.2lf\n",t,sqrt(Max));
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,136 @@
|
|||
#include<algorithm>
|
||||
#include<iostream>
|
||||
#include<string.h>
|
||||
#include<stdio.h>
|
||||
using namespace std;
|
||||
const int INF=0x3f3f3f3f;
|
||||
const int maxn=100010;
|
||||
typedef long long ll;
|
||||
const ll mod=20110911*2;
|
||||
#define lson L,mid,ls
|
||||
#define rson mid+1,R,rs
|
||||
ll sm[maxn],ss[maxn],sc[maxn<<2],sd[maxn<<2],siid[maxn<<2];
|
||||
ll sic[maxn<<2],sid[maxn<<2],ac[maxn<<2],ad[maxn<<2];
|
||||
ll asc,aic,asd,aid,aiid;
|
||||
void addc(int L,int R,int rt,ll d)
|
||||
{
|
||||
ac[rt]=(ac[rt]+d)%mod;
|
||||
sc[rt]=(sc[rt]+(R-L+1)*d)%mod;
|
||||
sic[rt]=(sic[rt]+(sm[R]-sm[L-1])*d)%mod;
|
||||
}
|
||||
void addd(int L,int R,int rt,ll d)
|
||||
{
|
||||
ad[rt]=(ad[rt]+d)%mod;
|
||||
sd[rt]=(sd[rt]+(R-L+1)*d)%mod;
|
||||
sid[rt]=(sid[rt]+(sm[R]-sm[L-1])*d)%mod;
|
||||
siid[rt]=(siid[rt]+(ss[R]-ss[L-1])*d)%mod;
|
||||
}
|
||||
void PushDown(int L,int R,int rt)
|
||||
{
|
||||
int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
|
||||
if(ad[rt])
|
||||
addd(lson,ad[rt]),addd(rson,ad[rt]),ad[rt]=0;
|
||||
if(ac[rt])
|
||||
addc(lson,ac[rt]),addc(rson,ac[rt]),ac[rt]=0;
|
||||
}
|
||||
void PushUp(int rt)
|
||||
{
|
||||
int ls=rt<<1,rs=ls|1;
|
||||
sc[rt]=(sc[ls]+sc[rs])%mod;
|
||||
sic[rt]=(sic[ls]+sic[rs])%mod;
|
||||
sd[rt]=(sd[ls]+sd[rs])%mod;
|
||||
sid[rt]=(sid[ls]+sid[rs])%mod;
|
||||
siid[rt]=(siid[ls]+siid[rs])%mod;
|
||||
}
|
||||
void build(int L,int R,int rt)
|
||||
{
|
||||
ac[rt]=ad[rt]=0;
|
||||
if(L==R)
|
||||
{
|
||||
scanf("%I64d%I64d",&sc[rt],&sd[rt]);
|
||||
sic[rt]=(L*sc[rt])%mod;
|
||||
sid[rt]=(L*sd[rt])%mod;
|
||||
siid[rt]=(sid[rt]*L)%mod;
|
||||
return;
|
||||
}
|
||||
int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
|
||||
build(lson);
|
||||
build(rson);
|
||||
PushUp(rt);
|
||||
}
|
||||
void update(int L,int R,int rt,int l,int r,ll d,int op)
|
||||
{
|
||||
if(l<=L&&R<=r)
|
||||
{
|
||||
if(op)
|
||||
addd(L,R,rt,d);
|
||||
else
|
||||
addc(L,R,rt,d);
|
||||
return;
|
||||
}
|
||||
int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
|
||||
PushDown(L,R,rt);
|
||||
if(l<=mid)
|
||||
update(lson,l,r,d,op);
|
||||
if(r>mid)
|
||||
update(rson,l,r,d,op);
|
||||
PushUp(rt);
|
||||
}
|
||||
void qu(int L,int R,int rt,int l,int r)
|
||||
{
|
||||
if(l<=L&&R<=r)
|
||||
{
|
||||
asc=(asc+sc[rt])%mod;
|
||||
aic=(aic+sic[rt])%mod;
|
||||
asd=(asd+sd[rt])%mod;
|
||||
aid=(aid+sid[rt])%mod;
|
||||
aiid=(aiid+siid[rt])%mod;
|
||||
return;
|
||||
}
|
||||
int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;
|
||||
PushDown(L,R,rt);
|
||||
if(l<=mid)
|
||||
qu(lson,l,r);
|
||||
if(r>mid)
|
||||
qu(rson,l,r);
|
||||
PushUp(rt);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,t,n,q,x,y,z,cas=1;
|
||||
char cmd[20];
|
||||
for(i=1;i<maxn;i++)
|
||||
{
|
||||
sm[i]=(sm[i-1]+i)%mod;
|
||||
ss[i]=(ss[i-1]+(ll)i*i)%mod;
|
||||
}
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
scanf("%d",&n);
|
||||
build(1,n,1);
|
||||
scanf("%d",&q);
|
||||
printf("Case %d:\n",cas++);
|
||||
while(q--)
|
||||
{
|
||||
asc=aic=asd=aid=aiid=0;
|
||||
scanf("%s%d%d",cmd,&x,&y);
|
||||
if(cmd[0]!='Q')
|
||||
scanf("%d",&z);
|
||||
if(cmd[2]=='s')
|
||||
update(1,n,1,x,y,z,0);
|
||||
else if(cmd[2]=='l')
|
||||
update(1,n,1,x,y,z,1);
|
||||
else
|
||||
{
|
||||
qu(1,n,1,x,y);
|
||||
ll ans=((ll)y*(y+1)*asd-(2*y+1)*aid+aiid)%mod;
|
||||
ans-=2*((y+1)*asc-aic);
|
||||
ans%=mod;
|
||||
ans=(ans+mod)%mod;
|
||||
printf("%I64d\n",ans/2);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <algorithm>
|
||||
#define mod 1000000007
|
||||
using namespace std;
|
||||
int t,n,a[100010],odd_num,m,indx,Minpos,Min;
|
||||
__int64 ans;
|
||||
void init(){
|
||||
odd_num = 0; ans = 1;
|
||||
Min = mod; indx = 1;
|
||||
}
|
||||
long long pow_mod(int a,int n,int p)
|
||||
{
|
||||
long long ret=1;
|
||||
long long A=a;
|
||||
while(n)
|
||||
{
|
||||
if (n & 1)
|
||||
ret=(ret*A)%p;
|
||||
A=(A*A)%p;
|
||||
n>>=1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%d",&t);
|
||||
for(int ca = 1; ca <= t; ca ++){
|
||||
scanf("%d%d",&n,&m); init();
|
||||
for(int i = 1; i <= n;i ++){
|
||||
scanf("%d",a + i);
|
||||
if(a[i] < 0) odd_num++;
|
||||
}
|
||||
sort(a + 1, a + 1 + n);
|
||||
if(odd_num & 1){
|
||||
int j = 1;
|
||||
while(a[j] < 0) j ++; indx = -- j;
|
||||
if(a[j] + m > 0) m += a[j], a[j] = 0;
|
||||
else a[j] += m,m = 0;
|
||||
}
|
||||
if(m > 0){
|
||||
for(int j = indx; j <= n && m > 0; j ++)
|
||||
if(a[j] == 0) a[j] ++, m --;
|
||||
for(int j = indx; j <= n && m > 0; j ++)
|
||||
if(a[j] == 1) a[j] ++, m --;
|
||||
for(int j = indx; j <= n && m > 0; j ++)
|
||||
if(a[j] == 2) a[j] ++, m --;
|
||||
for(int j = 1; j <= n; j ++)
|
||||
ans = (ans * a[j]) % mod;
|
||||
if(m <= 0){
|
||||
printf("Case %d: %I64d\n",ca,ans);
|
||||
}else{
|
||||
if(m == 1){
|
||||
for(int j = 1; j <= n; j ++)
|
||||
if(Min > a[j]) Min = a[j], Minpos = j;
|
||||
ans /= a[Minpos];
|
||||
ans = (ans * (a[Minpos] + 1)) % mod;
|
||||
printf("Case %d: %I64d\n",ca,ans);
|
||||
}else if(m % 3 == 0){
|
||||
ans = (ans * pow_mod(3,m/3,mod)) % mod;
|
||||
printf("Case %d: %I64d\n",ca,ans);
|
||||
}else if((m - 1) % 3 == 0){
|
||||
ans = (ans * pow_mod(3,(m - 1)/3 - 1,mod)) % mod;
|
||||
ans = (ans * 4) % mod;
|
||||
printf("Case %d: %I64d\n",ca,ans);
|
||||
}else if((m - 2) % 3 == 0){
|
||||
ans = (ans * pow_mod(3,(m - 2)/3,mod)) % mod;
|
||||
ans = (ans * 2) % mod;
|
||||
printf("Case %d: %I64d\n",ca,ans);
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
for(int j = 1; j <= n; j ++)
|
||||
ans = (ans * a[j]) % mod;
|
||||
printf("Case %d: %I64d\n",ca,ans);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,106 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<string>
|
||||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#include<cmath>
|
||||
#include<map>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
map<string,int> mp;
|
||||
map<int,string> sp;
|
||||
int head[2500];
|
||||
int n,m,pos,cn;
|
||||
struct edge
|
||||
{
|
||||
int v,next;
|
||||
}eg[2500*2500];
|
||||
struct visit
|
||||
{
|
||||
int v,cnt;
|
||||
}vd[2500];
|
||||
bool cmp(visit a,visit b)
|
||||
{
|
||||
if(a.cnt==b.cnt)return sp[a.v]<sp[b.v];
|
||||
return a.cnt>b.cnt;
|
||||
}
|
||||
void find(int s)
|
||||
{
|
||||
int vs[2500];int num=0;
|
||||
memset(vs,-1,sizeof(vs));
|
||||
bool vid[2500]={0};
|
||||
for(int i=0;i<2500;i++)vd[i].cnt=0;
|
||||
for(int i=head[s];i!=-1;i=eg[i].next)
|
||||
vid[eg[i].v]=1;
|
||||
for(int i=head[s];i!=-1;i=eg[i].next)
|
||||
{
|
||||
for(int j=head[eg[i].v];j!=-1;j=eg[j].next)
|
||||
{
|
||||
if(eg[j].v==s||vid[eg[j].v]==1)continue;
|
||||
if(vs[eg[j].v]==-1)
|
||||
{
|
||||
vd[num].v=eg[j].v;
|
||||
vd[num].cnt++;
|
||||
vs[eg[j].v]=num++;
|
||||
}
|
||||
else
|
||||
vd[vs[eg[j].v]].cnt++;
|
||||
}
|
||||
}
|
||||
sort(vd,vd+num,cmp);
|
||||
if(vd[0].cnt==0)printf("-");
|
||||
else
|
||||
{
|
||||
cout<<sp[vd[0].v];
|
||||
for(int i=1;i<num;i++)
|
||||
{
|
||||
if(vd[i].cnt==vd[0].cnt)
|
||||
{
|
||||
cout<<' '<<sp[vd[i].v];
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T,t,x,y;
|
||||
char s1[100],s2[100];
|
||||
scanf("%d",&T);
|
||||
for(int t=1;t<=T;t++)
|
||||
{
|
||||
pos=1,cn=0;
|
||||
memset(head,-1,sizeof(head));
|
||||
mp.clear();sp.clear();
|
||||
scanf("%d%d",&n,&m);
|
||||
getchar();
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
scanf("%s",s1);
|
||||
scanf("%s",s2);
|
||||
getchar();
|
||||
if(mp[s1]==0)
|
||||
{
|
||||
mp[s1]=pos;
|
||||
sp[pos++]=s1;
|
||||
}
|
||||
if(mp[s2]==0)
|
||||
{
|
||||
mp[s2]=pos;
|
||||
sp[pos++]=s2;
|
||||
}
|
||||
x=mp[s1];y=mp[s2];
|
||||
eg[cn].v=y;eg[cn].next=head[x];head[x]=cn++;
|
||||
eg[cn].v=x;eg[cn].next=head[y];head[y]=cn++;
|
||||
}
|
||||
printf("Case %d:\n",t);
|
||||
for(int i=0;i<m;i++)
|
||||
{
|
||||
scanf("%s",s1);
|
||||
getchar();
|
||||
int tem=mp[s1];
|
||||
find(tem);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
#include<iostream>
|
||||
#include<string>
|
||||
#include<stack>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
stack<int> fstack;
|
||||
queue<int> Q;
|
||||
char s[1000010];
|
||||
struct name
|
||||
{
|
||||
char str[11];
|
||||
}na[50001];
|
||||
int main()
|
||||
{
|
||||
int cas,n,num;
|
||||
char str1[11];
|
||||
scanf("%d",&cas);
|
||||
while(cas--)
|
||||
{
|
||||
while(!fstack.empty())
|
||||
fstack.pop();
|
||||
while(!Q.empty())
|
||||
Q.pop();
|
||||
scanf("%s",s);
|
||||
int len=strlen(s);
|
||||
num=0;
|
||||
int j=0;
|
||||
for(int i=0;i<len;i++)
|
||||
{
|
||||
if(s[i]>='a'&&s[i]<='z')
|
||||
str1[j++]=s[i];
|
||||
else
|
||||
{
|
||||
if(j!=0)
|
||||
{
|
||||
str1[j]='\0';
|
||||
j=0;
|
||||
strcpy(na[++num].str,str1);
|
||||
}
|
||||
if(s[i]=='(')
|
||||
{
|
||||
fstack.push(num);
|
||||
Q.push(num);
|
||||
}
|
||||
else if(s[i]==',')
|
||||
{
|
||||
if(s[i-1]!=')')
|
||||
Q.push(num);
|
||||
Q.push(fstack.top());
|
||||
}
|
||||
else {
|
||||
if(s[i-1]!=')')
|
||||
Q.push(num);
|
||||
Q.push(fstack.top());
|
||||
fstack.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
if(num==0)
|
||||
{
|
||||
str1[j]='\0';
|
||||
printf("1\n%s\n\n",str1);
|
||||
continue;
|
||||
}
|
||||
printf("%d\n",num);
|
||||
for(int i=1;i<=num;i++)
|
||||
printf("%s\n",na[i].str);
|
||||
while(!Q.empty())
|
||||
{
|
||||
if(Q.front()==1&&Q.size()==1)
|
||||
break;
|
||||
printf("%d ",Q.front());
|
||||
Q.pop();
|
||||
printf("%d\n",Q.front());
|
||||
}
|
||||
puts("");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,130 @@
|
|||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
#define db double
|
||||
#define rt return
|
||||
#define cs const
|
||||
cs db eps = 1e-8 , PI = acos(-1.);
|
||||
inline int sig(db x){rt (x>eps)-(x<-eps);}
|
||||
#define cp const P&
|
||||
#define op operator
|
||||
cs int N = 511;
|
||||
struct P {
|
||||
db x , y , z;
|
||||
P(db a = 0 , db b = 0 , db c = 0):x(a),y(b),z(c){}
|
||||
void in(){scanf("%lf%lf%lf" , &x , &y , &z);}
|
||||
P op+(cp a)cs{rt P(x+a.x , y+a.y , z+a.z);}
|
||||
P op-(cp a)cs{rt P(x-a.x , y-a.y , z-a.z);}
|
||||
P op*(cs db&k)cs{rt P(x*k , y*k , z*k);}
|
||||
P op^(cp a)cs{rt P(y*a.z-a.y*z , z*a.x-a.z*x , x*a.y-a.x*y);}
|
||||
db op*(cp a)cs{rt x*a.x + y*a.y + z*a.z;}
|
||||
db L(){rt sqrt(x*x + y*y + z*z);}
|
||||
db rela(P v){rt sig(*this * v) * L();}
|
||||
P cross(P a , P b){rt a-*this ^ b-*this;}
|
||||
db dot(P a , P b){rt (a-*this) * (b-*this);}
|
||||
int on_seg(P a , P b){rt cross(a,b).L() < eps && dot(a , b) < eps;}
|
||||
};
|
||||
P vp1[N] , vp2[N];
|
||||
int cnt1 , cnt2;
|
||||
struct face {
|
||||
P a , b , c , v;
|
||||
void init(){v = a.cross(b , c);}
|
||||
int side(P p){rt sig(v*(p-a));}
|
||||
int inter_line(P p , P vec , P &x) {
|
||||
if(!sig(v*vec)) rt 0;
|
||||
x = p + vec * (v*(a-p) / (v*vec));
|
||||
rt 1;
|
||||
}
|
||||
};
|
||||
inline db get_angle(P o , P a , P b , P v) {
|
||||
rt atan2(o.cross(a , b).rela(v) , o.dot(a , b));
|
||||
}
|
||||
int read() {
|
||||
scanf("%d%d" , &cnt1 , &cnt2);
|
||||
for(int i = 0 ; i < cnt1 ; i++) vp1[i].in();
|
||||
for(int i = 0 ; i < cnt2 ; i++) vp2[i].in();
|
||||
vp1[cnt1] = vp1[0] , vp2[cnt2] = vp2[0];
|
||||
rt cnt1 + cnt2;
|
||||
}
|
||||
inline int in_poly(P p , P vp[] , int n) {
|
||||
for(int i = 0 ;i < n ; i++)
|
||||
if(p.on_seg(vp[i] , vp[i+1])) rt 1;
|
||||
P v;
|
||||
for(int i = 2 ; i < n ; i++)
|
||||
if(sig(vp[0].cross(vp[1] , vp[i]).L())) {
|
||||
v = vp[0].cross(vp[1] , vp[i]);
|
||||
break;
|
||||
}
|
||||
db agl = 0.;
|
||||
for(int i = 0 ; i < n ; i++)
|
||||
agl += get_angle(p , vp[i] , vp[i+1] , v);
|
||||
rt !sig(agl-2.*PI) || !sig(agl+2.*PI);
|
||||
}
|
||||
inline int seg_seg(P a , P b , P c , P d) {
|
||||
if(sig(a.cross(b , c) * (d-a))) rt 0;
|
||||
P v = b - a ^ d - c;
|
||||
db t = v.L();
|
||||
if(sig(t) == 0)
|
||||
rt a.on_seg(c , d) || b.on_seg(c , d) || c.on_seg(a , b) || d.on_seg(a , b);
|
||||
db u1 = c.cross(d , a).rela(v) / t , u2 = a.cross(c , b).rela(v) / t;
|
||||
rt sig(u1*(u1-1.)) <= 0 && sig(u2*(u2-1.)) <= 0;
|
||||
}
|
||||
inline int seg_face(P a , P b , P vp[] , int n) {
|
||||
face f;
|
||||
for(int i = 2 ; i < n ; i++)
|
||||
if(sig(vp[0].cross(vp[1] , vp[i]).L())) {
|
||||
f.a = vp[0] , f.b = vp[1] , f.c = vp[i] , f.init();
|
||||
break;
|
||||
}
|
||||
int l = f.side(a) , r = f.side(b);
|
||||
if(l * r > 0) rt 0;
|
||||
if(l || r) {
|
||||
P xp;
|
||||
f.inter_line(a , b - a , xp);
|
||||
rt in_poly(xp , vp , n);
|
||||
}
|
||||
for(int i = 0 ; i < n ; i++)
|
||||
if(seg_seg(a , b , vp[i] , vp[i+1])) rt 1;
|
||||
rt in_poly(a , vp , n) || in_poly(b , vp , n);
|
||||
}
|
||||
inline int face_face(P vp1[] , int cn1 , P vp2[] , int cnt2) {
|
||||
for(int i = 0 ; i < cnt1 ; i++) {
|
||||
if(seg_face(vp1[i] , vp1[i+1] , vp2 , cnt2)) rt 1;
|
||||
}
|
||||
rt 0;
|
||||
}
|
||||
inline int test_data(P vp[] , int n) {
|
||||
int line = 1;
|
||||
for(int i = 2 ; line && i < n ; i++)
|
||||
if(sig(vp[0].cross(vp[1] , vp[i]).L())) line = 0;
|
||||
rt !line;
|
||||
}
|
||||
void solve() {
|
||||
int pic1 = test_data(vp1 , cnt1) , pic2 = test_data(vp2 , cnt2);
|
||||
int inter = 0;
|
||||
if(pic1 == 0 && pic2 == 0) {
|
||||
for(int i = 0 ; inter == 0 && i < cnt1 ; i++)
|
||||
for(int j = 0 ; inter == 0 && j < cnt2 ; j++)
|
||||
inter = seg_seg(vp1[i] , vp1[i+1] , vp2[j] , vp2[j+1]);
|
||||
}
|
||||
if(pic1 == 0 && pic2 == 1) {
|
||||
for(int i = 0 ; inter == 0 && i < cnt1 ; i++)
|
||||
inter = seg_face(vp1[i] , vp1[i+1] , vp2 , cnt2);
|
||||
}
|
||||
if(pic1 == 1 && pic2 == 0) {
|
||||
for(int i = 0 ; inter == 0 && i < cnt2 ; i++)
|
||||
inter = seg_face(vp2[i] , vp2[i+1] , vp1 , cnt1);
|
||||
}
|
||||
if(pic1 == 1 && pic2 == 1) {
|
||||
inter = face_face(vp1 , cnt1 , vp2 , cnt2) ||
|
||||
face_face(vp2 , cnt2 , vp1 , cnt1);
|
||||
}
|
||||
puts(inter ? "YES" : "NO");
|
||||
}
|
||||
int main() {
|
||||
while(read()) {
|
||||
solve();
|
||||
}
|
||||
rt 0;
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#define N 2010
|
||||
#define K 510
|
||||
char mol[K][N],den[K][N];
|
||||
char virt[N];
|
||||
int gcd(int a,int b)
|
||||
{
|
||||
return b?gcd(b,a%b):a;
|
||||
}
|
||||
int divide(char *str,int n,char *s)
|
||||
{
|
||||
int i,k,len;
|
||||
len=strlen(str);
|
||||
s[len]=0;
|
||||
for(k=0,i=len-1;i>=0;i--)
|
||||
{
|
||||
k=k*10+str[i]-'0';
|
||||
s[i]=k/n+'0';
|
||||
k%=n;
|
||||
}
|
||||
for(i=len-1;i>0;i--)
|
||||
{
|
||||
if(s[i]!='0')
|
||||
break;
|
||||
s[i]=0;
|
||||
}
|
||||
return k;
|
||||
}
|
||||
void mult(char *str,int n,char *x)
|
||||
{
|
||||
int i,s[N];
|
||||
for(i=0;i<N;i++)
|
||||
s[i]=0;
|
||||
for(i=0;str[i];i++)
|
||||
s[i]=(str[i]-'0')*n;
|
||||
for(i=0;i<N-1;i++)
|
||||
{
|
||||
s[i+1]+=s[i]/10;
|
||||
s[i]%=10;
|
||||
}
|
||||
for(i=N-1;i>0 && s[i]==0;i--);
|
||||
x[i+1]=0;
|
||||
for(;i>=0;i--)x[i]=s[i]+'0';
|
||||
}
|
||||
void init()
|
||||
{
|
||||
int i,j,k;
|
||||
strcpy(mol[1],"1");
|
||||
strcpy(den[1],"2");
|
||||
for(i=2;i<K;i++)
|
||||
{
|
||||
j=2*i;
|
||||
k=gcd(j,divide(mol[i-1],j,virt));
|
||||
divide(mol[i-1],k,mol[i]);
|
||||
mult(den[i-1],j/k,den[i]);
|
||||
j--;
|
||||
k=gcd(j,divide(den[i],j,virt));
|
||||
if(k>1)
|
||||
divide(den[i],k,den[i]);
|
||||
mult(mol[i],j/k,mol[i]);
|
||||
strrev(mol[i-1]);
|
||||
strrev(den[i-1]);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n,T;
|
||||
init();
|
||||
scanf("%d",&T);
|
||||
while(T--)
|
||||
{
|
||||
scanf("%d",&n);
|
||||
printf("%s/%s\n",mol[n],den[n]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
#include<stdio.h>
|
||||
#include<iostream>
|
||||
#include<string.h>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
const int MAXN=1010;
|
||||
const int INF=0x3fffffff;
|
||||
struct Node
|
||||
{
|
||||
int to;
|
||||
int next;
|
||||
}edge[MAXN*2];
|
||||
int tol;
|
||||
int head[MAXN];
|
||||
int dp[MAXN][220];
|
||||
int price[MAXN][60];
|
||||
int power[MAXN][60];
|
||||
void init()
|
||||
{
|
||||
memset(head,-1,sizeof(head));
|
||||
tol=0;
|
||||
}
|
||||
void add(int a,int b)
|
||||
{
|
||||
edge[tol].to=b;
|
||||
edge[tol].next=head[a];
|
||||
head[a]=tol++;
|
||||
edge[tol].to=a;
|
||||
edge[tol].next=head[b];
|
||||
head[b]=tol++;
|
||||
}
|
||||
int n,m;
|
||||
int tmp[MAXN];
|
||||
void dfs(int u,int pre)
|
||||
{
|
||||
if(head[u]==-1||(edge[head[u]].to==pre&&edge[head[u]].next==-1))
|
||||
{
|
||||
for(int i=0;i<=m;i++)dp[u][i]=0;
|
||||
for(int i=0;i<=m;i++)tmp[i]=dp[u][i];
|
||||
for(int i=m;i>=0;i--)
|
||||
{
|
||||
for(int j=1;j<=price[u][0];j++)
|
||||
if(price[u][j]<=i)
|
||||
dp[u][i]=max(dp[u][i],tmp[i-price[u][j]]+power[u][j]);
|
||||
tmp[i]=dp[u][i];
|
||||
}
|
||||
return;
|
||||
}
|
||||
for(int i=0;i<=m;i++) dp[u][i]=INF;
|
||||
for(int i=head[u];i!=-1;i=edge[i].next)
|
||||
{
|
||||
int v=edge[i].to;
|
||||
if(v==pre)continue;
|
||||
dfs(v,u);
|
||||
for(int j=m;j>=0;j--)
|
||||
{
|
||||
int t=0;
|
||||
for(int k=0;k<=j;k++)
|
||||
t=max(t,min(dp[u][j-k],dp[v][k]));
|
||||
dp[u][j]=t;
|
||||
}
|
||||
}
|
||||
for(int i=0;i<=m;i++)tmp[i]=dp[u][i];
|
||||
for(int i=m;i>=0;i--)
|
||||
{
|
||||
for(int j=1;j<=price[u][0];j++)
|
||||
if(price[u][j]<=i)
|
||||
dp[u][i]=max(dp[u][i],tmp[i-price[u][j]]+power[u][j]);
|
||||
tmp[i]=dp[u][i];
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int u,v;
|
||||
int T;
|
||||
scanf("%d",&T);
|
||||
while(T--)
|
||||
{
|
||||
init();
|
||||
scanf("%d",&n);
|
||||
for(int i=1;i<n;i++)
|
||||
{
|
||||
scanf("%d%d",&u,&v);
|
||||
add(u,v);
|
||||
}
|
||||
scanf("%d",&m);
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
scanf("%d",&price[i][0]);
|
||||
power[i][0]=price[i][0];
|
||||
for(int j=1;j<=price[i][0];j++)
|
||||
{
|
||||
scanf("%d%d",&price[i][j],&power[i][j]);
|
||||
}
|
||||
}
|
||||
dfs(1,0);
|
||||
printf("%d\n",dp[1][m]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
#include<iostream>
|
||||
#include<cmath>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<algorithm>
|
||||
#define LL long long
|
||||
#define eps 1e-7
|
||||
#define MOD 1000000007
|
||||
using namespace std;
|
||||
int c[2001][2001]={1},stir2[1005][1005]={1};
|
||||
int main(){
|
||||
for(int i=1;i<=2000;i++){
|
||||
c[i][0]=c[i][i]=1;
|
||||
for(int j=1;j<i;j++)
|
||||
c[i][j]=(c[i-1][j-1]+c[i-1][j])%MOD;
|
||||
}
|
||||
for(int i=1;i<=1000;i++){
|
||||
stir2[i][0]=0;
|
||||
stir2[i][i]=1;
|
||||
for(int j=1;j<i;j++)
|
||||
stir2[i][j]=((LL)j*stir2[i-1][j]+stir2[i-1][j-1])%MOD;
|
||||
}
|
||||
int n,r,k,m;
|
||||
while(cin>>n>>r>>k>>m){
|
||||
int sum=0;
|
||||
if(n-((r-1)*k+1)<0){
|
||||
cout<<0<<endl;
|
||||
continue;
|
||||
}
|
||||
for(int i=1;i<=min(r,m);i++)
|
||||
sum=(sum+stir2[r][i])%MOD;
|
||||
cout<<((LL)c[n-((r-1)*k+1)+r+1-1][r]*sum)%MOD<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
#include <cstdio>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int n, m;
|
||||
int c[60000];
|
||||
char str[60000];
|
||||
int lowbit ( int x )
|
||||
{
|
||||
return x & ( -x );
|
||||
}
|
||||
void update ( int x, int v )
|
||||
{
|
||||
for ( int i = x; i <= n; i += lowbit(i) )
|
||||
c[i] += v;
|
||||
}
|
||||
int getSum ( int x )
|
||||
{
|
||||
int sum = 0;
|
||||
for ( int i = x; i > 0; i -= lowbit(i) )
|
||||
sum += c[i];
|
||||
return sum;
|
||||
}
|
||||
bool check ( char x, char y, char z )
|
||||
{
|
||||
if ( x == 'w' && y == 'b' && z == 'w' )
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
void init ()
|
||||
{
|
||||
for ( int i = 3; i <= n; i++ )
|
||||
if ( check(str[i-2],str[i-1],str[i]) )
|
||||
update ( i, 1 );
|
||||
}
|
||||
int main()
|
||||
{
|
||||
char change;
|
||||
int t, l, r, pos, oper;
|
||||
int cnt = 0;
|
||||
scanf("%d",&t);
|
||||
while ( t-- )
|
||||
{
|
||||
scanf("%d%d",&n,&m);
|
||||
scanf("%s",str+1);
|
||||
memset(c,0,sizeof(c));
|
||||
init();
|
||||
printf("Case %d:\n",++cnt);
|
||||
while ( m-- )
|
||||
{
|
||||
scanf("%d",&oper);
|
||||
if ( oper == 0 )
|
||||
{
|
||||
scanf("%d%d",&l,&r);
|
||||
l++; r++;
|
||||
if ( l > r ) swap ( l, r );
|
||||
if ( r - l < 2 ) printf("0\n");
|
||||
else printf ("%d\n", getSum(r) - getSum(l+1) );
|
||||
}
|
||||
else
|
||||
{
|
||||
int i;
|
||||
scanf("%d %c",&pos, &change);
|
||||
pos++;
|
||||
if ( str[pos] == change ) continue;
|
||||
i = pos;
|
||||
if ( 3 <= i && i <= n )
|
||||
{
|
||||
if ( check(str[i-2],str[i-1],str[i]) && !check(str[i-2],str[i-1],change) )
|
||||
update(i,-1);
|
||||
if ( !check(str[i-2],str[i-1],str[i]) && check(str[i-2],str[i-1],change) )
|
||||
update(i,1);
|
||||
}
|
||||
i = pos + 1;
|
||||
if ( 3 <= i && i <= n )
|
||||
{
|
||||
if ( check(str[i-2],str[i-1],str[i]) && !check(str[i-2],change,str[i]) )
|
||||
update(i,-1);
|
||||
if ( !check(str[i-2],str[i-1],str[i]) && check(str[i-2],change,str[i]) )
|
||||
update(i,1);
|
||||
}
|
||||
i = pos + 2;
|
||||
if ( 3 <= i && i <= n )
|
||||
{
|
||||
if ( check(str[i-2],str[i-1],str[i]) && !check(change,str[i-1],str[i]) )
|
||||
update(i,-1);
|
||||
if ( !check(str[i-2],str[i-1],str[i]) && check(change,str[i-1],str[i]) )
|
||||
update(i,1);
|
||||
}
|
||||
str[pos] = change;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <algorithm>
|
||||
#define MAXN 20010
|
||||
int MOD;
|
||||
using namespace std;
|
||||
typedef long long LL;
|
||||
int cas, n, m, tx;
|
||||
int pri[MAXN], notp[MAXN], flag[MAXN], prs;
|
||||
int tot[MAXN], maxm;
|
||||
int fac[MAXN][305], facs[MAXN], phi[MAXN * 10];
|
||||
void init(){
|
||||
for (int i = 2; i < MAXN; i++) if (!notp[i]) {
|
||||
flag[i] = 1;
|
||||
for (int j = i*2; j < MAXN; j += i) {
|
||||
if (!notp[j]) notp[j] = flag[j] = 1;
|
||||
else if (flag[j]) flag[j]++;
|
||||
if (j%(i*i) == 0) flag[j] = 0;
|
||||
}
|
||||
}
|
||||
for (int i = 2; i < MAXN; i++)
|
||||
if (!notp[i]) pri[prs++] = i;
|
||||
for (int i = 1; i < MAXN; i++)
|
||||
for (int j = 1; j * j <= i; j++) if (i % j == 0) {
|
||||
fac[i][facs[i]++] = j;
|
||||
if (j * j != i)fac[i][facs[i]++] = i/j;
|
||||
}
|
||||
const int maxn = 100000;
|
||||
for (int i = 1; i <= maxn; i ++) phi[i] = i;
|
||||
for (int i = 2; i <= maxn; i ++) if(phi[i] == i) {
|
||||
for(int j = i; j <= maxn; j += i)
|
||||
phi[j] = phi[j] / i * (i - 1);
|
||||
}
|
||||
}
|
||||
int powmod(int x, int d){
|
||||
int ans = 1, tmp = x;
|
||||
for (; d; d >>= 1, tmp = (LL)tmp * tmp % MOD)
|
||||
if (d&1) ans = (LL)ans * tmp % MOD;
|
||||
return ans;
|
||||
}
|
||||
int cal(int l){
|
||||
int ans = powmod(tot[1], l);
|
||||
for (int i = 2; i <= maxm; i++) {
|
||||
if (flag[i] == 0) continue;
|
||||
if (flag[i] & 1) ans = (ans - powmod(tot[i], l)) % MOD;
|
||||
else ans = (ans + powmod(tot[i], l)) % MOD;
|
||||
}
|
||||
return (ans % MOD + MOD) % MOD;
|
||||
}
|
||||
void solve(){
|
||||
int ans = 0;
|
||||
for(int i = 1; i * i <= n; i ++) if(n % i == 0) {
|
||||
ans = (ans + (LL)cal(i) * phi[n / i]) % MOD;
|
||||
if(i * i != n)
|
||||
ans = (ans + (LL)cal(n / i) * phi[i]) % MOD;
|
||||
}
|
||||
printf("%d\n", (ans + MOD) % MOD / n);
|
||||
}
|
||||
int main(){
|
||||
init();
|
||||
scanf("%d", &cas);
|
||||
while (cas--) {
|
||||
scanf("%d%d", &m, &n);
|
||||
MOD = 10007 * n;
|
||||
memset(tot, 0, sizeof tot);
|
||||
maxm = 0;
|
||||
for (int i = 1; i <= m; i++) {
|
||||
scanf("%d", &tx);
|
||||
maxm = max(tx, maxm);
|
||||
for (int j = 0; j < facs[tx]; j++)
|
||||
tot[fac[tx][j]]++;
|
||||
}
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
int Maps[ 11 ][ 11 ];
|
||||
int Cost[ 11 ];
|
||||
int Inst[ 11 ][ 11 ];
|
||||
int F[ 11 ][ 1025 ];
|
||||
int Stat[ 11 ];
|
||||
int Q[ 11 ][ 1025 ];
|
||||
int main()
|
||||
{
|
||||
int N,M;
|
||||
while ( cin >> N >> M && N ) {
|
||||
for ( int i = 1 ; i <= M ; ++ i )
|
||||
cin >> Cost[ i ];
|
||||
for ( int i = 1 ; i <= N ; ++ i )
|
||||
for ( int j = 1 ; j <= M ; ++ j )
|
||||
cin >> Inst[ i ][ j ];
|
||||
for ( int i = 1 ; i <= N ; ++ i )
|
||||
for ( int j = 1 ; j <= N ; ++ j )
|
||||
cin >> Maps[ i ][ j ];
|
||||
int V = (1<<N)-1;
|
||||
for ( int i = 0 ; i <= N ; ++ i )
|
||||
for ( int j = 0 ; j <= V ; ++ j )
|
||||
F[ i ][ j ] = -10000001;
|
||||
for ( int j = 0 ; j <= V ; ++ j )
|
||||
F[ 0 ][ j ] = 0;
|
||||
for ( int i = 1 ; i <= M ; ++ i )
|
||||
for ( int j = 0 ; j <= V ; ++ j ) {
|
||||
int temp = j;
|
||||
for ( int k = 1 ; k <= N ; ++ k ) {
|
||||
Stat[ k ] = temp%2;
|
||||
temp /= 2;
|
||||
}
|
||||
int Save = 0;
|
||||
for ( int k = 1 ; k <= N ; ++ k )
|
||||
if ( Stat[ k ] )
|
||||
Save += Inst[ k ][ i ] - Cost[ i ];
|
||||
for ( int p = 1 ; p <= N ; ++ p )
|
||||
for ( int q = p+1 ; q <= N ; ++ q )
|
||||
if ( Stat[ p ] && Stat[ q ] )
|
||||
Save += Maps[ p ][ q ];
|
||||
int count = 0;
|
||||
Q[ 0 ][ 0 ] = j;
|
||||
for ( int k = 1 ; k <= N ; ++ k )
|
||||
if ( !Stat[ k ] ) {
|
||||
for ( int p = 0 ; p < (1<<count) ; ++ p ) {
|
||||
int New = Q[ count ][ p ];
|
||||
Q[ count+1 ][ p*2+0 ] = New;
|
||||
Q[ count+1 ][ p*2+1 ] = New|(1<<(k-1));
|
||||
}
|
||||
++ count;
|
||||
}
|
||||
int Min = -10000001;
|
||||
for ( int k = 0 ; k < (1<<count) ; ++ k ) {
|
||||
if ( Min < F[ i-1 ][ Q[ count ][ k ] ] )
|
||||
Min = F[ i-1 ][ Q[ count ][ k ] ];
|
||||
}
|
||||
F[ i ][ j ] = Min+Save;
|
||||
}
|
||||
int Max = -10000001;
|
||||
for ( int i = 1 ; i <= M ; ++ i )
|
||||
for ( int j = 0 ; j <= V ; ++ j )
|
||||
if ( Max < F[ i ][ j ] )
|
||||
Max = F[ i ][ j ];
|
||||
if ( Max > 0 )
|
||||
cout << Max << endl;
|
||||
else
|
||||
cout << "STAY HOME" << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<queue>
|
||||
#include<cmath>
|
||||
#include<string>
|
||||
#include<vector>
|
||||
#include<algorithm>
|
||||
#include<map>
|
||||
#include<set>
|
||||
#include<ctime>
|
||||
#define maxn 200005
|
||||
#define eps 1e-8
|
||||
#define inf 2000000000
|
||||
#define LL long long
|
||||
#define zero(a) fabs(a)<eps
|
||||
#define MOD 19901014
|
||||
#define N 1000005
|
||||
#define pi acos(-1.0)
|
||||
using namespace std;
|
||||
double dp[4004][4];
|
||||
double p[4004][4];
|
||||
int main(){
|
||||
int t,a,b,n;
|
||||
scanf("%d",&t);
|
||||
while(t--){
|
||||
scanf("%d%d%d",&n,&a,&b);
|
||||
for(int i=1;i<=n;i++)
|
||||
for(int j=0;j<4;j++)
|
||||
scanf("%lf",&p[i][j]);
|
||||
for(int i=n+1;i<=n+a;i++)
|
||||
for(int j=0;j<4;j++)
|
||||
p[i][j]=(j==3);
|
||||
memset(dp,0,sizeof(dp));
|
||||
dp[0][3]=1;
|
||||
for(int i=0;i<=n;i++){
|
||||
double p1=1,p2=1,p3=1;
|
||||
for(int j=a;j<=b;j++){
|
||||
dp[i+j][2]+=dp[i][1]*p1*p[i+j][2];
|
||||
dp[i+j][3]+=dp[i][1]*p1*p[i+j][3];
|
||||
p1*=(p[i+j][0]+p[i+j][1]);
|
||||
dp[i+j][1]+=dp[i][2]*p2*p[i+j][1];
|
||||
dp[i+j][3]+=dp[i][2]*p2*p[i+j][3];
|
||||
p2*=(p[i+j][0]+p[i+j][2]);
|
||||
dp[i+j][1]+=dp[i][3]*p3*p[i+j][1];
|
||||
dp[i+j][2]+=dp[i][3]*p3*p[i+j][2];
|
||||
dp[i+j][3]+=dp[i][3]*p3*p[i+j][3];
|
||||
p3*=p[i+j][0];
|
||||
}
|
||||
}
|
||||
double ans=0;
|
||||
for(int i=1;i<=n+a;i++)
|
||||
for(int j=1;j<4;j++)
|
||||
ans+=dp[i][j];
|
||||
printf("%.8f\n",ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <map>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
char ss[4][10], str[600];
|
||||
int l[5], c[4], n, mul[10], len, cn;
|
||||
int maxl[5];
|
||||
char stmp[10];
|
||||
bool pd(int x);
|
||||
int a[30], stri;
|
||||
bool check(int ssx){
|
||||
int i, x;
|
||||
for (i = 0; ss[ssx][i]; i++){
|
||||
if (ss[ssx][i] < 10){
|
||||
if (!check(ss[ssx][i])){
|
||||
return false;
|
||||
}
|
||||
}else{
|
||||
x = ss[ssx][i];
|
||||
if (a[x] == -1){
|
||||
a[x] = str[stri];
|
||||
}
|
||||
if (a[x] != str[stri]) return false;
|
||||
stri++;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool wk(int x, int p){
|
||||
int i;
|
||||
if (l[x] > len) return false;
|
||||
if (p == c[x]){
|
||||
maxl[x] = max(maxl[x + 1], l[x]);
|
||||
return pd(x - 1);
|
||||
}
|
||||
for (i = x + 1; i < n; i++){
|
||||
l[x] += l[i];
|
||||
ss[x][p] = i;
|
||||
if (wk(x, p + 1)) return true;
|
||||
l[x] -= l[i];
|
||||
}
|
||||
ss[x][p] = cn++;
|
||||
l[x]++;
|
||||
if (wk(x, p + 1)) return true;
|
||||
cn--;
|
||||
l[x]--;
|
||||
ss[x][p] = 0;
|
||||
if (wk(x, c[x])) return true;
|
||||
return false;
|
||||
}
|
||||
bool pd(int x){
|
||||
if (mul[x] * maxl[x + 1] < len){
|
||||
return false;
|
||||
}
|
||||
if (x < 0){
|
||||
if (l[0] != len) return false;
|
||||
memset(a, -1, sizeof(a));
|
||||
stri = 0;
|
||||
return check(0);
|
||||
}
|
||||
l[x] = 0;
|
||||
if (wk(x, 0)) return true;
|
||||
return false;
|
||||
}
|
||||
map<char, int> mp;
|
||||
int main(){
|
||||
int i, s, ss;
|
||||
while (scanf("%d", &n) != EOF){
|
||||
s = 1;
|
||||
for (i = 0; i < n; i++){
|
||||
scanf("%d", &c[i]);
|
||||
s += c[i] - 1;
|
||||
}
|
||||
ss = 0;
|
||||
mul[0] = c[0];
|
||||
for (i = 1; i < n; i++){
|
||||
mul[i] = mul[i - 1] * c[i];
|
||||
}
|
||||
l[n] = 1;
|
||||
maxl[n] = 1;
|
||||
scanf("%s", str);
|
||||
mp.clear();
|
||||
for (i = 0; str[i]; i++){
|
||||
if (mp.count(str[i]) == 0){
|
||||
mp[str[i]] = 1;
|
||||
ss++;
|
||||
}
|
||||
}
|
||||
if (ss > s){
|
||||
printf("No\n");
|
||||
continue;
|
||||
}
|
||||
len = strlen(str);
|
||||
if (len <= s){
|
||||
printf("Yes\n");
|
||||
continue;
|
||||
}
|
||||
cn = 10;
|
||||
if (pd(n - 1)){
|
||||
printf("Yes\n");
|
||||
}else{
|
||||
printf("No\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,113 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
#define lson l, m, rt << 1
|
||||
#define rson m + 1, r, rt << 1 | 1
|
||||
#define LL long long
|
||||
const int maxn = 100110;
|
||||
int sum[maxn << 2], cover[maxn << 2];
|
||||
int V[maxn], p[maxn][4];
|
||||
int n, w, h, cnt, tot;
|
||||
struct ST{
|
||||
int l, r, h, c;
|
||||
ST(){}
|
||||
ST(int L, int R, int H, int C):l(L), r(R), h(H), c(C){};
|
||||
bool operator < (const ST &cmp) const{
|
||||
return h < cmp.h;
|
||||
}
|
||||
}ss[maxn];
|
||||
void PushUp(int rt, int l, int r)
|
||||
{
|
||||
if(cover[rt]) sum[rt] = V[r + 1] - V[l];
|
||||
else if(l == r) sum[rt] = 0;
|
||||
else sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
|
||||
}
|
||||
void update(int L, int R, int c, int l, int r, int rt)
|
||||
{
|
||||
if(L <= l && r <= R){
|
||||
cover[rt] += c;
|
||||
PushUp(rt, l, r);
|
||||
return ;
|
||||
}
|
||||
int m = (l + r) >> 1;
|
||||
if(L <= m) update(L, R, c, lson);
|
||||
if(m < R) update(L, R, c, rson);
|
||||
PushUp(rt, l, r);
|
||||
}
|
||||
int Bin(int x)
|
||||
{
|
||||
int l = 0, r = tot - 1;
|
||||
while(l <= r){
|
||||
int mid = (l + r) >> 1;
|
||||
if(x == V[mid]) return mid;
|
||||
else if(x > V[mid]) l = mid + 1;
|
||||
else r = mid - 1;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
LL gao()
|
||||
{
|
||||
LL res = 0;
|
||||
tot = 1;
|
||||
sort(V, V + cnt);
|
||||
sort(ss, ss + cnt);
|
||||
for(int i = 1; i < cnt; i++){
|
||||
if(V[i] != V[i - 1])
|
||||
V[tot++] = V[i];
|
||||
}
|
||||
memset(sum, 0, sizeof(sum));
|
||||
memset(cover, 0, sizeof(cover));
|
||||
for(int i = 0; i < cnt - 1; i++){
|
||||
int l = Bin(ss[i].l), r= Bin(ss[i].r) - 1;
|
||||
if(l <= r){
|
||||
update(l, r, ss[i].c, 0, tot, 1);
|
||||
res += (LL)sum[1] * (ss[i + 1].h - ss[i].h);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
LL sloveX(int n1, int m)
|
||||
{
|
||||
cnt = 0;
|
||||
for(int i = 0; i < n1; i++){
|
||||
V[cnt] = max(0, p[i][0] - m);
|
||||
ss[cnt++] = ST(max(0, p[i][0] - m), p[i][2], p[i][1] - 1, 1);
|
||||
V[cnt] = p[i][2];
|
||||
ss[cnt++] = ST(max(0, p[i][0] - m), p[i][2], p[i][3], -1);
|
||||
}
|
||||
V[cnt] = w - m + 1;
|
||||
ss[cnt++] = ST(w - m + 1, w, 0, 1);
|
||||
V[cnt] = w;
|
||||
ss[cnt++] = ST(w - m + 1, w, h, -1);
|
||||
return gao();
|
||||
}
|
||||
LL sloveY(int n1, int m)
|
||||
{
|
||||
cnt = 0;
|
||||
for(int i = 0; i < n1; i++){
|
||||
V[cnt] = p[i][0] - 1;
|
||||
ss[cnt++] = ST(p[i][0] - 1, p[i][2], max(0, p[i][1] - m), 1);
|
||||
V[cnt] = p[i][2];
|
||||
ss[cnt++] = ST(p[i][0] - 1, p[i][2], p[i][3], -1);
|
||||
}
|
||||
V[cnt] = 0;
|
||||
ss[cnt++] = ST(0, w, h - m + 1, 1);
|
||||
V[cnt] = w;
|
||||
ss[cnt++] = ST(0, w, h, -1);
|
||||
return gao();
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n1, m;
|
||||
while(scanf("%d %d %d %d", &w, &h, &n1, &m) == 4){
|
||||
for(int i = 0; i < n1; i++)
|
||||
scanf("%d %d %d %d", &p[i][0], &p[i][1], &p[i][2], &p[i][3]);
|
||||
LL ans = 0;
|
||||
if(w >= m)ans = (LL)w * h - sloveX(n1, m);
|
||||
if(m != 1 && h >= m) ans += (LL)w * h - sloveY(n1, m);
|
||||
printf("%I64d\n", ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
#include<iostream>
|
||||
#include<cctype>
|
||||
#include<cstring>
|
||||
#include<cstdio>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
char s[4100];
|
||||
while(gets(s))
|
||||
{
|
||||
int len=strlen(s);
|
||||
int line;
|
||||
if(len%16==0)line=len/16;
|
||||
else line=len/16+1;
|
||||
for(int i=0;i<line;i++)
|
||||
{
|
||||
int count=0;
|
||||
printf("%.4x: ",i<<4);
|
||||
for(int j=i*16;s[j]&&j<i*16+16;j++)
|
||||
{
|
||||
printf("%.2x",s[j]);
|
||||
count+=2;
|
||||
if(j&1){putchar(' ');count++;}
|
||||
}
|
||||
while(count++<40)putchar(' ');
|
||||
for(int j=i*16;s[j]&&j<i*16+16;j++)
|
||||
{
|
||||
if(islower(s[j]))
|
||||
putchar(toupper(s[j]));
|
||||
else
|
||||
putchar(tolower(s[j]));
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
using namespace std;
|
||||
const int N=1005;
|
||||
const int mod=1000000007;
|
||||
char str[N];
|
||||
int dp[N][N],sum[N][N];
|
||||
int main()
|
||||
{
|
||||
while(scanf("%s",str+2)!=EOF)
|
||||
{
|
||||
memset(dp,0,sizeof(dp));
|
||||
memset(sum,0,sizeof(sum));
|
||||
int n=(int)strlen(str+2);
|
||||
dp[1][1]=sum[1][1]=1;
|
||||
for(int i=2;i<=n+1;i++)
|
||||
{
|
||||
for(int j=1;j<=i;j++)
|
||||
{
|
||||
if(str[i]=='I'||str[i]=='?') dp[i][j]=(dp[i][j]+sum[i-1][j-1])%mod;
|
||||
if(str[i]=='D'||str[i]=='?')
|
||||
{
|
||||
int tmp=((sum[i-1][i-1]-sum[i-1][j-1])%mod+mod)%mod;
|
||||
dp[i][j]=(dp[i][j]+tmp)%mod;
|
||||
}
|
||||
sum[i][j]=(dp[i][j]+sum[i][j-1])%mod;
|
||||
}
|
||||
}
|
||||
printf("%d\n",sum[n+1][n+1]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <queue>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#define eps 1e-5
|
||||
#define MAXN 111111
|
||||
#define MAXM 1111111
|
||||
#define INF 1000000008
|
||||
using namespace std;
|
||||
int fa[55555];
|
||||
int ans[15];
|
||||
struct P
|
||||
{
|
||||
int a, b, c, x, y;
|
||||
char op[15];
|
||||
}p[55555];
|
||||
int vis[55555];
|
||||
int find(int x)
|
||||
{
|
||||
if(fa[x] == -1) return x;
|
||||
int t = find(fa[x]);
|
||||
fa[x] = t;
|
||||
return t;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n, m, q;
|
||||
while(scanf("%d%d%d", &n, &m, &q) != EOF)
|
||||
{
|
||||
memset(ans, 0, sizeof(ans));
|
||||
for(int i = 0; i < q; i++)
|
||||
{
|
||||
scanf("%s%d%d%d%d", p[i].op, &p[i].x, &p[i].y, &p[i].a, &p[i].b);
|
||||
if(p[i].op[0] == 'R') scanf("%d", &p[i].c);
|
||||
}
|
||||
for(int k = 0; k < n; k++)
|
||||
{
|
||||
for(int i = 0; i <= m; i++) fa[i] = -1, vis[i] = 0;
|
||||
for(int i = q - 1; i >= 0; i--)
|
||||
{
|
||||
int co = p[i].b, a, b;
|
||||
if(p[i].op[0] == 'R')
|
||||
{
|
||||
co = p[i].c;
|
||||
if(k < p[i].x || k >= p[i].x + p[i].a) continue;
|
||||
a = p[i].y;
|
||||
b = p[i].y + p[i].b - 1;
|
||||
}
|
||||
else if(p[i].op[0] == 'C')
|
||||
{
|
||||
if(abs(k - p[i].x) > p[i].a) continue;
|
||||
int tmp = p[i].a * p[i].a - (k - p[i].x) * (k - p[i].x);
|
||||
int tp = (int)(sqrt(tmp));
|
||||
a = p[i].y - tp;
|
||||
b = p[i].y + tp;
|
||||
}
|
||||
else if(p[i].op[0] == 'D')
|
||||
{
|
||||
if(abs(k - p[i].x) > p[i].a) continue;
|
||||
int tmp = p[i].a - abs(k - p[i].x);
|
||||
a = p[i].y - tmp;
|
||||
b = p[i].y + tmp;
|
||||
}
|
||||
else if(p[i].op[0] == 'T')
|
||||
{
|
||||
if(k - p[i].x >= (p[i].a + 1) / 2 || k < p[i].x) continue;
|
||||
int tmp = (p[i].a - 1) / 2 - (k - p[i].x);
|
||||
a = p[i].y - tmp;
|
||||
b = p[i].y + tmp;
|
||||
}
|
||||
a = max(a, 0);
|
||||
b = min(m - 1, b);
|
||||
int fx = find(a), fy;
|
||||
for(int j = b; j >= a; j = fy - 1)
|
||||
{
|
||||
fy = find(j);
|
||||
if(!vis[fy]) ans[co]++;
|
||||
vis[fy] = 1;
|
||||
if(fx != fy) fa[fy] = fx;
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i = 1; i < 9; i++) printf("%d ", ans[i]);
|
||||
printf("%d\n", ans[9]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
#include<cstring>
|
||||
#include<cmath>
|
||||
#define eps 1e-10
|
||||
#define N 100005
|
||||
#define inf 1<<20
|
||||
#define zero(a) (fabs(a)<eps)
|
||||
#define lson (step<<1)
|
||||
#define rson (step<<1|1)
|
||||
using namespace std;
|
||||
struct Trie{
|
||||
int next[4],fail;
|
||||
int end;
|
||||
void Init(){next[0]=next[1]=next[2]=next[3]=0;fail=end=0;}
|
||||
}tree[N];
|
||||
int dp[2][1005][1<<10];
|
||||
char str[105];
|
||||
int val[105],tot;
|
||||
int id(char ch){
|
||||
if(ch=='A') return 0;
|
||||
if(ch=='T') return 1;
|
||||
if(ch=='G') return 2;
|
||||
return 3;
|
||||
}
|
||||
void Insert(char *s,int len,int k){
|
||||
int p=0;
|
||||
for(int i=0;i<len;i++){
|
||||
int q=id(s[i]);
|
||||
if(tree[p].next[q]==0){
|
||||
tree[++tot].Init();
|
||||
tree[p].next[q]=tot;
|
||||
}
|
||||
p=tree[p].next[q];
|
||||
}
|
||||
tree[p].end|=(1<<k);
|
||||
}
|
||||
void Bulid_Fail(){
|
||||
int que[N];
|
||||
int head=0,tail=0;
|
||||
que[tail++]=0;
|
||||
while(head<tail){
|
||||
int p=que[head++];
|
||||
for(int i=0;i<4;i++){
|
||||
if(tree[p].next[i]==0)
|
||||
tree[p].next[i]=tree[tree[p].fail].next[i];
|
||||
else{
|
||||
int q=tree[p].next[i];
|
||||
if(p)
|
||||
tree[q].fail=tree[tree[p].fail].next[i];
|
||||
tree[q].end|=tree[tree[q].fail].end;
|
||||
que[tail++]=q;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int n,m;
|
||||
int get(int state){
|
||||
int ans=0;
|
||||
for(int i=0;i<n;i++)
|
||||
if(state&(1<<i))
|
||||
ans+=val[i];
|
||||
return ans;
|
||||
}
|
||||
void DP(){
|
||||
memset(dp,0,sizeof(dp));
|
||||
dp[0][0][0]=1;
|
||||
for(int i=1;i<=m;i++){
|
||||
memset(dp[i&1],0,sizeof(dp[i&1]));
|
||||
for(int j=0;j<=tot;j++){
|
||||
for(int k=0;k<4;k++){
|
||||
for(int r=0;r<(1<<n);r++){
|
||||
if(dp[(i+1)&1][j][r])
|
||||
dp[i&1][tree[j].next[k]][r|tree[tree[j].next[k]].end]=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int ans=-inf;
|
||||
for(int j=0;j<(1<<n);j++)
|
||||
for(int i=0;i<=tot;i++)
|
||||
if(dp[m&1][i][j]){
|
||||
ans=max(ans,get(j));
|
||||
break;
|
||||
}
|
||||
if(ans<0) puts("No Rabbit after 2012!");
|
||||
else printf("%d\n",ans);
|
||||
}
|
||||
int main(){
|
||||
while(scanf("%d%d",&n,&m)!=EOF){
|
||||
tree[0].Init();tot=0;
|
||||
for(int i=0;i<n;i++){
|
||||
scanf("%s%d",str,&val[i]);
|
||||
Insert(str,strlen(str),i);
|
||||
}
|
||||
Bulid_Fail();
|
||||
DP();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,162 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<algorithm>
|
||||
#include<iostream>
|
||||
#include<math.h>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
#define LL __int64
|
||||
#define MOD 1000000007
|
||||
#define maxn 2000007
|
||||
LL yu[maxn];
|
||||
struct matrix
|
||||
{
|
||||
LL mat[7][7];
|
||||
matrix()
|
||||
{
|
||||
memset(mat,0,sizeof(mat));
|
||||
}
|
||||
friend matrix operator *(matrix A,matrix B)
|
||||
{
|
||||
int i,j,k;
|
||||
matrix C;
|
||||
for(i=1; i<=6; i++)
|
||||
{
|
||||
for(j=1; j<=6; j++)
|
||||
{
|
||||
for(k=1; k<=6; k++)
|
||||
{
|
||||
C.mat[i][j]=(C.mat[i][j]+(A.mat[i][k]*B.mat[k][j])%MOD)%MOD;
|
||||
}
|
||||
C.mat[i][j]=C.mat[i][j]%MOD;
|
||||
}
|
||||
}
|
||||
return C;
|
||||
}
|
||||
} ONE,AA[30];
|
||||
matrix powmul(matrix A,LL k)
|
||||
{
|
||||
matrix B;
|
||||
for(int i=1; i<=6; i++)B.mat[i][i]=1;
|
||||
int l=1;
|
||||
while(k)
|
||||
{
|
||||
if(k&1)B=B*AA[l];
|
||||
l++;
|
||||
k>>=1;
|
||||
}
|
||||
return B;
|
||||
}
|
||||
vector<int>vec;
|
||||
void init()
|
||||
{
|
||||
int i,j;
|
||||
ONE.mat[1][1]=1;ONE.mat[1][2]=1;ONE.mat[1][3]=4;
|
||||
ONE.mat[1][4]=6;ONE.mat[1][5]=4;ONE.mat[1][6]=1;
|
||||
ONE.mat[2][1]=0;ONE.mat[2][2]=1;ONE.mat[2][3]=4;
|
||||
ONE.mat[2][4]=6;ONE.mat[2][5]=4;ONE.mat[2][6]=1;
|
||||
ONE.mat[3][1]=0;ONE.mat[3][2]=0;ONE.mat[3][3]=1;
|
||||
ONE.mat[3][4]=3;ONE.mat[3][5]=3;ONE.mat[3][6]=1;
|
||||
ONE.mat[4][4]=1;ONE.mat[4][5]=2;ONE.mat[4][6]=1;
|
||||
ONE.mat[5][4]=0;ONE.mat[5][5]=1;ONE.mat[5][6]=1;
|
||||
ONE.mat[6][6]=1;
|
||||
yu[0]=0;
|
||||
yu[1]=1;
|
||||
LL x;
|
||||
for(i=2;i<maxn;i++)
|
||||
{
|
||||
x=i%MOD;
|
||||
x=x*i;
|
||||
x=x%MOD;
|
||||
x=x*i;
|
||||
x=x%MOD;
|
||||
x=x*i;
|
||||
x=x%MOD;
|
||||
yu[i]=yu[i-1]+x;
|
||||
yu[i]=yu[i]%MOD;
|
||||
}
|
||||
AA[1]=ONE;
|
||||
for(i=2;i<30;i++)AA[i]=AA[i-1]*AA[i-1];
|
||||
}
|
||||
matrix A;
|
||||
LL kan(LL x)
|
||||
{
|
||||
if(x<maxn)return yu[x];
|
||||
matrix OP;
|
||||
for(int i=1;i<=6;i++)OP.mat[i][1]=1;
|
||||
A=powmul(ONE,x-1);
|
||||
OP=A*OP;
|
||||
return OP.mat[1][1];
|
||||
}
|
||||
LL pan(LL n,LL x)
|
||||
{
|
||||
if(x>=n)return 0;
|
||||
LL p=1;
|
||||
LL ns=4;
|
||||
while(ns--)
|
||||
{
|
||||
p=p*x;
|
||||
p=p%MOD;
|
||||
}
|
||||
ns=(n-1)/x;
|
||||
p=p*kan(ns);
|
||||
p=p%MOD;
|
||||
return p;
|
||||
}
|
||||
void dos(LL n)
|
||||
{
|
||||
LL p=1;
|
||||
LL ans=0;
|
||||
int i,j,leap;
|
||||
LL m=n;
|
||||
vec.clear();
|
||||
for(i=2;i*i<=n;i++)
|
||||
{
|
||||
if(n%i==0)
|
||||
{
|
||||
vec.push_back(i);
|
||||
}
|
||||
while(n%i==0)n=n/i;
|
||||
}
|
||||
if(n!=1)vec.push_back(n);
|
||||
n=m;
|
||||
int t=1<<(vec.size());
|
||||
for(i=1;i<t;i++)
|
||||
{
|
||||
leap=0;
|
||||
p=1;
|
||||
for(j=0;j<vec.size();j++)
|
||||
{
|
||||
if(i&(1<<j))
|
||||
{
|
||||
leap++;
|
||||
p=p*vec[j];
|
||||
}
|
||||
}
|
||||
leap=leap%2;
|
||||
if(leap)ans+=pan(n,p);
|
||||
else ans-=pan(n,p);
|
||||
ans=(ans+MOD)%MOD;
|
||||
}
|
||||
ans=kan(n-1)-ans;
|
||||
ans=(ans+MOD)%MOD;
|
||||
printf("%I64d\n",ans);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
LL n;
|
||||
init();
|
||||
scanf("%d",&T);
|
||||
while(T--)
|
||||
{
|
||||
scanf("%I64d",&n);
|
||||
if(n==1)
|
||||
{
|
||||
cout<<"0"<<endl;
|
||||
continue;
|
||||
}
|
||||
dos(n);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,123 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
#define MAXN 210
|
||||
int nx, ny, g[MAXN][MAXN], sy[MAXN], cx[MAXN], cy[MAXN];
|
||||
int path(int u) {
|
||||
for (int v = 1; v <= ny; v++)if (g[u][v] && !sy[v]) {
|
||||
sy[v] = 1;
|
||||
if (!cy[v] || path(cy[v])) {
|
||||
cx[u] = v;
|
||||
cy[v] = u;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int MaximumMatch() {
|
||||
int i, ret = 0;
|
||||
memset(cx, 0, sizeof (cx));
|
||||
memset(cy, 0, sizeof (cy));
|
||||
for (i = 1; i <= nx; i++)if (!cx[i]) {
|
||||
memset(sy, 0, sizeof (sy));
|
||||
ret += path(i);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
char c[2010];
|
||||
int s[2010][2010], w, h, n, m;
|
||||
int sum(int x0, int y0, int x1, int y1){
|
||||
x0--, y0--;
|
||||
return s[x1][y1] - s[x0][y1] - s[x1][y0] + s[x0][y0];
|
||||
}
|
||||
int sumx[2010], sumy[2010];
|
||||
int pd(int a, int b){
|
||||
int i, j, ans = 0, t;
|
||||
for (i = 0; i < n; i++){
|
||||
for (j = 0; j < m; j++){
|
||||
if (sum((a + b) * i + b + 1, (a + b) * j + b + 1, (a + b) * i + b + a, (a + b) * j + b + a) != a * a){
|
||||
ans++;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (i = 0; i <= n; i++){
|
||||
sumx[i] = sum((a + b) * i + 1, 1, (a + b) * i + b, w);
|
||||
}
|
||||
for (j = 0; j <= m; j++){
|
||||
sumy[j] = sum(1, (a + b) * j + 1, h, (a + b) * j + b);
|
||||
}
|
||||
for (i = 0; i <= n; i++){
|
||||
for (j = 0; j <= m; j++){
|
||||
g[i + 1][j + 1] = 0;
|
||||
t = sum((a + b) * i + 1, (a + b) * j + 1, (a + b) * i + b, (a + b) * j + b);
|
||||
sumx[i] -= t;
|
||||
sumy[j] -= t;
|
||||
}
|
||||
}
|
||||
for (i = 0; i <= n; i++){
|
||||
if (sumx[i]) ans++;
|
||||
}
|
||||
for (j = 0; j <= m; j++){
|
||||
if (sumy[j]) ans++;
|
||||
}
|
||||
for (i = 0; i <= n; i++){
|
||||
if (sumx[i]) continue;
|
||||
for (j = 0; j <= m; j++){
|
||||
if (sumy[j]) continue;
|
||||
t = sum((a + b) * i + 1, (a + b) * j + 1, (a + b) * i + b, (a + b) * j + b);
|
||||
if (t) g[i + 1][j + 1] = 1;
|
||||
}
|
||||
}
|
||||
nx = n + 1;
|
||||
ny = m + 1;
|
||||
return ans + MaximumMatch();
|
||||
}
|
||||
int main(){
|
||||
int t, i, j, x, y, ans;
|
||||
while (scanf("%d%d%d%d%d", &h, &w, &n, &m, &t) != EOF){
|
||||
for (i = 1; i <= h; i++){
|
||||
scanf("%s", c);
|
||||
for (j = 0; c[j]; j++){
|
||||
s[i][j + 1] = s[i][j];
|
||||
if (c[j] == '1'){
|
||||
s[i][j + 1]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (i = 1; i <= h; i++){
|
||||
for (j = 1; j <= w; j++){
|
||||
s[i][j] += s[i - 1][j];
|
||||
}
|
||||
}
|
||||
if (n != m){
|
||||
y = (h * m - w * n) / (m - n);
|
||||
x = (h * (m + 1) - w * (n + 1)) / (n - m);
|
||||
if (x <= 0 || y <= 0 || y > x || n * x + y * (n + 1) != h || m * x + y * (m + 1) != w){
|
||||
printf("-1\n");
|
||||
continue;
|
||||
}
|
||||
printf("%d\n", pd(x, y) * t);
|
||||
}else{
|
||||
if (h != w){
|
||||
printf("-1\n");
|
||||
continue;
|
||||
}
|
||||
ans = 1<<30;
|
||||
for (x = 1; n * x < h; x++){
|
||||
if ((h - n * x) % (n + 1) == 0){
|
||||
y = (h - n * x) / (n + 1);
|
||||
if (x >= y){
|
||||
ans = min(ans, pd(x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ans == (1<<30)){
|
||||
printf("-1\n");
|
||||
}else{
|
||||
printf("%d\n", ans * t);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
#include<stdio.h>
|
||||
#include<cstring>
|
||||
#include<algorithm>
|
||||
#define I(x) scanf("%d",&x)
|
||||
using namespace std;
|
||||
int main(){
|
||||
int n,a,sum,t,b,ca=0;
|
||||
I(t);
|
||||
while(t--){
|
||||
I(n);
|
||||
sum=0;
|
||||
for(int i=0;i<n;i++){
|
||||
I(a);
|
||||
sum+=a;
|
||||
if(i==0) b=a;
|
||||
}
|
||||
printf("Case %d: %.6lf\n",++ca,1.0*b/sum);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,250 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<set>
|
||||
#include<algorithm>
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
struct point{
|
||||
int h,l;
|
||||
friend bool operator <(point p1,point p2){return p1.h<p2.h||(p1.h==p2.h&&p1.l<p2.l);}
|
||||
};
|
||||
int h[]={1,1,1,1,1,2,3,4,5,6,7,8,9,9,9,9,9,8,7,6,5,4,3,2};
|
||||
int l[]={5,7,9,11,13,14,15,16,17,16,15,14,13,11,9,7,5,4,3,2,1,2,3,4};
|
||||
int hMove[]={0,-1,-1,0,1,1},lMove[]={-2,-1,1,2,1,-1};
|
||||
int cas,bnum,wnum;char map[19][19];set<point> s,whs;bool hasmove;
|
||||
int judge(char ch1,char ch2){
|
||||
int i,j,cnt1,cnt2;
|
||||
for(i=0;i<24;i++){
|
||||
if(map[h[i]][l[i]]==ch1){
|
||||
for(j=0;j<6;j++){
|
||||
cnt1=1;cnt2=0;
|
||||
if(map[h[i]+hMove[j]][l[i]+lMove[j]]==0||map[h[i]+hMove[j]][l[i]+lMove[j]]=='.')
|
||||
continue;
|
||||
while(map[h[i]+cnt1*hMove[j]][l[i]+cnt1*lMove[j]]==ch1)
|
||||
cnt1++;
|
||||
if(cnt1>=3) continue;
|
||||
while(map[h[i]+(cnt1+cnt2)*hMove[j]][l[i]+(cnt1+cnt2)*lMove[j]]==ch2)
|
||||
cnt2++;
|
||||
if(cnt2>cnt1){
|
||||
if(map[h[i]-hMove[j]][l[i]-lMove[j]]==0){
|
||||
if(ch1=='B'&&bnum-cnt1<9) return 2;
|
||||
if(ch1=='W'&&wnum-cnt1<9) return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int wmove1(int h,int l){
|
||||
int j,flag;bool idx;
|
||||
for(j=0;j<6;j++){
|
||||
idx=false;
|
||||
if(map[h+hMove[j]][l+lMove[j]]=='.'){
|
||||
swap(map[h][l],map[h+hMove[j]][l+lMove[j]]);
|
||||
idx=true;hasmove=true;
|
||||
}
|
||||
if(idx){
|
||||
flag=judge('W','B');
|
||||
swap(map[h][l],map[h+hMove[j]][l+lMove[j]]);
|
||||
if(flag==1) return flag;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int wmove2(int h1,int l1,int h2,int l2){
|
||||
int j,flag;int idx;
|
||||
for(j=0;j<6;j++){
|
||||
idx=0;
|
||||
if(map[h1+hMove[j]][l1+lMove[j]]=='.'&&map[h2+hMove[j]][l2+lMove[j]]=='.'){
|
||||
swap(map[h1][l1],map[h1+hMove[j]][l1+lMove[j]]);
|
||||
swap(map[h2][l2],map[h2+hMove[j]][l2+lMove[j]]);
|
||||
idx=1;
|
||||
}
|
||||
if(h2-h1==hMove[j]&&l2-l1==lMove[j]){
|
||||
if(map[h2+hMove[j]][l2+lMove[j]]=='.'){
|
||||
swap(map[h1][l1],map[h2+hMove[j]][l2+lMove[j]]);
|
||||
idx=2;
|
||||
}
|
||||
if(map[h2+hMove[j]][l2+lMove[j]]=='B'&&map[h2+2*hMove[j]][l2+2*lMove[j]]=='.'){
|
||||
swap(map[h2+hMove[j]][l2+lMove[j]],map[h2+2*hMove[j]][l2+2*lMove[j]]);
|
||||
swap(map[h1][l1],map[h2+hMove[j]][l2+lMove[j]]);
|
||||
idx=3;
|
||||
}
|
||||
if(map[h2+hMove[j]][l2+lMove[j]]=='B'&&map[h2+2*hMove[j]][l2+2*lMove[j]]==0){
|
||||
map[h2+hMove[j]][l2+lMove[j]]='W';
|
||||
map[h1][l1]='.';
|
||||
idx=4;
|
||||
}
|
||||
}
|
||||
if(idx) hasmove=true;
|
||||
if(idx==1){
|
||||
flag=judge('W','B');
|
||||
swap(map[h1][l1],map[h1+hMove[j]][l1+lMove[j]]);
|
||||
swap(map[h2][l2],map[h2+hMove[j]][l2+lMove[j]]);
|
||||
if(flag==1) return flag;
|
||||
}
|
||||
if(idx==2){
|
||||
flag=judge('W','B');
|
||||
swap(map[h1][l1],map[h2+hMove[j]][l2+lMove[j]]);
|
||||
if(flag==1) return flag;
|
||||
}
|
||||
if(idx==3){
|
||||
flag=judge('W','B');
|
||||
swap(map[h1][l1],map[h2+hMove[j]][l2+lMove[j]]);
|
||||
swap(map[h2+hMove[j]][l2+lMove[j]],map[h2+2*hMove[j]][l2+2*lMove[j]]);
|
||||
if(flag==1) return flag;
|
||||
}
|
||||
if(idx==4){
|
||||
flag=judge('W','B');
|
||||
map[h2+hMove[j]][l2+lMove[j]]='B';
|
||||
map[h1][l1]='W';
|
||||
if(flag==1) return flag;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int wmove3(int h1,int l1,int h2,int l2,int h3,int l3)
|
||||
{
|
||||
int j,flag;int idx;
|
||||
for(j=0;j<6;j++){
|
||||
idx=0;
|
||||
if(map[h1+hMove[j]][l1+lMove[j]]=='.'&&map[h2+hMove[j]][l2+lMove[j]]=='.'
|
||||
&&map[h3+hMove[j]][l3+lMove[j]]=='.'){
|
||||
swap(map[h1][l1],map[h1+hMove[j]][l1+lMove[j]]);
|
||||
swap(map[h2][l2],map[h2+hMove[j]][l2+lMove[j]]);
|
||||
swap(map[h3][l3],map[h3+hMove[j]][l3+lMove[j]]);
|
||||
idx=1;
|
||||
}
|
||||
if(h2-h1==hMove[j]&&l2-l1==lMove[j]){
|
||||
if(map[h3+hMove[j]][l3+lMove[j]]=='.'){
|
||||
swap(map[h1][l1],map[h3+hMove[j]][l3+lMove[j]]);
|
||||
idx=2;
|
||||
}
|
||||
if(map[h3+hMove[j]][l3+lMove[j]]=='B'&&map[h3+2*hMove[j]][l3+2*lMove[j]]=='.'){
|
||||
swap(map[h3+hMove[j]][l3+lMove[j]],map[h3+2*hMove[j]][l3+2*lMove[j]]);
|
||||
swap(map[h1][l1],map[h3+hMove[j]][l3+lMove[j]]);
|
||||
idx=3;
|
||||
}
|
||||
if(map[h3+hMove[j]][l3+lMove[j]]=='B'&&map[h3+2*hMove[j]][l3+2*lMove[j]]==0){
|
||||
map[h1][l1]='.';
|
||||
map[h3+hMove[j]][l3+lMove[j]]='W';
|
||||
idx=4;
|
||||
}
|
||||
if(map[h3+hMove[j]][l3+lMove[j]]=='B'&&map[h3+2*hMove[j]][l3+2*lMove[j]]=='B'
|
||||
&&map[h3+3*hMove[j]][l3+3*lMove[j]]=='.'){
|
||||
swap(map[h3+hMove[j]][l3+lMove[j]],map[h3+3*hMove[j]][l3+3*lMove[j]]);
|
||||
swap(map[h1][l1],map[h3+hMove[j]][l3+lMove[j]]);
|
||||
idx=5;
|
||||
}
|
||||
if(map[h3+hMove[j]][l3+lMove[j]]=='B'&&map[h3+2*hMove[j]][l3+2*lMove[j]]=='B'
|
||||
&&map[h3+3*hMove[j]][l3+3*lMove[j]]==0){
|
||||
map[h1][l1]='.';
|
||||
map[h2][l2]='.';
|
||||
map[h3+hMove[j]][l3+lMove[j]]='W';
|
||||
map[h3+2*hMove[j]][l3+2*lMove[j]]='W';
|
||||
idx=6;
|
||||
}
|
||||
}
|
||||
if(idx) hasmove=true;
|
||||
if(idx==1){
|
||||
flag=judge('W','B');
|
||||
swap(map[h1][l1],map[h1+hMove[j]][l1+lMove[j]]);
|
||||
swap(map[h2][l2],map[h2+hMove[j]][l2+lMove[j]]);
|
||||
swap(map[h3][l3],map[h3+hMove[j]][l3+lMove[j]]);
|
||||
if(flag==1) return flag;
|
||||
}
|
||||
if(idx==2){
|
||||
flag=judge('W','B');
|
||||
swap(map[h1][l1],map[h3+hMove[j]][l3+lMove[j]]);
|
||||
if(flag==1) return flag;
|
||||
}
|
||||
if(idx==3){
|
||||
flag=judge('W','B');
|
||||
swap(map[h1][l1],map[h3+hMove[j]][l3+lMove[j]]);
|
||||
swap(map[h3+hMove[j]][l3+lMove[j]],map[h3+2*hMove[j]][l3+2*lMove[j]]);
|
||||
if(flag==1) return flag;
|
||||
}
|
||||
if(idx==4){
|
||||
flag=judge('W','B');
|
||||
map[h3+hMove[j]][l3+lMove[j]]='B';
|
||||
map[h1][l1]='W';
|
||||
if(flag==1) return flag;
|
||||
}
|
||||
if(idx==5){
|
||||
flag=judge('W','B');
|
||||
swap(map[h1][l1],map[h3+hMove[j]][l3+lMove[j]]);
|
||||
swap(map[h3+hMove[j]][l3+lMove[j]],map[h3+3*hMove[j]][l3+3*lMove[j]]);
|
||||
if(flag==1) return flag;
|
||||
}
|
||||
if(idx==6){
|
||||
flag=judge('W','B');
|
||||
map[h1][l1]='W';
|
||||
map[h2][l2]='W';
|
||||
map[h3+hMove[j]][l3+lMove[j]]='B';
|
||||
map[h3+2*hMove[j]][l3+2*lMove[j]]='B';
|
||||
if(flag==1) return flag;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void mycin(){
|
||||
int i;
|
||||
for(i=5;i<=13;i+=2) cin>>map[1][i];
|
||||
for(i=4;i<=14;i+=2) cin>>map[2][i];
|
||||
for(i=3;i<=15;i+=2) cin>>map[3][i];
|
||||
for(i=2;i<=16;i+=2) cin>>map[4][i];
|
||||
for(i=1;i<=17;i+=2) cin>>map[5][i];
|
||||
for(i=2;i<=16;i+=2) cin>>map[6][i];
|
||||
for(i=3;i<=15;i+=2) cin>>map[7][i];
|
||||
for(i=4;i<=14;i+=2) cin>>map[8][i];
|
||||
for(i=5;i<=13;i+=2) cin>>map[9][i];
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,j,k=1,flag;point p;
|
||||
set<point>::iterator iter;
|
||||
scanf("%d",&cas);
|
||||
while(cas--){
|
||||
s.clear();whs.clear();hasmove=false;
|
||||
memset(map,0,sizeof(map));
|
||||
mycin();
|
||||
for(i=1,wnum=bnum=0;i<=9;i++){
|
||||
for(j=1;j<=17;j++){
|
||||
if(map[i][j]=='W'){wnum++;p.h=i;p.l=j;whs.insert(p);}
|
||||
if(map[i][j]=='B') bnum++;
|
||||
}
|
||||
}
|
||||
flag=judge('B','W');
|
||||
for(iter=whs.begin();iter!=whs.end()&&flag!=2;iter++){
|
||||
int h1=iter->h,l1=iter->l;
|
||||
flag=wmove1(h1,l1);
|
||||
if(flag==1) break;
|
||||
}
|
||||
for(iter=whs.begin();iter!=whs.end()&&flag!=1&&flag!=2;iter++){
|
||||
for(j=0;j<6&&flag!=1;j++){
|
||||
if(map[iter->h+hMove[j]][iter->l+lMove[j]]=='W'){
|
||||
int h1=iter->h,l1=iter->l,h2=h1+hMove[j],l2=l1+lMove[j];
|
||||
flag=wmove2(h1,l1,h2,l2);
|
||||
if(flag==1) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for(iter=whs.begin();iter!=whs.end()&&flag!=1&&flag!=2;iter++){
|
||||
for(j=0;j<6&&flag!=1;j++){
|
||||
if(map[iter->h+hMove[j]][iter->l+lMove[j]]=='W'&&map[iter->h+2*hMove[j]][iter->l+2*lMove[j]]=='W'){
|
||||
int h1=iter->h,l1=iter->l,h2=h1+hMove[j],l2=l1+lMove[j],h3=h1+2*hMove[j],l3=l1+2*lMove[j];
|
||||
flag=wmove3(h1,l1,h2,l2,h3,l3);
|
||||
if(flag==1) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(hasmove==false&&flag!=2)
|
||||
flag=judge('W','B');
|
||||
printf("Case %d: ",k++);
|
||||
if(flag==0) printf("Black\n");
|
||||
if(flag==1) printf("Draw\n");
|
||||
if(flag==2) printf("White\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,290 @@
|
|||
#include <set>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <iostream>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#define MID(x,y) ( ( x + y ) >> 1 )
|
||||
#define L(x) ( x << 1 )
|
||||
#define R(x) ( x << 1 | 1 )
|
||||
#define FOR(i,s,t) for(int i=(s); i<(t); i++)
|
||||
#define BUG puts("here!!!")
|
||||
#define STOP system("pause")
|
||||
#define file_r(x) freopen(x, "r", stdin)
|
||||
#define file_w(x) freopen(x, "w", stdout)
|
||||
using namespace std;
|
||||
const int MAX = 1000;
|
||||
const double eps = 1e-6;
|
||||
const double inf = 1e50;
|
||||
struct point{
|
||||
double x, y;
|
||||
void get()
|
||||
{
|
||||
scanf("%lf%lf", &x, &y);
|
||||
}
|
||||
};
|
||||
bool dy(double x,double y) { return x > y + eps;}
|
||||
bool xy(double x,double y) { return x < y - eps;}
|
||||
bool dyd(double x,double y) { return x > y - eps;}
|
||||
bool xyd(double x,double y) { return x < y + eps;}
|
||||
bool dd(double x,double y) { return fabs( x - y ) < eps;}
|
||||
double disp2p(point a,point b)
|
||||
{
|
||||
return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );
|
||||
}
|
||||
bool operator==(point a, point b)
|
||||
{
|
||||
return dd(a.x, b.x) && dd(a.y, b.y);
|
||||
}
|
||||
typedef struct NODE{
|
||||
int to;
|
||||
double len;
|
||||
NODE *next;
|
||||
}NODE;
|
||||
NODE *p[MAX],node[MAX*MAX];
|
||||
struct circle{
|
||||
point c;
|
||||
double r;
|
||||
};
|
||||
circle c[MAX];
|
||||
int cou;
|
||||
void init()
|
||||
{
|
||||
cou = 0;
|
||||
memset(p, 0, sizeof(p));
|
||||
}
|
||||
void Add(int from,int to,double len)
|
||||
{
|
||||
node[cou].next = p[from];
|
||||
node[cou].to = to;
|
||||
node[cou].len = len;
|
||||
p[from] = &node[cou++];
|
||||
}
|
||||
queue<int> q;
|
||||
double SPFA_List(int from,int to,int n)
|
||||
{
|
||||
while( !q.empty() ) q.pop();
|
||||
double dis[MAX];
|
||||
bool inq[MAX];
|
||||
for(int i=0; i<n; i++)
|
||||
dis[i] = inf;
|
||||
memset(inq,false,sizeof(inq));
|
||||
dis[from] = 0;
|
||||
q.push(from);
|
||||
inq[from] = 1;
|
||||
while( !q.empty() )
|
||||
{
|
||||
int now = q.front();
|
||||
q.pop();
|
||||
inq[now] = false;
|
||||
NODE *head = p[now];
|
||||
while( head != NULL )
|
||||
{
|
||||
int v = head->to;
|
||||
double len = head->len;
|
||||
if( dy(dis[v], dis[now] + len) )
|
||||
{
|
||||
dis[v] = dis[now] + len;
|
||||
if( !inq[v] )
|
||||
{
|
||||
inq[v] = true;
|
||||
q.push(v);
|
||||
}
|
||||
}
|
||||
head = head->next;
|
||||
}
|
||||
}
|
||||
if( dd(dis[to], inf) ) return -1;
|
||||
return dis[to];
|
||||
}
|
||||
point l2l_inst_p(point u1,point u2,point v1,point v2)
|
||||
{
|
||||
point ans = u1;
|
||||
double t = ((u1.x - v1.x)*(v1.y - v2.y) - (u1.y - v1.y)*(v1.x - v2.x))/
|
||||
((u1.x - u2.x)*(v1.y - v2.y) - (u1.y - u2.y)*(v1.x - v2.x));
|
||||
ans.x += (u2.x - u1.x)*t;
|
||||
ans.y += (u2.y - u1.y)*t;
|
||||
return ans;
|
||||
}
|
||||
void l2c_inst_p(point c,double r,point l1,point l2,point &p1,point &p2)
|
||||
{
|
||||
point p = c;
|
||||
double t;
|
||||
p.x += l1.y - l2.y;
|
||||
p.y += l2.x - l1.x;
|
||||
p = l2l_inst_p(p,c,l1,l2);
|
||||
t = sqrt(r*r - disp2p(p,c)*disp2p(p,c))/disp2p(l1,l2);
|
||||
p1.x = p.x + (l2.x - l1.x)*t;
|
||||
p1.y = p.y + (l2.y - l1.y)*t;
|
||||
p2.x = p.x - (l2.x - l1.x)*t;
|
||||
p2.y = p.y - (l2.y - l1.y)*t;
|
||||
}
|
||||
void c2c_inst_p(point c1,double r1,point c2,double r2,point &p1,point &p2)
|
||||
{
|
||||
point u,v;
|
||||
double t;
|
||||
t = (1 + (r1*r1 - r2*r2)/disp2p(c1,c2)/disp2p(c1,c2))/2;
|
||||
u.x = c1.x + (c2.x - c1.x)*t;
|
||||
u.y = c1.y + (c2.y - c1.y)*t;
|
||||
v.x = u.x + c1.y - c2.y;
|
||||
v.y = u.y - c1.x + c2.x;
|
||||
l2c_inst_p(c1,r1,u,v,p1,p2);
|
||||
}
|
||||
bool c2c_tangent(point a,double r1,point b,double r2)
|
||||
{
|
||||
if( dd(disp2p(a,b),r1+r2) || dd(disp2p(a,b),fabs(r1-r2)) )
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
point c2c_tangent_p(point a,double r1,point b,double r2)
|
||||
{
|
||||
point t;
|
||||
if( dd(disp2p(a,b),r1 + r2) )
|
||||
{
|
||||
t.x = (r1*b.x + r2*a.x)/(r1 + r2);
|
||||
t.y = (r1*b.y + r2*a.y)/(r1 + r2);
|
||||
return t;
|
||||
}
|
||||
t.x = (r1*b.x - r2*a.x)/(r1 - r2);
|
||||
t.y = (r1*b.y - r2*a.y)/(r1 - r2);
|
||||
return t;
|
||||
}
|
||||
point g[MAX];
|
||||
bool f[MAX][MAX];
|
||||
double crossProduct(point a,point b,point c)
|
||||
{
|
||||
return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
|
||||
}
|
||||
double disp2l(point a,point l1,point l2)
|
||||
{
|
||||
return fabs( crossProduct(a,l1,l2) )/disp2p(l1,l2);
|
||||
}
|
||||
bool onSegment(point a, point b, point c)
|
||||
{
|
||||
if( dd(crossProduct(a,b,c),0.0) && dyd(c.x,min(a.x,b.x)) &&
|
||||
xyd(c.x,max(a.x,b.x)) && dyd(c.y,min(a.y,b.y)) && xyd(c.y,max(a.y,b.y)) )
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
bool cmp(point a, point b)
|
||||
{
|
||||
if( dd(a.x, b.x) ) return xy(a.y, b.y);
|
||||
return xy(a.x, b.x);
|
||||
}
|
||||
point tp[MAX];
|
||||
bool check(int cnt, int n)
|
||||
{
|
||||
FOR(i, 1, cnt)
|
||||
{
|
||||
point tt;
|
||||
tt.x = (tp[i].x + tp[i-1].x)/2;
|
||||
tt.y = (tp[i].y + tp[i-1].y)/2;
|
||||
bool f = false;
|
||||
FOR(k, 0, n)
|
||||
if( xyd(disp2p(c[k].c, tt), c[k].r) )
|
||||
{
|
||||
f = true;
|
||||
break;
|
||||
}
|
||||
if( !f )
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
double solve(int n)
|
||||
{
|
||||
int l = 0;
|
||||
int s = 0, t = n-1;
|
||||
FOR(i, 0, n)
|
||||
g[l++] = c[i].c;
|
||||
FOR(i, 0, n)
|
||||
{
|
||||
FOR(k, i+1, n)
|
||||
{
|
||||
if( dy(disp2p(c[i].c, c[k].c), c[i].r + c[k].r)
|
||||
|| c[i].c == c[k].c )
|
||||
continue;
|
||||
if( c2c_tangent(c[i].c, c[i].r, c[k].c, c[k].r) )
|
||||
{
|
||||
point tt = c2c_tangent_p(c[i].c, c[i].r, c[k].c, c[k].r);
|
||||
g[l++] = tt;
|
||||
continue;
|
||||
}
|
||||
point t1, t2;
|
||||
c2c_inst_p(c[i].c, c[i].r, c[k].c, c[k].r, t1, t2);
|
||||
g[l++] = t1;
|
||||
g[l++] = t2;
|
||||
}
|
||||
}
|
||||
memset(f, false, sizeof(f));
|
||||
int tmp[MAX];
|
||||
FOR(i, 0, n)
|
||||
{
|
||||
int cnt = 0;
|
||||
FOR(k, 0, l)
|
||||
if( xyd(disp2p(c[i].c, g[k]), c[i].r) )
|
||||
tmp[cnt++] = k;
|
||||
FOR(k, 0, cnt)
|
||||
FOR(j, k+1, cnt)
|
||||
{
|
||||
int x = tmp[k], y = tmp[j];
|
||||
if( f[x][y] ) continue;
|
||||
double dis = disp2p(g[x], g[y]);
|
||||
Add(x, y, dis);
|
||||
Add(y, x, dis);
|
||||
f[x][y] = f[y][x] = true;
|
||||
}
|
||||
}
|
||||
FOR(i, 0, l)
|
||||
FOR(k, i+1, l)
|
||||
{
|
||||
if( f[i][k] ) continue;
|
||||
int cnt = 0;
|
||||
tp[cnt++] = g[i];
|
||||
tp[cnt++] = g[k];
|
||||
FOR(j, 0, n)
|
||||
if( xyd(disp2l(c[j].c, g[i], g[k]),c[j].r) )
|
||||
{
|
||||
point t1, t2;
|
||||
l2c_inst_p(c[j].c, c[j].r, g[i], g[k], t1, t2);
|
||||
if( onSegment(g[i], g[k], t1) )
|
||||
tp[cnt++] = t1;
|
||||
if( onSegment(g[i], g[k], t2) )
|
||||
tp[cnt++] = t2;
|
||||
}
|
||||
sort(tp, tp+cnt, cmp);
|
||||
if( check(cnt, n) )
|
||||
{
|
||||
Add(i, k, disp2p(g[i], g[k]));
|
||||
Add(k, i, disp2p(g[k], g[i]));
|
||||
}
|
||||
}
|
||||
return SPFA_List(s, t, l);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int ncases, n, ind = 1;
|
||||
scanf("%d", &ncases);
|
||||
while( ncases-- )
|
||||
{
|
||||
scanf("%d", &n);
|
||||
init();
|
||||
FOR(i, 0, n)
|
||||
{
|
||||
c[i].c.get();
|
||||
scanf("%lf", &c[i].r);
|
||||
}
|
||||
double ans = solve(n);
|
||||
printf("Case %d: ",ind++);
|
||||
if( ans < -eps )
|
||||
puts("No such path.");
|
||||
else
|
||||
printf("%.4lf\n", ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
# include<stdio.h>
|
||||
# include<string.h>
|
||||
# define N 600000
|
||||
# define mod 1000000007
|
||||
int n,m;
|
||||
__int64 dp[2][N],sum;
|
||||
char map[15][15][5];
|
||||
int a[200],tag,flag;
|
||||
int s[15];
|
||||
void DFS(int r,int up,int down,int num,int right)
|
||||
{
|
||||
int xx,rr,yy,ll,i;
|
||||
if(num==m)
|
||||
{
|
||||
if(r==0) dp[flag][down]+=tag;
|
||||
else
|
||||
{
|
||||
dp[flag][down]+=dp[(flag+1)%2][up]*tag;
|
||||
dp[flag][down]%=mod;
|
||||
}
|
||||
return;
|
||||
}
|
||||
for(i=1;i<4;i++)
|
||||
if(map[r][num][i]!=map[r][num][0]) break;
|
||||
if(i==4)
|
||||
{
|
||||
tag*=4;
|
||||
xx=a[map[r][num][0]];
|
||||
if(xx==right || right==-1) DFS(r,up*3+xx,down*3+xx,num+1,xx);
|
||||
tag/=4;
|
||||
return;
|
||||
}
|
||||
for(i=0;i<4;i++)
|
||||
{
|
||||
xx=a[map[r][num][i]];
|
||||
rr=a[map[r][num][(i+1)%4]];
|
||||
yy=a[map[r][num][(i+2)%4]];
|
||||
ll=a[map[r][num][(i+3)%4]];
|
||||
if(right==-1 || ll==right) DFS(r,up*3+xx,down*3+yy,num+1,rr);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,j,ncase,t;
|
||||
scanf("%d",&ncase);
|
||||
a['F']=0;
|
||||
a['C']=1;
|
||||
a['R']=2;
|
||||
s[0]=1;
|
||||
for(i=1;i<=12;i++)
|
||||
s[i]=s[i-1]*3;
|
||||
for(t=1;t<=ncase;t++)
|
||||
{
|
||||
scanf("%d%d",&n,&m);
|
||||
for(i=0;i<n;i++)
|
||||
for(j=0;j<m;j++)
|
||||
scanf("%s",map[i][j]);
|
||||
flag=0;
|
||||
memset(dp,0,sizeof(dp));
|
||||
for(i=0;i<n;i++)
|
||||
{
|
||||
tag=1;
|
||||
flag=(flag+1)%2;
|
||||
DFS(i,0,0,0,-1);
|
||||
for(j=0;j<s[m];j++) dp[(flag+1)%2][j]=0;
|
||||
}
|
||||
sum=0;
|
||||
for(i=0;i<s[m];i++)
|
||||
{
|
||||
sum+=dp[flag][i];
|
||||
sum%=mod;
|
||||
}
|
||||
printf("Case %d: %I64d\n",t,sum);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
#include<cstdio>
|
||||
#include<cstdlib>
|
||||
#include<cstring>
|
||||
#include<cmath>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
const int maxn=1510;
|
||||
double a[maxn], d[maxn];
|
||||
void getd()
|
||||
{
|
||||
d[1]=0.5;
|
||||
for(int i=2; i<maxn; i++)
|
||||
d[i]=d[i-1]*(2*i-1)/(2*i);
|
||||
}
|
||||
void geta()
|
||||
{
|
||||
a[0]=1.0;
|
||||
for(int i=1; i<maxn; i++)
|
||||
a[i]=a[i-1]+2*d[i];
|
||||
}
|
||||
int main()
|
||||
{
|
||||
getd();
|
||||
geta();
|
||||
int t, ca=1, n;
|
||||
scanf("%d", &t);
|
||||
while(t-->0)
|
||||
{
|
||||
scanf("%d", &n);
|
||||
printf("Case %d: ", ca++);
|
||||
if(n&1)
|
||||
printf("%.6lf\n", a[n/2]);
|
||||
else
|
||||
printf("%.6lf\n", (a[(n-1)/2]+a[(n+1)/2])*0.5);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,118 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<algorithm>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
#define MAXN 222
|
||||
#define MAXM 2222222
|
||||
#define inf 1<<30
|
||||
struct Edge{
|
||||
int v,cap,cost,next;
|
||||
}edge[MAXM];
|
||||
int n,m,vs,vt,NE;
|
||||
int head[MAXN];
|
||||
void Insert(int u,int v,int cap,int cost)
|
||||
{
|
||||
edge[NE].v=v;
|
||||
edge[NE].cap=cap;
|
||||
edge[NE].cost=cost;
|
||||
edge[NE].next=head[u];
|
||||
head[u]=NE++;
|
||||
edge[NE].v=u;
|
||||
edge[NE].cap=0;
|
||||
edge[NE].cost=-cost;
|
||||
edge[NE].next=head[v];
|
||||
head[v]=NE++;
|
||||
}
|
||||
int dist[MAXN],pre[MAXN],cur[MAXN];
|
||||
bool mark[MAXN];
|
||||
bool spfa(int vs,int vt)
|
||||
{
|
||||
memset(mark,false,sizeof(mark));
|
||||
fill(dist,dist+MAXN-1,inf);
|
||||
dist[vs]=0;
|
||||
queue<int>que;
|
||||
que.push(vs);
|
||||
while(!que.empty()){
|
||||
int u=que.front();
|
||||
que.pop();
|
||||
mark[u]=false;
|
||||
for(int i=head[u];i!=-1;i=edge[i].next){
|
||||
int v=edge[i].v,cost=edge[i].cost;
|
||||
if(edge[i].cap>0&&dist[u]+cost<dist[v]){
|
||||
dist[v]=cost+dist[u];
|
||||
pre[v]=u;
|
||||
cur[v]=i;
|
||||
if(!mark[v]){
|
||||
mark[v]=true;
|
||||
que.push(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return dist[vt]<inf;
|
||||
}
|
||||
int MinCostFlow(int vs,int vt)
|
||||
{
|
||||
int flow=0,cost=0;
|
||||
while(spfa(vs,vt)){
|
||||
int aug=inf;
|
||||
for(int u=vt;u!=vs;u=pre[u]){
|
||||
aug=min(aug,edge[cur[u]].cap);
|
||||
}
|
||||
flow+=aug;cost+=dist[vt]*aug;
|
||||
for(int u=vt;u!=vs;u=pre[u]){
|
||||
edge[cur[u]].cap-=aug;
|
||||
edge[cur[u]^1].cap+=aug;
|
||||
}
|
||||
}
|
||||
return cost;
|
||||
}
|
||||
int In[MAXN],Out[MAXN];
|
||||
bool Judge()
|
||||
{
|
||||
for(int i=head[vs];i!=-1;i=edge[i].next){
|
||||
int cap=edge[i].cap;
|
||||
if(cap>0)return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int s,t,u,v,a,b,sum,cost,T=1,_case;
|
||||
scanf("%d",&_case);
|
||||
while(_case--){
|
||||
scanf("%d%d%d%d",&n,&m,&s,&t);
|
||||
NE=0;
|
||||
memset(head,-1,sizeof(head));
|
||||
memset(In,0,sizeof(In));
|
||||
memset(Out,0,sizeof(Out));
|
||||
sum=0;
|
||||
while(m--){
|
||||
scanf("%d%d%d%d",&u,&v,&a,&b);
|
||||
if(a<=b){
|
||||
Insert(v,u,1,b-a);
|
||||
In[v]++,Out[u]++;
|
||||
sum+=a;
|
||||
}else {
|
||||
Insert(u,v,1,a-b);
|
||||
sum+=b;
|
||||
}
|
||||
}
|
||||
In[s]++;
|
||||
Out[t]++;
|
||||
vs=0,vt=n+1;
|
||||
for(int i=1;i<=n;i++){
|
||||
if(In[i]>Out[i])Insert(vs,i,(In[i]-Out[i]),0);
|
||||
else if(In[i]<Out[i])Insert(i,vt,(Out[i]-In[i]),0);
|
||||
}
|
||||
cost=MinCostFlow(vs,vt);
|
||||
printf("Case %d: ",T++);
|
||||
if(Judge()){
|
||||
printf("%d\n",sum+cost);
|
||||
}else
|
||||
puts("impossible");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,118 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#define LEN 25
|
||||
#define N 10
|
||||
using namespace std;
|
||||
bool g[N][N];
|
||||
char name[N][LEN];
|
||||
int n;
|
||||
int p1[N],p2[N];
|
||||
void change(int *ans)
|
||||
{
|
||||
int x,y,OK=0;
|
||||
for(int i=0; i<n; i++)
|
||||
{
|
||||
x=ans[i];
|
||||
y=p1[i];
|
||||
if(strcmp(name[x],name[y]) < 0)
|
||||
break;
|
||||
else if(strcmp(name[y],name[x]) < 0)
|
||||
{
|
||||
OK=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(OK)
|
||||
for(int i=0; i<n; i++)
|
||||
ans[i]=p1[i];
|
||||
}
|
||||
bool fight()
|
||||
{
|
||||
int i=0,j=0,x,y;
|
||||
while(i<n && j<n)
|
||||
{
|
||||
x=p1[i];
|
||||
y=p2[j];
|
||||
if(g[y][x]) i++;
|
||||
else j++;
|
||||
}
|
||||
return i<n;
|
||||
}
|
||||
void solve(int Case)
|
||||
{
|
||||
for(int i=0; i<n; i++) p1[i]=p2[i]=i;
|
||||
int ans[N],FIND,OK;
|
||||
FIND=0;
|
||||
do
|
||||
{
|
||||
sort(p2,p2+n);
|
||||
OK=1;
|
||||
do
|
||||
{
|
||||
if(!fight())
|
||||
{
|
||||
OK=0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
while(next_permutation(p2,p2+n));
|
||||
if(OK)
|
||||
{
|
||||
if(!FIND)
|
||||
{
|
||||
FIND=1;
|
||||
for(int i=0; i<n; i++)
|
||||
ans[i]=p1[i];
|
||||
}
|
||||
else
|
||||
change(ans);
|
||||
}
|
||||
}
|
||||
while(next_permutation(p1,p1+n));
|
||||
if(!FIND)
|
||||
printf("Case %d: No\n",Case);
|
||||
else
|
||||
{
|
||||
printf("Case %d: Yes\n",Case);
|
||||
for(int i=0; i<n; i++)
|
||||
{
|
||||
if(i) printf(" ");
|
||||
printf("%s",name[ans[i]]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
void input()
|
||||
{
|
||||
scanf("%d",&n);
|
||||
for(int i=0; i<n; i++)
|
||||
scanf("%s",name[i]);
|
||||
memset(g,0,sizeof(g));
|
||||
for(int i=0; i<n; i++)
|
||||
{
|
||||
int m;
|
||||
char tmp[LEN];
|
||||
scanf("%d",&m);
|
||||
for(int j=0; j<m; j++)
|
||||
{
|
||||
scanf("%s",tmp);
|
||||
for(int k=0; k<n; k++) if(!strcmp(name[k],tmp))
|
||||
{
|
||||
g[i][k]=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T,t;
|
||||
scanf("%d",&T);
|
||||
for(t=1; t<=T; t++)
|
||||
{
|
||||
input();
|
||||
solve(t);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,193 @@
|
|||
#include<stdio.h>
|
||||
#include<memory.h>
|
||||
#include <climits>
|
||||
#include<string.h>
|
||||
#include<stdlib.h>
|
||||
#define N 1024
|
||||
#define M 1024*110
|
||||
int l[M], r[M], d[M], u[M], col[M], row[M], o[N], res[N], s[N],head[M];
|
||||
int node=0,count;
|
||||
int map[10][10],visit[10][10],a[10][10];
|
||||
int cmp(const void *a,const void *b)
|
||||
{
|
||||
return *(int*)a-*(int*)b;
|
||||
}
|
||||
inline void addnode(int &x)
|
||||
{
|
||||
++x;
|
||||
r[x] = l[x] = u[x] = d[x] = x;
|
||||
}
|
||||
inline void insert_row(int rowx, int x)
|
||||
{
|
||||
r[l[rowx]] = x;
|
||||
l[x] = l[rowx];
|
||||
r[x] = rowx;
|
||||
l[rowx] = x;
|
||||
}
|
||||
inline void insert_col(int colx, int x)
|
||||
{
|
||||
d[u[colx]] = x;
|
||||
u[x] = u[colx];
|
||||
d[x] = colx;
|
||||
u[colx] = x;
|
||||
}
|
||||
void dlx_init(int col)
|
||||
{
|
||||
memset(head,-1,sizeof(head));
|
||||
memset(res,0,sizeof(res));
|
||||
memset(o,0,sizeof(o));
|
||||
memset(s,0,sizeof(s));
|
||||
int i;
|
||||
node=-1;
|
||||
addnode(node);
|
||||
for(i=1;i<=col;i++)
|
||||
{
|
||||
addnode(node);
|
||||
insert_row(0, node);
|
||||
}
|
||||
}
|
||||
inline void insert_node(int x,int y)
|
||||
{
|
||||
s[y]++;
|
||||
addnode(node);
|
||||
row[node]=x;
|
||||
col[node]=y;
|
||||
insert_col(y,node);
|
||||
if(head[x]==-1) head[x]=node;
|
||||
else
|
||||
insert_row(head[x],node);
|
||||
}
|
||||
inline void remove(int c)
|
||||
{
|
||||
l[r[c]] = l[c];
|
||||
r[l[c]] = r[c];
|
||||
int i,j;
|
||||
for (i = d[c]; i != c; i = d[i])
|
||||
for (j = r[i]; j != i; j = r[j])
|
||||
{
|
||||
u[d[j]] = u[j];
|
||||
d[u[j]] = d[j];
|
||||
s[col[j]]--;
|
||||
}
|
||||
}
|
||||
inline void resume(int c)
|
||||
{
|
||||
for (int i = u[c]; i != c; i = u[i])
|
||||
for (int j = l[i]; j != i; j = l[j])
|
||||
{
|
||||
u[d[j]] = j;
|
||||
d[u[j]] = j;
|
||||
s[col[j]]++;
|
||||
}
|
||||
l[r[c]] = c;
|
||||
r[l[c]] = c;
|
||||
}
|
||||
void print()
|
||||
{
|
||||
int i,k=0,j,ans[82]={0};
|
||||
for(i=0;i<81;i++)
|
||||
ans[i]=res[i];
|
||||
qsort(ans,81,sizeof(int),cmp);
|
||||
for(i=0;i<9;i++)
|
||||
for(j=0;j<9;j++)
|
||||
map[i][j]=ans[k++]-(i*9+j)*9;
|
||||
}
|
||||
bool DLX(int deep)
|
||||
{
|
||||
if (r[0] == 0)
|
||||
{
|
||||
count++;
|
||||
if(count==1)
|
||||
print();
|
||||
else
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
int min = INT_MAX, tempc,i,j;
|
||||
for (i = r[0]; i != 0; i = r[i])
|
||||
if (s[i] < min)
|
||||
{
|
||||
min = s[i];
|
||||
tempc = i;
|
||||
}
|
||||
remove(tempc);
|
||||
for (i = d[tempc]; i != tempc; i = d[i])
|
||||
{
|
||||
res[deep] = row[i];
|
||||
for (j = r[i]; j != i; j = r[j]) remove(col[j]);
|
||||
if (DLX(deep + 1)) return true;
|
||||
for (j = l[i]; j != i; j = l[j]) resume(col[j]);
|
||||
}
|
||||
resume(tempc);
|
||||
return false;
|
||||
}
|
||||
inline void dad(int i,int j,int ii)
|
||||
{
|
||||
int x=(i*9+j)*9+ii;
|
||||
insert_node(x,i*9+j+1);
|
||||
insert_node(x,81+i*9+ii);
|
||||
insert_node(x,81*2+j*9+ii);
|
||||
insert_node(x,81*3+9*(visit[i][j]-1)+ii);
|
||||
}
|
||||
void dfs(int i,int j,int n)
|
||||
{
|
||||
if(i<0||i>9||j<0||j>9)
|
||||
return;
|
||||
visit[i][j]=n;
|
||||
if(!(a[i][j]&16)&&!visit[i-1][j])
|
||||
dfs(i-1,j,n);
|
||||
if(!(a[i][j]&32)&&!visit[i][j+1])
|
||||
dfs(i,j+1,n);
|
||||
if(!(a[i][j]&64)&&!visit[i+1][j])
|
||||
dfs(i+1,j,n);
|
||||
if(!(a[i][j]&128)&&!visit[i][j-1])
|
||||
dfs(i,j-1,n);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n,cases=0;
|
||||
scanf("%d",&n);
|
||||
while(cases++<n)
|
||||
{
|
||||
count=0;
|
||||
memset(visit,0,sizeof(visit));
|
||||
memset(a,0,sizeof(a));
|
||||
dlx_init(81*4);
|
||||
int k=0,i=0,j=0,s=1;
|
||||
for(i=0;i<9;i++)
|
||||
for(j=0;j<9;j++)
|
||||
{
|
||||
scanf("%d",&map[i][j]);
|
||||
a[i][j]=map[i][j]&(~15);
|
||||
map[i][j]=map[i][j]&15;
|
||||
}
|
||||
for(i=0;i<9;i++)
|
||||
for(j=0;j<9;j++)
|
||||
if(!visit[i][j])
|
||||
dfs(i,j,s++);
|
||||
for(i=0;i<9;i++)
|
||||
for(j=0;j<9;j++)
|
||||
if(map[i][j]==0)
|
||||
{
|
||||
int ii;
|
||||
for(ii=1;ii<=9;ii++)
|
||||
dad(i,j,ii);
|
||||
}
|
||||
else
|
||||
dad(i,j,map[i][j]);
|
||||
DLX(0);
|
||||
printf("Case %d:\n",cases);
|
||||
if(!count)
|
||||
printf("No solution\n");
|
||||
else if(count==1)
|
||||
for(i=0;i<9;i++)
|
||||
{
|
||||
for(j=0;j<9;j++)
|
||||
printf("%d",map[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
else
|
||||
printf("Multiple Solutions\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
#include<stdio.h>
|
||||
#include<iostream>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
#define N 100004
|
||||
struct node
|
||||
{
|
||||
int s,t;
|
||||
}ans[N];
|
||||
bool cmp(node a,node b)
|
||||
{
|
||||
if(a.t!=b.t)return a.t>b.t;
|
||||
else a.s>b.s;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t,c=0,n;
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
scanf("%d",&n);
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
scanf("%d%d",&ans[i].s,&ans[i].t);
|
||||
}
|
||||
sort(ans,ans+n,cmp);
|
||||
int ans1=0,time=0;
|
||||
for(int i =0;i<n;i++)
|
||||
{
|
||||
time+=ans[i].s;
|
||||
ans1=max(time+ans[i].t,ans1);
|
||||
}
|
||||
printf("Case %d: %d\n",++c,ans1);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
#define max(a,b) ((a)>=(b)?(a):(b))
|
||||
const int maxn = 50000;
|
||||
struct node
|
||||
{
|
||||
double x, y;
|
||||
}pt[maxn+5];
|
||||
bool set(double &tl, double &tr, double cl, double cr)
|
||||
{
|
||||
if (cl>tr || tl>cr) return false;
|
||||
if (tr>cr) tr = cr;
|
||||
if (tl<cl) tl = cl;
|
||||
return true;
|
||||
}
|
||||
bool ok(double r, int n, double &p)
|
||||
{
|
||||
int i;
|
||||
double r2 = r*r;
|
||||
double td, tl, tr;
|
||||
td = sqrt(r2-pt[0].y*pt[0].y);
|
||||
tl = pt[0].x - td;
|
||||
tr = pt[0].x + td;
|
||||
for (i=1; i<n; i++)
|
||||
{
|
||||
td = sqrt(r2-pt[i].y*pt[i].y);
|
||||
if (!set(tl, tr, pt[i].x-td, pt[i].x+td))
|
||||
return false;
|
||||
}
|
||||
p = (tl+tr)/2;
|
||||
return true;
|
||||
}
|
||||
void solve(int n, double &p, double &d)
|
||||
{
|
||||
double low = 0, high = 3e5;
|
||||
int i;
|
||||
for (i=0; i<n; i++) low = max(low, fabs(pt[i].y));
|
||||
double mid;
|
||||
while(fabs(high-low)>1e-9)
|
||||
{
|
||||
mid = (low+high)/2;
|
||||
if (ok(mid, n, p))
|
||||
{
|
||||
d = mid;
|
||||
high = mid;
|
||||
}
|
||||
else low = mid;
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int i;
|
||||
double p, d;
|
||||
while(scanf("%d", &n)!=EOF)
|
||||
{
|
||||
if (n==0) break;
|
||||
for (i=0; i<n; i++) scanf("%lf %lf", &pt[i].x, &pt[i].y);
|
||||
solve(n, p, d);
|
||||
printf("%.9lf %.9lf\n", p, d);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
using namespace std;
|
||||
const double PI = acos(-1.0);
|
||||
const double e = 2.718281828459;
|
||||
const double eps = 1e-8;
|
||||
char s[10];
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int flag = 0;
|
||||
while(cin>>n && n)
|
||||
{
|
||||
if(!flag)
|
||||
flag = 1;
|
||||
else
|
||||
printf("\n");
|
||||
int sum1, sum2, m;
|
||||
sum1 = sum2 = 0;
|
||||
while(n--)
|
||||
{
|
||||
cin>>s>>m;
|
||||
if(s[0] == 'D')
|
||||
{
|
||||
printf("DROP 2 %d\n", m);
|
||||
sum2 += m;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(sum1 >= m)
|
||||
{
|
||||
printf("TAKE 1 %d\n", m);
|
||||
sum1 -= m;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(sum1 > 0)
|
||||
{
|
||||
printf("TAKE 1 %d\n", sum1);
|
||||
m -= sum1;
|
||||
sum1 = 0;
|
||||
}
|
||||
printf("MOVE 2->1 %d\n", sum2);
|
||||
sum1 = sum2;
|
||||
sum2 = 0;
|
||||
if(m > 0)
|
||||
{
|
||||
printf("TAKE 1 %d\n", m);
|
||||
sum1 -= m;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <list>
|
||||
#include <deque>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <bitset>
|
||||
#include <cctype>
|
||||
#include <ctime>
|
||||
#include <utility>
|
||||
#define M0(x) memset(x, 0, sizeof(x))
|
||||
#define clr(x,y) memset(x, y, sizeof(x))
|
||||
#define P 10567201
|
||||
#define N 1010
|
||||
using namespace std;
|
||||
long long c[1010][1010], two[1010];
|
||||
int n, m, v;
|
||||
long long f[N+5], inv[N+5];
|
||||
using namespace std;
|
||||
void get_inv(){
|
||||
inv[1] = 1;
|
||||
for (int i = 2; i < N; ++i)
|
||||
inv[i] = (P - P / i) * inv[P % i] % P;
|
||||
}
|
||||
void pre_do(){
|
||||
get_inv();
|
||||
two[0] = 1;
|
||||
for (int i = 1; i < N; ++i){
|
||||
two[i] = (two[i-1] << 1);
|
||||
if (two[i] >= P) two[i] -= P;
|
||||
}
|
||||
for (int i = 1; i < N; ++i){
|
||||
c[i][0] = 1;
|
||||
for (int j = 1; j < N; ++j)
|
||||
c[i][j] = c[i][j-1] * (two[i]-j+1) % P * inv[j] % P;
|
||||
}
|
||||
}
|
||||
void solve(){
|
||||
if (v == 0) f[0] = 1;
|
||||
else f[0] = 0;
|
||||
f[1] = 1;
|
||||
for (int i = 2; i <= m; ++i){
|
||||
long long same = f[i-2] * (two[n]-i+2) % P;
|
||||
f[i] = (c[n][i-1] - same) * inv[i] % P;
|
||||
}
|
||||
f[m] += (f[m] < 0 ? P : 0);
|
||||
printf("%I64d\n", f[m]);
|
||||
}
|
||||
int main(){
|
||||
pre_do();
|
||||
while (scanf("%d%d%d", &n, &m, &v) != EOF){
|
||||
if (!(n+m+v)) break;
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
#include<cmath>
|
||||
#include<cstring>
|
||||
#include<vector>
|
||||
#define M 502
|
||||
using namespace std;
|
||||
double p[M][M][2];
|
||||
int d[]={5,20,1,18,4,13,6,10,15,2,17,3,19,7,16,8,11,14,9,12,5,20};
|
||||
int cal(int a,int b){ return a>=b?a-b:a;}
|
||||
int main()
|
||||
{
|
||||
int n,m;
|
||||
for(n=0;n<M;n++){
|
||||
p[0][n][0]=1;
|
||||
p[n][0][1]=1;
|
||||
}
|
||||
for(n=1;n<M;n++)
|
||||
for(m=1;m<M;m++){
|
||||
if(n<=20) p[n][m][0]=n/20.0;
|
||||
if(m<=20) p[n][m][1]=m/3.0;
|
||||
for(int k=0;k<90;k++){
|
||||
double a=0;
|
||||
for(int i=1;i<=20;i++)
|
||||
a+=p[cal(n,d[i])][m][1];
|
||||
p[n][m][0]=1-a/20.0;
|
||||
double b=1e300;
|
||||
for(int i=1;i<=20;i++){
|
||||
double c=0;
|
||||
for(int j=-1;j<=1;j++)
|
||||
c+=p[n][cal(m,d[i+j])][0];
|
||||
c/=3.0;
|
||||
if(b>c) b=c;
|
||||
}
|
||||
p[n][m][1]=1-b;
|
||||
if(n>20) break;
|
||||
}
|
||||
}
|
||||
while(scanf("%d",&n)&&n){
|
||||
printf("%.12lf %.12lf\n",p[n][n][0],p[n][n][1]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
# include<stdio.h>
|
||||
# include<string.h>
|
||||
# include<queue>
|
||||
using namespace std;
|
||||
# define N 35
|
||||
# define INF 0xfffffff
|
||||
int dir[4][2]={0,1,0,-1,1,0,-1,0};
|
||||
struct node{
|
||||
int x,y;
|
||||
};
|
||||
int map[N][N],visit[N][N];
|
||||
int count1[N][N],mark[N][N];
|
||||
int xx[N][N],yy[N][N],tt[N][N];
|
||||
int n,m,flag;
|
||||
void bfs()
|
||||
{
|
||||
int i,ans;
|
||||
queue<node>q;
|
||||
node cur,next;
|
||||
cur.x=0;
|
||||
cur.y=0;
|
||||
q.push(cur);
|
||||
visit[0][0]=0;
|
||||
mark[0][0]=1;
|
||||
count1[0][0]++;
|
||||
while(!q.empty())
|
||||
{
|
||||
cur=q.front();
|
||||
mark[cur.x][cur.y]=0;
|
||||
q.pop();
|
||||
if(count1[cur.x][cur.y]>n*m) {flag=1;return;}
|
||||
if(cur.x==n-1 && cur.y==m-1) continue;
|
||||
if(xx[cur.x][cur.y]!=-1)
|
||||
{
|
||||
next.x=xx[cur.x][cur.y];
|
||||
next.y=yy[cur.x][cur.y];
|
||||
ans=visit[cur.x][cur.y]+tt[cur.x][cur.y];
|
||||
if(visit[next.x][next.y]>ans)
|
||||
{
|
||||
visit[next.x][next.y]=ans;
|
||||
if(mark[next.x][next.y]==0)
|
||||
{
|
||||
mark[next.x][next.y]=1;
|
||||
count1[next.x][next.y]++;
|
||||
q.push(next);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
for(i=0;i<4;i++)
|
||||
{
|
||||
next.x=cur.x+dir[i][0];
|
||||
next.y=cur.y+dir[i][1];
|
||||
ans=visit[cur.x][cur.y]+1;
|
||||
if(next.x<0 || next.y<0 || next.x>=n || next.y>=m || map[next.x][next.y]==1) continue;
|
||||
if(visit[next.x][next.y]>ans)
|
||||
{
|
||||
visit[next.x][next.y]=ans;
|
||||
if(mark[next.x][next.y]==0)
|
||||
{
|
||||
mark[next.x][next.y]=1;
|
||||
count1[next.x][next.y]++;
|
||||
q.push(next);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,j,num,a,b;
|
||||
while(scanf("%d%d",&n,&m)!=EOF)
|
||||
{
|
||||
if(n==0 && m==0) break;
|
||||
scanf("%d",&num);
|
||||
memset(map,0,sizeof(map));
|
||||
while(num--)
|
||||
{
|
||||
scanf("%d%d",&a,&b);
|
||||
map[a][b]=1;
|
||||
}
|
||||
scanf("%d",&num);
|
||||
memset(xx,-1,sizeof(xx));
|
||||
while(num--)
|
||||
{
|
||||
scanf("%d%d",&a,&b);
|
||||
scanf("%d%d%d",&xx[a][b],&yy[a][b],&tt[a][b]);
|
||||
}
|
||||
memset(mark,0,sizeof(mark));
|
||||
memset(count1,0,sizeof(count1));
|
||||
for(i=0;i<=n;i++)
|
||||
for(j=0;j<=m;j++)
|
||||
visit[i][j]=INF;
|
||||
flag=0;
|
||||
bfs();
|
||||
if(flag==1) {printf("Never\n");continue;}
|
||||
if(visit[n-1][m-1]==INF) printf("Impossible\n");
|
||||
else printf("%d\n",visit[n-1][m-1]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <set>
|
||||
#include <bitset>
|
||||
#include <cctype>
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
#include <numeric>
|
||||
#include <utility>
|
||||
#include <stack>
|
||||
#include <queue>
|
||||
#include <deque>
|
||||
#include <iomanip>
|
||||
#include <cassert>
|
||||
#define pb push_back
|
||||
#define mp make_pair
|
||||
#define Maxn 1100
|
||||
#define fi first
|
||||
#define se second
|
||||
using namespace std;
|
||||
typedef pair<double, double> pii;
|
||||
const double eps=1e-8;
|
||||
const double inf=1e300;
|
||||
int n;
|
||||
double dp[Maxn][2];
|
||||
pii seg[Maxn][2];
|
||||
inline double getdis(pii a, pii b)
|
||||
{
|
||||
return sqrt((a.fi-b.fi)*(a.fi-b.fi)+(a.se-b.se)*(a.se-b.se));
|
||||
}
|
||||
inline int sign(double x)
|
||||
{
|
||||
return x<-eps?-1:x>eps;
|
||||
}
|
||||
pii intersert(pii A, pii B, double y)
|
||||
{
|
||||
double dx = A.fi - B.fi;
|
||||
double dy = A.se - B.se;
|
||||
return mp(A.fi + (y-A.se)*dx/dy, y);
|
||||
}
|
||||
bool leftturn(pii p, pii a, pii b)
|
||||
{
|
||||
double x1 = (a.fi-p.fi), y1 = (a.se-p.se);
|
||||
double x2 = (b.fi - p.fi), y2 = (b.se-p.se);
|
||||
return sign(x1*y2-x2*y1)>0;
|
||||
}
|
||||
bool noRightTurn(pii p, pii a, pii b)
|
||||
{
|
||||
double x1 = (a.fi-p.fi), y1 = (a.se-p.se);
|
||||
double x2 = (b.fi - p.fi), y2 = (b.se-p.se);
|
||||
return sign(x1*y2 - x2*y1)>=0;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
double a, b, c;
|
||||
while (~scanf("%d", &n), n)
|
||||
{
|
||||
scanf("%lf%lf", &a, &b);
|
||||
seg[0][0] = mp(a, b);
|
||||
for (int i=1; i<=n; i++)
|
||||
{
|
||||
scanf("%lf%lf%lf", &c, &a, &b);
|
||||
seg[i][0] = mp(a, c);
|
||||
seg[i][1] = mp(b, c);
|
||||
}
|
||||
dp[n][0] = dp[n][1] = 0;
|
||||
int j;
|
||||
for (int i=n-1; i>=0; i--)
|
||||
{
|
||||
for (int side=0; side<2; side++)
|
||||
{
|
||||
dp[i][side] = inf;
|
||||
pair<pii,pii> next = mp(seg[i+1][0], seg[i+1][1]);
|
||||
pii pt = seg[i][side];
|
||||
for (j=i+1; j<=n; j++)
|
||||
{
|
||||
if (leftturn(pt, seg[j][1], next.fi) || leftturn(pt, next.se, seg[j][0])) break;
|
||||
if (noRightTurn(pt, next.fi, seg[j][0]))
|
||||
{
|
||||
next.fi = seg[j][0];
|
||||
dp[i][side] = min(dp[i][side], getdis(pt, next.fi)+dp[j][0]);
|
||||
}
|
||||
if (noRightTurn(pt, seg[j][1], next.se))
|
||||
{
|
||||
next.se = seg[j][1];
|
||||
dp[i][side] = min(dp[i][side], getdis(pt, next.se)+dp[j][1]);
|
||||
}
|
||||
}
|
||||
if (j > n)
|
||||
{
|
||||
double y = seg[n][0].se;
|
||||
next.fi = intersert(pt, next.fi,y);
|
||||
next.se = intersert(pt, next.se, y);
|
||||
if (pt.fi < next.fi.fi) dp[i][side] = min(dp[i][side], getdis(pt, next.fi));
|
||||
else if (pt.fi > next.se.fi) dp[i][side] = min(dp[i][side], getdis(pt, next.se));
|
||||
else dp[i][side] = min(dp[i][side], pt.se - y);
|
||||
}
|
||||
if (i == 0) break;
|
||||
}
|
||||
}
|
||||
printf("%.10f\n", dp[0][0]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
#include"stdio.h"
|
||||
#include"string.h"
|
||||
#define N 10002
|
||||
struct node
|
||||
{
|
||||
int x,y;
|
||||
}A[N];
|
||||
int main()
|
||||
{
|
||||
int i,j,k;
|
||||
int n,m;
|
||||
int s,d,x,y;
|
||||
while(scanf("%d%d",&n,&m),n||m)
|
||||
{
|
||||
for(i=0;i<n;i++)
|
||||
{
|
||||
scanf("%d%d",&s,&d);
|
||||
scanf("%d%d",&x,&y);
|
||||
A[i].x=x;
|
||||
A[i].y=x+y;
|
||||
}
|
||||
for(i=0;i<m;i++)
|
||||
{
|
||||
scanf("%d%d",&x,&y);
|
||||
y=x+y;
|
||||
for(k=0,j=0;j<n;j++)
|
||||
{
|
||||
if(A[j].y>x&&A[j].x<y)
|
||||
k++;
|
||||
}
|
||||
printf("%d\n",k);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
#include<cstring>
|
||||
const int MAXN=40000+5;
|
||||
const int SIGMA_SIZE=26;
|
||||
const int INF=~0U>>1;
|
||||
struct State{
|
||||
State* go[SIGMA_SIZE],*suf;
|
||||
int val,cnt,right;
|
||||
State():suf(0) {val=cnt=0;memset(go,0,sizeof go);}
|
||||
}*root,*last;
|
||||
State mem[MAXN<<1],*cur;
|
||||
int ans1,ans2;
|
||||
int m;
|
||||
inline void init()
|
||||
{
|
||||
ans1=-1;
|
||||
cur=mem;
|
||||
mem[0]=State();
|
||||
last=root=cur++;
|
||||
}
|
||||
inline void extend(int w)
|
||||
{
|
||||
State* p=last,*np=cur++;
|
||||
*np=State();
|
||||
np->right=np->val=p->val+1;
|
||||
while(p && !p->go[w]) p->go[w]=np,p=p->suf;
|
||||
if(!p) np->suf=root;
|
||||
else
|
||||
{
|
||||
State* q=p->go[w];
|
||||
if(q->val==p->val+1) np->suf=q;
|
||||
else
|
||||
{
|
||||
State* nq=cur++;
|
||||
*nq=State();
|
||||
memcpy(nq->go,q->go,sizeof q->go);
|
||||
nq->right=nq->val=p->val+1;
|
||||
nq->suf=q->suf;
|
||||
q->suf=np->suf=nq;
|
||||
while(p && p->go[w]==q) p->go[w]=nq,p=p->suf;
|
||||
}
|
||||
}
|
||||
last=np;
|
||||
}
|
||||
inline int idx(char c)
|
||||
{
|
||||
return c-'a';
|
||||
}
|
||||
char s[MAXN];
|
||||
int n;
|
||||
State* pt[MAXN<<1];
|
||||
void work()
|
||||
{
|
||||
static int ws[MAXN<<1];
|
||||
State* t;
|
||||
for(int i=0;i<=n;++i) ws[i]=0;
|
||||
for(t=mem+1;t!=cur;++t) ++ws[t->val];
|
||||
for(int i=1;i<=n;++i) ws[i]+=ws[i-1];
|
||||
for(t=cur-1;t!=mem;--t) pt[--ws[t->val]]=t;
|
||||
t=root;
|
||||
for(int i=0;i<n;++i) t=t->go[idx(s[i])],t->cnt++;
|
||||
for(int i=cur-mem-2;i>=0;--i)
|
||||
{
|
||||
State* u=pt[i];
|
||||
if(u->cnt>=m)
|
||||
{
|
||||
if(u->val>ans1) ans1=u->val,ans2=u->right-u->val;
|
||||
else if(u->val==ans1) ans2=std::max(ans2,u->right-u->val);
|
||||
}
|
||||
if(u->suf)
|
||||
u->suf->cnt+=u->cnt,u->suf->right=std::max(u->suf->right,u->right);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(scanf("%d",&m)!=EOF && m)
|
||||
{
|
||||
init();
|
||||
scanf("%s",s);
|
||||
n=strlen(s);
|
||||
if(m==1)
|
||||
{
|
||||
printf("%d 0\n",n);
|
||||
continue;
|
||||
}
|
||||
for(int i=0;i<n;++i)
|
||||
extend(idx(s[i]));
|
||||
work();
|
||||
if(ans1==-1) puts("none");
|
||||
else printf("%d %d\n",ans1,ans2);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
#include<iostream>
|
||||
#include<cstring>
|
||||
#include<cmath>
|
||||
const int N=1010;
|
||||
const double inf=1e14;
|
||||
using namespace std;
|
||||
struct Point{
|
||||
int x,y,z;
|
||||
}point[N];
|
||||
int n;
|
||||
double edge[N][N];
|
||||
int nearvex[N];
|
||||
double lowcost[N];
|
||||
double sum;
|
||||
int used[N][N];
|
||||
int visited[N];
|
||||
double Max[N][N];
|
||||
void prim(int v0){
|
||||
sum=0;
|
||||
memset(used,0,sizeof(used));
|
||||
memset(visited,0,sizeof(visited));
|
||||
memset(Max,0,sizeof(Max));
|
||||
for(int i=1;i<=n;i++){
|
||||
lowcost[i]=edge[v0][i];
|
||||
nearvex[i]=v0;
|
||||
}
|
||||
visited[v0]=1;
|
||||
for(int i=1;i<n;i++){
|
||||
double min=inf;
|
||||
int v=-1;
|
||||
for(int j=1;j<=n;j++){
|
||||
if(!visited[j]&&lowcost[j]<min){
|
||||
v=j,min=lowcost[j];
|
||||
}
|
||||
}
|
||||
if(v!=-1){
|
||||
sum+=lowcost[v];
|
||||
used[v][nearvex[v]]=used[nearvex[v]][v]=1;
|
||||
visited[v]=1;
|
||||
for(int k=1;k<=n;k++){
|
||||
if(visited[k]&&k!=v){
|
||||
Max[v][k]=Max[k][v]=(Max[k][nearvex[v]]>lowcost[v]?Max[k][nearvex[v]]:lowcost[v]);
|
||||
}
|
||||
if(!visited[k]&&edge[v][k]<lowcost[k]){
|
||||
lowcost[k]=edge[v][k];
|
||||
nearvex[k]=v;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main(){
|
||||
int t;
|
||||
scanf("%d",&t);
|
||||
while(t--){
|
||||
scanf("%d",&n);
|
||||
for(int i=1;i<=n;i++){
|
||||
scanf("%d%d%d",&point[i].x,&point[i].y,&point[i].z);
|
||||
}
|
||||
for(int i=1;i<=n;i++){
|
||||
edge[i][i]=0;
|
||||
for(int j=i+1;j<=n;j++){
|
||||
double dis=sqrt(pow((point[i].x-point[j].x)*1.0,2)+pow((point[i].y-point[j].y)*1.0,2));
|
||||
edge[i][j]=edge[j][i]=dis;
|
||||
}
|
||||
}
|
||||
prim(1);
|
||||
double r=-1;
|
||||
for(int i=1;i<=n;i++){
|
||||
for(int j=1;j<=n;j++)if(i!=j){
|
||||
if(used[i][j]){
|
||||
r=(r>(point[i].z+point[j].z)*1.0/(sum-edge[i][j])?r:(point[i].z+point[j].z)*1.0/(sum-edge[i][j]));
|
||||
}else if(!used[i][j]){
|
||||
r=(r>(point[i].z+point[j].z)*1.0/(sum-Max[i][j])?r:(point[i].z+point[j].z)*1.0/(sum-Max[i][j]));
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%.2lf\n",r);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<stdlib.h>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
const int MAXN = 20;
|
||||
const int MAXM = 820;
|
||||
int ans, n, d[MAXN][MAXN], m, ns;
|
||||
int used[210][210];
|
||||
struct point
|
||||
{
|
||||
int x, y;
|
||||
}p[MAXN];
|
||||
struct triangle
|
||||
{
|
||||
int a, b, c;
|
||||
}t[MAXM];
|
||||
int dist(point p1, point p2)
|
||||
{
|
||||
int x, y;
|
||||
x = p1.x - p2.x;
|
||||
y = p1.y - p2.y;
|
||||
return x * x + y * y;
|
||||
}
|
||||
bool ponls(point p0, point p1, point p2)
|
||||
{
|
||||
int x1, x2, y1, y2;
|
||||
x1 = p0.x - p2.x;
|
||||
y1 = p0.y - p2.y;
|
||||
x2 = p1.x - p2.x;
|
||||
y2 = p1.y - p2.y;
|
||||
if(x1 * y2 == x2 * y1)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
bool similar(triangle t1, triangle t2)
|
||||
{
|
||||
int a1, a2, b1, b2, c1, c2;
|
||||
a1 = t1.a, b1 = t1.b, c1 = t1.c;
|
||||
a2 = t2.a, b2 = t2.b, c2 = t2.c;
|
||||
if(a1 * b2 == b1 * a2 && a1 * c2 == c1 * a2 && b1 * c2 == c1 * b2)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
void ReadGraph()
|
||||
{
|
||||
int i, j, k, s, v;
|
||||
n = 0;
|
||||
memset(used, 0, sizeof used);
|
||||
for(i = 1; i <= ns; i ++)
|
||||
{
|
||||
scanf("%d%d", &s, &v);
|
||||
s += 100, v += 100;
|
||||
if(used[s][v]) continue;
|
||||
used[s][v] = 1;
|
||||
p[++ n].x = s;
|
||||
p[n]. y = v;
|
||||
}
|
||||
for(i = 1; i <= n; i ++)
|
||||
for(j = 1; j <= n; j ++)
|
||||
d[i][j] = d[j][i] = dist(p[i], p[j]);
|
||||
}
|
||||
void MakeTriangle()
|
||||
{
|
||||
int i, j, k, temp[3];
|
||||
m = 0;
|
||||
for(i = 1; i <= n; i ++)
|
||||
for(j = i + 1; j <= n; j ++)
|
||||
for(k = j + 1; k <= n; k ++)
|
||||
{
|
||||
if(ponls(p[i], p[j], p[k]))
|
||||
continue;
|
||||
temp[0] = d[i][j];
|
||||
temp[1] = d[j][k];
|
||||
temp[2] = d[i][k];
|
||||
sort(temp, temp + 3);
|
||||
t[++ m].a = temp[0];
|
||||
t[m].b = temp[1];
|
||||
t[m].c = temp[2];
|
||||
}
|
||||
}
|
||||
void cal()
|
||||
{
|
||||
int cnt, i, j;
|
||||
ans = 0;
|
||||
for(i = 1; i <= m; i ++)
|
||||
{
|
||||
cnt = 1;
|
||||
for(j = i + 1; j <= m; j ++)
|
||||
{
|
||||
if( similar(t[i], t[j]))
|
||||
++ cnt;
|
||||
}
|
||||
ans = max(ans, cnt);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(scanf("%d", &ns), ns)
|
||||
{
|
||||
ReadGraph();
|
||||
MakeTriangle();
|
||||
cal();
|
||||
printf("%d\n", ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,176 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<algorithm>
|
||||
#include<cstdlib>
|
||||
using namespace std;
|
||||
int ratk[12][2]={
|
||||
{-1,0},{0,-1},{0,1},{1,0},
|
||||
{-1,-1},{-1,1},{1,-1},{1,1},
|
||||
{-2,0},{0,-2},{0,2},{2,0}
|
||||
};
|
||||
int natk[3]={8,12,4};
|
||||
double fatk[3][3]={{1,2,0.5},{0.5,1,2},{2,0.5,1}};
|
||||
int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
|
||||
struct Soldier{
|
||||
int flag, type;
|
||||
Soldier(){}
|
||||
Soldier(int f, int t):flag(f), type(t){}
|
||||
}sld[8];
|
||||
struct Next{
|
||||
int id, mx, my, aid, sc;
|
||||
Next(){}
|
||||
Next(int _id, int _mx, int _my, int _aid, int _sc):
|
||||
id(_id), mx(_mx), my(_my), aid(_aid), sc(_sc){}
|
||||
friend bool operator< (const Next& a, const Next& b){
|
||||
return a.sc > b.sc;
|
||||
}
|
||||
}nexts[12][150];
|
||||
int nsz[12];
|
||||
struct State{
|
||||
int x, y, sc;
|
||||
State(){}
|
||||
State(int _x, int _y, int _sc):
|
||||
x(_x), y(_y), sc(_sc){}
|
||||
}sta[12][8];
|
||||
short smaze[12][8][8];
|
||||
int lx, ly, lk;
|
||||
int n, s[3];
|
||||
short maze[8][8];
|
||||
int skm, szy;
|
||||
inline bool myturn(int k, int id){
|
||||
if(sld[id].flag!=(k&1)) return false;
|
||||
if(sta[k][id].sc==0) return false;
|
||||
return true;
|
||||
}
|
||||
inline bool moveillegal(int k, int id, int x, int y){
|
||||
if(x<0||x>=lx) return true;
|
||||
if(y<0||y>=ly) return true;
|
||||
if(maze[x][y]==2) return true;
|
||||
if(sld[id].type==2&&maze[x][y]==1) return true;
|
||||
if(smaze[k][x][y]!=-1&&sld[smaze[k][x][y]].flag!=sld[id].flag) return true;
|
||||
return false;
|
||||
}
|
||||
inline bool attackillegal(int k, int id, int x, int y){
|
||||
if(x<0||x>=lx) return true;
|
||||
if(y<0||y>=ly) return true;
|
||||
if(smaze[k][x][y]==-1) return true;
|
||||
if(sld[smaze[k][x][y]].flag==sld[id].flag) return true;
|
||||
if(sta[k][smaze[k][x][y]].sc==0) return true;
|
||||
return false;
|
||||
}
|
||||
inline void donext(int k, Next& p){
|
||||
if(p.id==-1) return;
|
||||
swap(smaze[k][sta[k][p.id].x][sta[k][p.id].y],smaze[k][p.mx][p.my]);
|
||||
sta[k][p.id].x = p.mx;
|
||||
sta[k][p.id].y = p.my;
|
||||
if(p.aid==-1) return;
|
||||
sta[k][p.aid].sc-=p.sc;
|
||||
sld[p.aid].flag?szy-=p.sc:skm-=p.sc;
|
||||
if(sta[k][p.aid].sc==0){
|
||||
smaze[k][sta[k][p.aid].x][sta[k][p.aid].y]=-1;
|
||||
}
|
||||
}
|
||||
inline void undonext(int k, Next& p){
|
||||
if(p.id==-1) return;
|
||||
sta[k][p.id].x = sta[k-1][p.id].x;
|
||||
sta[k][p.id].y = sta[k-1][p.id].y;
|
||||
swap(smaze[k][sta[k][p.id].x][sta[k][p.id].y],smaze[k][p.mx][p.my]);
|
||||
if(p.aid==-1) return;
|
||||
sta[k][p.aid].sc+=p.sc;
|
||||
sld[p.aid].flag?szy+=p.sc:skm+=p.sc;
|
||||
smaze[k][sta[k][p.aid].x][sta[k][p.aid].y]=p.aid;
|
||||
}
|
||||
inline int attack(int k, int sid, int tid){
|
||||
return min(sta[k][tid].sc,
|
||||
int(sta[k][sid].sc*fatk[sld[sid].type][sld[tid].type]));
|
||||
}
|
||||
inline void getattack(int k, int id, int x, int y){
|
||||
for(int d=0;d<natk[sld[id].type];d++){
|
||||
int tx = x + ratk[d][0];
|
||||
int ty = y + ratk[d][1];
|
||||
if(!attackillegal(k, id, tx, ty)){
|
||||
int atkid = smaze[k][tx][ty];
|
||||
int atksc = attack(k, id, atkid);
|
||||
nexts[k][nsz[k]++] = Next(id, x, y, atkid, atksc);
|
||||
}
|
||||
}
|
||||
}
|
||||
bool reach[8][8];
|
||||
inline void stepbystep(int dep, int k, int id, int x, int y){
|
||||
reach[x][y] = true;
|
||||
if(dep==0) return;
|
||||
for(int d=0;d<4;d++){
|
||||
int tx = x+dir[d][0];
|
||||
int ty = y+dir[d][1];
|
||||
if(!moveillegal(k, id, tx, ty)){
|
||||
stepbystep(dep-1, k, id, tx, ty);
|
||||
}
|
||||
}
|
||||
}
|
||||
inline void getmove(int k, int id){
|
||||
memset(reach, false, sizeof(reach));
|
||||
stepbystep(s[sld[id].type], k, id, sta[k][id].x, sta[k][id].y);
|
||||
for(int i=0;i<lx;i++){
|
||||
for(int j=0;j<ly;j++){
|
||||
if(reach[i][j]&&smaze[k][i][j]==-1){
|
||||
getattack(k, id, i, j);
|
||||
nexts[k][nsz[k]++] = Next(id, i, j, -1, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
getattack(k, id, sta[k][id].x, sta[k][id].y);
|
||||
}
|
||||
inline void getnext(int k){
|
||||
nsz[k]=0;
|
||||
for(int i=0;i<n;i++){
|
||||
if(myturn(k,i)) getmove(k, i);
|
||||
}
|
||||
nexts[k][nsz[k]++] = Next(-1, -1, -1, -1, 0);
|
||||
sort(nexts[k], nexts[k]+nsz[k]);
|
||||
}
|
||||
int dfs(int k, int sc, int maxcut, int mincut){
|
||||
if(k==lk) return sc;
|
||||
if(szy==0||skm==0) return sc;
|
||||
getnext(k);
|
||||
memcpy(sta[k+1],sta[k],sizeof(sta[k]));
|
||||
memcpy(smaze[k+1],smaze[k],sizeof(smaze[k]));
|
||||
int maxval = sc-szy, minval = sc+skm;
|
||||
for(int i=0;i<nsz[k];i++){
|
||||
Next &u = nexts[k][i];
|
||||
donext(k+1, u);
|
||||
int val = dfs(k+1,(k&1)?(sc-u.sc):(sc+u.sc), maxval, minval);
|
||||
undonext(k+1, u);
|
||||
if(maxval<val) maxval = val;
|
||||
if(minval>val) minval = val;
|
||||
if(((k&1)==0)&&val>=mincut) return val;
|
||||
if(((k&1)==0)&&(val-sc>=szy)) return val;
|
||||
if(((k&1)==1)&&val<=maxcut) return val;
|
||||
if(((k&1)==1)&&(val-sc<=-skm)) return val;
|
||||
}
|
||||
return (k&1)?minval:maxval;
|
||||
}
|
||||
int main(){
|
||||
while(scanf("%d%d%d", &lx, &ly, &lk),lx||ly){
|
||||
memset(maze, 0, sizeof(maze));
|
||||
memset(smaze, -1, sizeof(smaze));
|
||||
for(int i=0;i<lx;i++){
|
||||
for(int j=0;j<ly;j++){
|
||||
scanf("%d", &maze[i][j]);
|
||||
}
|
||||
}
|
||||
scanf("%d%d%d%d", &n, &s[0], &s[2], &s[1]);
|
||||
szy=skm=0;
|
||||
for(int i=0;i<n;i++){
|
||||
int x, y, a, b, c;
|
||||
scanf("%d%d%d%d%d", &x, &y, &a, &b, &c);
|
||||
if(c==1) c=2; else if(c==2) c=1;
|
||||
x--; y--;
|
||||
sld[i] = Soldier(a, c);
|
||||
smaze[0][x][y] = i;
|
||||
sta[0][i] = State(x, y, b);
|
||||
a?szy+=b:skm+=b;
|
||||
}
|
||||
int ans = dfs(0, skm-szy, -szy, skm);
|
||||
printf("%d\n", ans);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <queue>
|
||||
using namespace std;
|
||||
#define INF 2000000
|
||||
const int N=60;
|
||||
struct Edge
|
||||
{
|
||||
int v,w;
|
||||
Edge *nxt;
|
||||
}memo[N*N],*cur,*adj[N];
|
||||
int n,m,K,mask;
|
||||
int st[N],vis[N][1<<11];
|
||||
int d[N][1<<11],dp[1<<11];
|
||||
queue<int>que;
|
||||
void addEdge(int u,int v,int w)
|
||||
{
|
||||
cur->v=v; cur->w=w;
|
||||
cur->nxt=adj[u];
|
||||
adj[u]=cur++;
|
||||
}
|
||||
bool check(int s)
|
||||
{
|
||||
int res=0;
|
||||
for(int i=0;s;i++,s>>=1)
|
||||
res+=(s&1)*(i<K?1:-1);
|
||||
return (res==0?true:false);
|
||||
}
|
||||
void spfa()
|
||||
{
|
||||
while(que.size())
|
||||
{
|
||||
int top=que.front(); que.pop();
|
||||
int u=top/10000,s=top%10000;
|
||||
vis[u][s]=0;
|
||||
for(Edge* it=adj[u];it;it=it->nxt)
|
||||
{
|
||||
int v=it->v,w=it->w;
|
||||
if(d[v][s|st[v]]>d[u][s]+w)
|
||||
{
|
||||
d[v][s|st[v]]=d[u][s]+w;
|
||||
if((s|st[v])==s&&!vis[v][s])
|
||||
que.push(v*10000+s),vis[v][s]=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void init()
|
||||
{
|
||||
cur=memo; mask=(1<<(2*K))-1;
|
||||
memset(st,0,sizeof(st));
|
||||
memset(vis,0,sizeof(vis));
|
||||
memset(adj,0,sizeof(adj));
|
||||
for(int i=1;i<=n;i++)
|
||||
for(int j=0;j<=mask;j++) d[i][j]=INF;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
int u,v,w;
|
||||
scanf("%d%d%d",&n,&m,&K);
|
||||
init();
|
||||
for(int i=0;i<m;i++)
|
||||
{
|
||||
scanf("%d%d%d",&u,&v,&w);
|
||||
addEdge(u,v,w);
|
||||
addEdge(v,u,w);
|
||||
}
|
||||
for(int i=1;i<=K;i++)
|
||||
{
|
||||
st[i]=1<<(i-1);
|
||||
d[i][st[i]]=0;
|
||||
st[n-i+1]=(1<<(K+(i-1)));
|
||||
d[n-i+1][st[n-i+1]]=0;
|
||||
}
|
||||
for(int s=0;s<=mask;s++)
|
||||
{
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
for(int p=(s-1)&s;p;p=(p-1)&s)
|
||||
d[i][s]=min(d[i][s],d[i][p|st[i]]+d[i][(s-p)|st[i]]);
|
||||
if(d[i][s]<INF&&!vis[i][s])
|
||||
que.push(i*10000+s),vis[i][s]=1;
|
||||
}
|
||||
spfa();
|
||||
}
|
||||
for(int s=0;s<=mask;s++)
|
||||
{
|
||||
dp[s]=INF;
|
||||
for(int i=1;i<=n;i++) dp[s]=min(dp[s],d[i][s]);
|
||||
}
|
||||
for(int s=1;s<=mask;s++)
|
||||
if(check(s))
|
||||
for(int p=(s-1)&s;p;p=(p-1)&s)
|
||||
if(check(p)) dp[s]=min(dp[s],dp[p]+dp[s-p]);
|
||||
if(dp[mask]>=INF) puts("No solution");
|
||||
else printf("%d\n",dp[mask]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#define forn(i,n) for(int i=0; i<(int)(n); i++)
|
||||
using namespace std;
|
||||
const int N = 4;
|
||||
const double eps = 1e-6;
|
||||
const double pi = acos(-1.0);
|
||||
struct matrix {
|
||||
int n;
|
||||
double a[N][N];
|
||||
void clear() {forn(i,n)forn(j,n)a[i][j]=0;}
|
||||
matrix(){}
|
||||
matrix(int z):n(z){clear();}
|
||||
matrix operator * (const matrix& u){
|
||||
matrix ans(n);
|
||||
forn(i,n) forn(k,n) forn(j,n)
|
||||
ans.a[i][j] += a[i][k] * u.a[k][j];
|
||||
return ans;
|
||||
}
|
||||
matrix pow(int k){
|
||||
matrix r(n), t=*this;
|
||||
forn(i,n) r.a[i][i]=1.0;
|
||||
while (k){
|
||||
if (k&1) r = t*r;
|
||||
t = t*t;
|
||||
k >>= 1;
|
||||
} return r;
|
||||
}
|
||||
};
|
||||
char op[20];
|
||||
matrix dfs(int tim){
|
||||
matrix res(4);
|
||||
forn(i,4)res.a[i][i]=1;
|
||||
double r[5];
|
||||
while (~scanf("%s", op)){
|
||||
if (op[0] == 'e') break;
|
||||
matrix tmp(4);
|
||||
forn(i,4)tmp.a[i][i]=1;
|
||||
if (op[0] == 't'){
|
||||
forn(i,3) scanf("%lf", &r[i]);
|
||||
forn(i,3) tmp.a[i][3]=r[i];
|
||||
}
|
||||
else if (op[0] == 's'){
|
||||
forn(i,3) scanf("%lf", &r[i]);
|
||||
forn(i,3) tmp.a[i][i]=r[i];
|
||||
}
|
||||
else if (op[0] == 'r' && op[1] == 'o'){
|
||||
double t=0.0, c, s;
|
||||
forn(i,4) scanf("%lf", &r[i]);
|
||||
forn(i,3) t += r[i]*r[i];
|
||||
t = sqrt(t);
|
||||
forn(i,3) r[i] /= t;
|
||||
r[3] = r[3]/180*pi;
|
||||
c = cos(r[3]), s = sin(r[3]);
|
||||
forn(i,3){
|
||||
forn(j,3){
|
||||
bool f = (i+1)%3==j;
|
||||
if (i == j) tmp.a[i][j]=(1-c)*r[i]*r[j]+c;
|
||||
else tmp.a[i][j]=(1-c)*r[i]*r[j]+s*(f?-r[3^i^j]:r[3^i^j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
int tim2;
|
||||
scanf("%d", &tim2);
|
||||
tmp = dfs(tim2);
|
||||
}
|
||||
res = tmp*res;
|
||||
}
|
||||
return res.pow(tim);
|
||||
}
|
||||
double p[5], q[5];
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
while (~scanf("%d", &n) && n){
|
||||
matrix tmp(4);
|
||||
forn(i,4)tmp.a[i][i]=1;
|
||||
tmp = dfs(1)*tmp;
|
||||
forn(i,n){
|
||||
forn(j,3) scanf("%lf",&p[j]);
|
||||
p[3] = 1.0;
|
||||
forn(j,3){
|
||||
q[j] = 0.0;
|
||||
forn(k,4) q[j]+=tmp.a[j][k]*p[k];
|
||||
}
|
||||
forn(j,3) printf("%.2f%c",q[j]+eps,j==2?'\n':' ');
|
||||
}
|
||||
puts("");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
#include<cstring>
|
||||
#include<cmath>
|
||||
#define eps 1e-10
|
||||
#define N (1<<7)+5
|
||||
#define inf 1<<20
|
||||
#define zero(a) (fabs(a)<eps)
|
||||
#define lson (step<<1)
|
||||
#define rson (step<<1|1)
|
||||
using namespace std;
|
||||
double p1,p2,p3,p4,p21,p31,p41;
|
||||
double dp[2005][2005],c[2005];
|
||||
int n,m,k;
|
||||
int main(){
|
||||
while(scanf("%d%d%d%lf%lf%lf%lf",&n,&m,&k,&p1,&p2,&p3,&p4)!=EOF){
|
||||
if(zero(p4)) {puts("0.00000");continue;}
|
||||
p21=p2/(1-p1);
|
||||
p31=p3/(1-p1);
|
||||
p41=p4/(1-p1);
|
||||
dp[1][1]=p4/(1-p1-p2);
|
||||
for(int i=2;i<=n;i++){
|
||||
for(int j=2;j<=(i<k?i:k);j++)
|
||||
c[j]=dp[i-1][j-1]*p31+p41;
|
||||
for(int j=k+1;j<=i;j++)
|
||||
c[j]=dp[i-1][j-1]*p31;
|
||||
double p=1,tmp=0;
|
||||
for(int j=i;j>1;j--){
|
||||
tmp+=p*c[j];
|
||||
p*=p21;
|
||||
}
|
||||
dp[i][i]=(tmp+p*p41)/(1-p*p21);
|
||||
dp[i][1]=p21*dp[i][i]+p41;
|
||||
for(int j=2;j<i;j++)
|
||||
dp[i][j]=p21*dp[i][j-1]+c[j];
|
||||
}
|
||||
printf("%.5f\n",dp[n][m]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
typedef __int64 LL;
|
||||
LL gcd(LL a,LL b)
|
||||
{
|
||||
return b==0?a:gcd(b,a%b);
|
||||
}
|
||||
LL lcm(LL a,LL b)
|
||||
{
|
||||
return a/gcd(a,b)*b;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
LL n,s1,v1,s2,v2;
|
||||
LL T,tt=0;
|
||||
cin>>T;
|
||||
while(T--)
|
||||
{
|
||||
LL i,j,k,ans,p,q,m,num;
|
||||
cin>>n>>s1>>v1>>s2>>v2;
|
||||
m=lcm(s1,s2);
|
||||
num=n/m;
|
||||
if(num){num--;}
|
||||
if(v1*s2>=v2*s1)
|
||||
{
|
||||
num=m/s1*num;
|
||||
p=(n-num*s1)/s2;
|
||||
ans=num*v1+p*v2;
|
||||
if(s1>=s2)
|
||||
{
|
||||
for(i=num;i*s1<=n;i++)
|
||||
{
|
||||
q=i*v1+(n-i*s1)/s2*v2;
|
||||
if(q>ans)ans=q;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(i=p;i>=0;i--)
|
||||
{
|
||||
q=i*v2+(n-i*s2)/s1*v1;
|
||||
if(q>ans)ans=q;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
num=m/s2*num;
|
||||
p=(n-num*s2)/s1;
|
||||
ans=num*v2+p*v1;
|
||||
if(s2>=s1)
|
||||
{
|
||||
for(i=num;i*s2<=n;i++)
|
||||
{
|
||||
q=i*v2+(n-i*s2)/s1*v1;
|
||||
if(q>ans)ans=q;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(i=p;i>=0;i--)
|
||||
{
|
||||
q=i*v1+(n-i*s1)/s2*v2;
|
||||
if(q>ans)ans=q;
|
||||
}
|
||||
}
|
||||
}
|
||||
cout<<"Case #"<<++tt<<": "<<ans<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,156 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
const int maxn = 16;
|
||||
struct Trian {
|
||||
int x, y, z;
|
||||
Trian () {};
|
||||
Trian (int x, int y, int z) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
}
|
||||
Trian add (int type) {
|
||||
int mv = (x+y+z == 0 ? 1 : -1);
|
||||
if (type == 0)
|
||||
return Trian(x, y+mv, z+mv);
|
||||
else if (type == 1)
|
||||
return Trian(x+mv, y, z+mv);
|
||||
else
|
||||
return Trian(x+mv, y+mv, z);
|
||||
}
|
||||
void rotate () {
|
||||
int tmp = x;
|
||||
x = y;
|
||||
y = z;
|
||||
z = tmp;
|
||||
}
|
||||
void overturn () {
|
||||
x = 1 - x;
|
||||
y = 1 - y;
|
||||
z = -z;
|
||||
}
|
||||
void move (int x, int y, int z) {
|
||||
this->x += x;
|
||||
this->y += y;
|
||||
this->z += z;
|
||||
}
|
||||
};
|
||||
bool operator == (const Trian& a, const Trian& b) {
|
||||
return a.x == b.x && a.y == b.y && a.z == b.z;
|
||||
}
|
||||
bool operator < (const Trian& a, const Trian& b) {
|
||||
if (a.x != b.x)
|
||||
return a.x < b.x;
|
||||
else if (a.y != b.y)
|
||||
return a.y < b.y;
|
||||
else
|
||||
return a.z < b.z;
|
||||
}
|
||||
struct Hash {
|
||||
int cnt, key[maxn+5];
|
||||
};
|
||||
bool operator < (const Hash& a, const Hash& b) {
|
||||
if (a.cnt != b.cnt)
|
||||
return a.cnt < b.cnt;
|
||||
for (int i = 0; i < a.cnt; i++)
|
||||
if (a.key[i] != b.key[i])
|
||||
return a.key[i] < b.key[i];
|
||||
return false;
|
||||
}
|
||||
struct Pizz {
|
||||
int cnt;
|
||||
Trian arr[maxn+5];
|
||||
Pizz () { cnt = 0; }
|
||||
bool add (const Trian& u) {
|
||||
for (int i = 0; i < cnt; i++)
|
||||
if (arr[i] == u)
|
||||
return false;
|
||||
arr[cnt++] = u;
|
||||
return true;
|
||||
}
|
||||
void remove () { cnt--; }
|
||||
Hash getKey () {
|
||||
Hash u;
|
||||
u.cnt = cnt;
|
||||
for (int i = 0; i < cnt; i++) {
|
||||
u.key[i] = arr[i].x + 8;
|
||||
u.key[i] <<= 4;
|
||||
u.key[i] += arr[i].y + 8;
|
||||
u.key[i] <<= 4;
|
||||
u.key[i] += arr[i].z + 8;
|
||||
}
|
||||
return u;
|
||||
}
|
||||
};
|
||||
set<Hash> vis;
|
||||
int ans[maxn+5] = {0};
|
||||
bool find (Pizz v) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
sort(v.arr, v.arr+v.cnt);
|
||||
if (vis.find(v.getKey()) != vis.end())
|
||||
return true;
|
||||
for (int j = 0; j < v.cnt; j++)
|
||||
v.arr[j].rotate();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool search (Pizz u) {
|
||||
for (int i = 0; i < u.cnt; i++) {
|
||||
int x = u.arr[i].x;
|
||||
int y = u.arr[i].y;
|
||||
int z = u.arr[i].z;
|
||||
Pizz v = u;
|
||||
if (x + y + z == 0) {
|
||||
for (int j = 0; j < v.cnt; j++)
|
||||
v.arr[j].move(-x, -y, -z);
|
||||
if (find(v))
|
||||
return true;
|
||||
} else {
|
||||
for (int j = 0; j < v.cnt; j++) {
|
||||
v.arr[j].overturn();
|
||||
v.arr[j].move(x-1, y-1, z);
|
||||
}
|
||||
if (find(v))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void insert (Pizz u) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
sort(u.arr, u.arr+u.cnt);
|
||||
vis.insert(u.getKey());
|
||||
for (int j = 0; j < u.cnt; j++)
|
||||
u.arr[j].rotate();
|
||||
}
|
||||
}
|
||||
void dfs (Pizz u) {
|
||||
if (u.cnt == maxn+1)
|
||||
return;
|
||||
if (search(u))
|
||||
return;
|
||||
insert(u);
|
||||
ans[u.cnt]++;
|
||||
for (int i = 0; i < u.cnt; i++) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
Trian v = u.arr[i].add(j);
|
||||
if (u.add(v)) {
|
||||
dfs(u);
|
||||
u.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int rec[maxn+5] = {0, 1, 1, 1, 4, 6, 19, 43, 120, 307, 866, 2336, 6588, 18373, 52119, 147700, 422016};
|
||||
int main () {
|
||||
int cas, n;
|
||||
scanf("%d", &cas);
|
||||
for (int i = 1; i <= cas; i++) {
|
||||
scanf("%d", &n);
|
||||
printf("Case #%d: %d\n", i, rec[n]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,192 @@
|
|||
#include<iostream>
|
||||
#include<sstream>
|
||||
#include<fstream>
|
||||
#include<vector>
|
||||
#include<list>
|
||||
#include<deque>
|
||||
#include<queue>
|
||||
#include<stack>
|
||||
#include<map>
|
||||
#include<set>
|
||||
#include<bitset>
|
||||
#include<algorithm>
|
||||
#include<cstdio>
|
||||
#include<cstdlib>
|
||||
#include<cstring>
|
||||
#include<cctype>
|
||||
#include<cmath>
|
||||
#include<ctime>
|
||||
#include<iomanip>
|
||||
using namespace std;
|
||||
const double eps(1e-8);
|
||||
typedef long long lint;
|
||||
const long double PI = acos(-1.0);
|
||||
struct Complex
|
||||
{
|
||||
long double real, image;
|
||||
Complex(long double _real, long double _image)
|
||||
{
|
||||
real = _real;
|
||||
image = _image;
|
||||
}
|
||||
Complex(){}
|
||||
};
|
||||
Complex operator + (const Complex &c1, const Complex &c2)
|
||||
{
|
||||
return Complex(c1.real + c2.real, c1.image + c2.image);
|
||||
}
|
||||
Complex operator - (const Complex &c1, const Complex &c2)
|
||||
{
|
||||
return Complex(c1.real - c2.real, c1.image - c2.image);
|
||||
}
|
||||
Complex operator * (const Complex &c1, const Complex &c2)
|
||||
{
|
||||
return Complex(c1.real*c2.real - c1.image*c2.image, c1.real*c2.image + c1.image*c2.real);
|
||||
}
|
||||
int rev(int id, int len)
|
||||
{
|
||||
int ret = 0;
|
||||
for(int i = 0; (1 << i) < len; i++)
|
||||
{
|
||||
ret <<= 1;
|
||||
if(id & (1 << i)) ret |= 1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
Complex A[1 << 16];
|
||||
void FFT(Complex *a, int len, int DFT)
|
||||
{
|
||||
for(int i = 0; i < len; i++) A[rev(i, len)] = a[i];
|
||||
for(int s = 1; (1 << s) <= len; s++)
|
||||
{
|
||||
int m = (1 << s);
|
||||
Complex wm = Complex(cos(DFT*2*PI/m), sin(DFT*2*PI/m));
|
||||
for(int k = 0; k < len; k += m)
|
||||
{
|
||||
Complex w = Complex(1, 0);
|
||||
for(int j = 0; j < (m >> 1); j++)
|
||||
{
|
||||
Complex t = w*A[k + j + (m >> 1)];
|
||||
Complex u = A[k + j];
|
||||
A[k + j] = u + t;
|
||||
A[k + j + (m >> 1)] = u - t;
|
||||
w = w*wm;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(DFT == -1) for(int i = 0; i < len; i++) A[i].real /= len, A[i].image /= len;
|
||||
for(int i = 0; i < len; i++) a[i] = A[i];
|
||||
}
|
||||
int polynomialLength;
|
||||
void multiply(Complex *p1, Complex *p2)
|
||||
{
|
||||
for(int i = 0; i < polynomialLength; i++)
|
||||
p1[i] = p1[i]*p2[i];
|
||||
return;
|
||||
}
|
||||
void Copy(Complex *p1, Complex *p2)
|
||||
{
|
||||
for(int i = 0; i < polynomialLength; i++)
|
||||
p1[i] = p2[i];
|
||||
return;
|
||||
}
|
||||
int a[13010];
|
||||
int p, m;
|
||||
Complex f[6][1 << 16];
|
||||
Complex g[7][1 << 16];
|
||||
const int par[6][10] =
|
||||
{
|
||||
{0},
|
||||
{1},
|
||||
{1, -1},
|
||||
{1, -3, 2},
|
||||
{1, -6, 3, 8, -6},
|
||||
{1, -10, 15, 20, -20, -30, 24}
|
||||
};
|
||||
void solve()
|
||||
{
|
||||
int maxL = 0;
|
||||
for(int i = 1; i <= m; i++)
|
||||
maxL = max(maxL, a[i]);
|
||||
polynomialLength = 1;
|
||||
while(polynomialLength <= maxL*p) polynomialLength <<= 1;
|
||||
for(int i = 1; i <= p; i++)
|
||||
{
|
||||
for(int j = 0; j < polynomialLength; j++)
|
||||
f[i][j] = Complex(0, 0);
|
||||
for(int j = 1; j <= m; j++)
|
||||
f[i][i*a[j]].real += 1;
|
||||
FFT(f[i], polynomialLength, 1);
|
||||
}
|
||||
int inclusiveLength = -1;
|
||||
switch(p)
|
||||
{
|
||||
case 1: Copy(g[0], f[1]);
|
||||
inclusiveLength = 1;
|
||||
break;
|
||||
case 2: Copy(g[0], f[1]);
|
||||
multiply(g[0], f[1]);
|
||||
Copy(g[1], f[2]);
|
||||
inclusiveLength = 2;
|
||||
break;
|
||||
case 3: Copy(g[0], f[1]);
|
||||
multiply(g[0], f[1]); multiply(g[0], f[1]);
|
||||
Copy(g[1], f[2]);
|
||||
multiply(g[1], f[1]);
|
||||
Copy(g[2], f[3]);
|
||||
inclusiveLength = 3;
|
||||
break;
|
||||
case 4: Copy(g[0], f[1]);
|
||||
multiply(g[0], f[1]); multiply(g[0], f[1]); multiply(g[0], f[1]);
|
||||
Copy(g[1], f[2]);
|
||||
multiply(g[1], f[1]); multiply(g[1], f[1]);
|
||||
Copy(g[2], f[2]);
|
||||
multiply(g[2], f[2]);
|
||||
Copy(g[3], f[3]);
|
||||
multiply(g[3], f[1]);
|
||||
Copy(g[4], f[4]);
|
||||
inclusiveLength = 5;
|
||||
break;
|
||||
case 5: Copy(g[0], f[1]);
|
||||
multiply(g[0], f[1]); multiply(g[0], f[1]); multiply(g[0], f[1]); multiply(g[0], f[1]);
|
||||
Copy(g[1], f[2]);
|
||||
multiply(g[1], f[1]); multiply(g[1], f[1]); multiply(g[1], f[1]);
|
||||
Copy(g[2], f[2]);
|
||||
multiply(g[2], f[2]); multiply(g[2], f[1]);
|
||||
Copy(g[3], f[3]);
|
||||
multiply(g[3], f[1]); multiply(g[3], f[1]);
|
||||
Copy(g[4], f[3]);
|
||||
multiply(g[4], f[2]);
|
||||
Copy(g[5], f[4]);
|
||||
multiply(g[5], f[1]);
|
||||
Copy(g[6], f[5]);
|
||||
inclusiveLength = 7;
|
||||
break;
|
||||
}
|
||||
for(int i = 0; i < inclusiveLength; i++)
|
||||
FFT(g[i], polynomialLength, -1);
|
||||
for(int i = 0; i < polynomialLength; i++)
|
||||
{
|
||||
double res = 0;
|
||||
for(int j = 0; j < inclusiveLength; j++)
|
||||
res += g[j][i].real*par[p][j];
|
||||
for(int j = 1; j <= p; j++) res /= j;
|
||||
if(res > 0.5) printf("%d: %.0f\n", i, res);
|
||||
}
|
||||
return;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
scanf("%d", &T);
|
||||
for(int cas = 1; cas <= T; cas++)
|
||||
{
|
||||
scanf("%d %d", &m, &p);
|
||||
for(int i = 1; i <= m; i++)
|
||||
scanf("%d", &a[i]);
|
||||
printf("Case #%d:\n", cas);
|
||||
solve();
|
||||
putchar('\n');
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,178 @@
|
|||
#include <iostream>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<algorithm>
|
||||
#include<cctype>
|
||||
using namespace std;
|
||||
const int N = 100005;
|
||||
typedef __int64 ll;
|
||||
struct node
|
||||
{
|
||||
int l,r,f,key,value;
|
||||
friend bool operator <(const struct node a,const struct node b)
|
||||
{
|
||||
return a.key < b.key;
|
||||
}
|
||||
}lcm[N];
|
||||
int n,m,root,num,maxdp;
|
||||
int stack[N];
|
||||
int dep[N];
|
||||
int head[N];
|
||||
struct nd
|
||||
{
|
||||
int to,next;
|
||||
}g[N];
|
||||
void add(int s,int e)
|
||||
{
|
||||
g[num].to = e;
|
||||
g[num].next = head[s];
|
||||
head[s] = num ++;
|
||||
}
|
||||
int build()
|
||||
{
|
||||
int top = -1;
|
||||
int i,j;
|
||||
top = -1;
|
||||
for(i = 1;i <= n;i ++)
|
||||
{
|
||||
j = top;
|
||||
while(j >= 0 && lcm[stack[j]].value > lcm[i].value)
|
||||
j --;
|
||||
if(j != -1)
|
||||
{
|
||||
lcm[i].f = stack[j];
|
||||
lcm[stack[j]].r = i;
|
||||
}
|
||||
if(j < top)
|
||||
{
|
||||
lcm[stack[j + 1]].f = i;
|
||||
lcm[i].l = stack[j + 1];
|
||||
}
|
||||
stack[++ j] = i;
|
||||
top = j;
|
||||
}
|
||||
lcm[stack[0]].f = -1;
|
||||
return stack[0];
|
||||
}
|
||||
void bfs(int root,int dp)
|
||||
{
|
||||
int front,rear;
|
||||
front = rear = 0;
|
||||
dep[root] = dp;
|
||||
add(dp,root);
|
||||
stack[rear ++] = root;
|
||||
while(front != rear)
|
||||
{
|
||||
int cur = stack[front ++];
|
||||
if(lcm[cur].l > 0)
|
||||
{
|
||||
dep[lcm[cur].l] = dep[cur] + 1;
|
||||
add(dep[cur] + 1,lcm[cur].l);
|
||||
stack[rear ++] = lcm[cur].l;
|
||||
if(maxdp < dep[cur] + 1)
|
||||
maxdp = dep[cur] + 1;
|
||||
}
|
||||
if(lcm[cur].r > 0)
|
||||
{
|
||||
dep[lcm[cur].r] = dep[cur] + 1;
|
||||
add(dep[cur] + 1,lcm[cur].r);
|
||||
stack[rear ++] = lcm[cur].r;
|
||||
if(maxdp < dep[cur] + 1)
|
||||
maxdp = dep[cur] + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
char ans[205][N + 205];
|
||||
void print(ll l,ll r,ll u,ll d)
|
||||
{
|
||||
ll i,j,k;
|
||||
for(i = u;i <= d;i ++)
|
||||
{
|
||||
memset(ans[i - u],0,sizeof(ans[i - u]));
|
||||
if(i & 1)
|
||||
{
|
||||
for(j = head[(i + 1)>>1];~j;j = g[j].next)
|
||||
{
|
||||
ans[i - u][g[j].to] = 'o';
|
||||
if(lcm[g[j].to].l != -1)
|
||||
{
|
||||
ans[i - u][lcm[g[j].to].l] = '+';
|
||||
for(k = lcm[g[j].to].l + 1;k < g[j].to;k ++)
|
||||
ans[i - u][k] = '-';
|
||||
}
|
||||
if(lcm[g[j].to].r != -1)
|
||||
{
|
||||
ans[i - u][lcm[g[j].to].r] = '+';
|
||||
for(k = lcm[g[j].to].r - 1;k > g[j].to;k --)
|
||||
ans[i - u][k] = '-';
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(j = head[i>>1];~j;j = g[j].next)
|
||||
{
|
||||
if(lcm[g[j].to].l != -1)
|
||||
ans[i - u][lcm[g[j].to].l] = '|';
|
||||
if(lcm[g[j].to].r != -1)
|
||||
ans[i - u][lcm[g[j].to].r] = '|';
|
||||
}
|
||||
}
|
||||
}
|
||||
for(i = 0;i <= d - u;i ++)
|
||||
{
|
||||
bool fuck = false;
|
||||
for(j = l;j <= r;j ++)
|
||||
if(ans[i][j])
|
||||
{
|
||||
fuck = true;
|
||||
break;
|
||||
}
|
||||
if(fuck == false)
|
||||
continue;
|
||||
for(j = l;j <= r;j ++)
|
||||
if(ans[i][j])
|
||||
putchar(ans[i][j]);
|
||||
else
|
||||
putchar(' ');
|
||||
putchar(10);
|
||||
}
|
||||
}
|
||||
void rush()
|
||||
{
|
||||
ll l,r,u,d;
|
||||
scanf("%d",&m);
|
||||
while(m --)
|
||||
{
|
||||
scanf("%I64d%I64d%I64d%I64d",&u,&l,&d,&r);
|
||||
d = u + d - 1;
|
||||
r = l + r - 1;
|
||||
if(d > maxdp + maxdp-1)
|
||||
d = maxdp + maxdp-1;
|
||||
print(l,r,u,d);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,t,cas = 0;
|
||||
scanf("%d",&t);
|
||||
while(t --)
|
||||
{
|
||||
scanf("%d",&n);
|
||||
for(i = num = 1;i <= n;i ++)
|
||||
{
|
||||
lcm[i].l = lcm[i].r = lcm[i].f = -1;
|
||||
head[i] = -1;
|
||||
scanf("%d",&lcm[i].key);
|
||||
lcm[i].value = i;
|
||||
}
|
||||
sort(lcm + 1,lcm + 1 + n);
|
||||
root = build();
|
||||
maxdp = 1;
|
||||
bfs(root,1);
|
||||
printf("Case #%d:\n",++cas);
|
||||
rush();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,171 @@
|
|||
#include <iostream>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<algorithm>
|
||||
#include<map>
|
||||
#include<string>
|
||||
using namespace std;
|
||||
const int N = 3005;
|
||||
const int M = 1000005;
|
||||
int head[N];
|
||||
struct node
|
||||
{
|
||||
int to,next;
|
||||
}e[M];
|
||||
bool vis[N];
|
||||
int num,id;
|
||||
char s1[10005],s[9][100];
|
||||
map<string,int>non,verb;
|
||||
bool flag;
|
||||
void build(int s,int ed)
|
||||
{
|
||||
e[num].to = ed;
|
||||
e[num].next = head[s];
|
||||
head[s] = num ++;
|
||||
}
|
||||
void gofind(int cur,int tag)
|
||||
{
|
||||
vis[cur] = true;
|
||||
if(flag)
|
||||
return;
|
||||
if(cur == tag)
|
||||
{
|
||||
flag = true;
|
||||
return;
|
||||
}
|
||||
int i;
|
||||
for(i = head[cur];~i;i = e[i].next)
|
||||
if(vis[e[i].to] == false)
|
||||
gofind(e[i].to,tag);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t,cas = 0;
|
||||
scanf("%d",&t);
|
||||
gets(s1);
|
||||
while(t --)
|
||||
{
|
||||
non.clear();verb.clear();
|
||||
memset(head,-1,sizeof(head));
|
||||
num = id = 1;
|
||||
printf("Case #%d:\n",++cas);
|
||||
while(gets(s1))
|
||||
{
|
||||
int len = strlen(s1);
|
||||
if(s1[len - 1] == '!')
|
||||
break;
|
||||
else if(s1[len - 1] == '.')
|
||||
{
|
||||
s1[len - 1] = '\0';
|
||||
if(sscanf(s1,"%s%s%s%s%s%s",s[0],s[1],s[2],s[3],s[4],s[5]) != 6)
|
||||
{
|
||||
sscanf(s1,"%s%s%s",s[2],s[3],s[4]);
|
||||
string a,b;
|
||||
a = s[2];b = s[4];
|
||||
if(non.find(a) == non.end())
|
||||
non[a] = id ++;
|
||||
if(s[3][0] == 'a')
|
||||
{
|
||||
if(non.find(b) == non.end())
|
||||
non[b] = id ++;
|
||||
build(non[a],non[b]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(verb.find(b) == verb.end())
|
||||
verb[b] = id ++;
|
||||
build(non[a],verb[b]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sscanf(s1,"%s%s%s%s%s%s",s[2],s[2],s[2],s[2],s[3],s[4]);
|
||||
string a,b;
|
||||
a = s[2];b = s[4];
|
||||
if(verb.find(a) == verb.end())
|
||||
verb[a] = id ++;
|
||||
if(s[3][0] == 'a')
|
||||
{
|
||||
if(non.find(b) == non.end())
|
||||
non[b] = id ++;
|
||||
build(verb[a],non[b]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(verb.find(b) == verb.end())
|
||||
verb[b] = id ++;
|
||||
build(verb[a],verb[b]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(s1[len -1] == '?')
|
||||
{
|
||||
memset(vis,false,sizeof(vis));
|
||||
s1[len - 1] = '\0';
|
||||
if(sscanf(s1,"%s%s%s%s%s%s",s[0],s[1],s[2],s[3],s[4],s[5]) != 6)
|
||||
{
|
||||
sscanf(s1,"%s%s%s",s[3],s[2],s[4]);
|
||||
string a,b;
|
||||
a = s[2];b = s[4];
|
||||
if(non.find(a) == non.end())
|
||||
non[a] = id ++;
|
||||
if(s[3][0] == 'a')
|
||||
{
|
||||
if(non.find(b) == non.end())
|
||||
non[b] = id ++;
|
||||
flag = false;
|
||||
gofind(non[a],non[b]);
|
||||
if(flag)
|
||||
putchar('Y');
|
||||
else
|
||||
putchar('M');
|
||||
}
|
||||
else
|
||||
{
|
||||
if(verb.find(b) == verb.end())
|
||||
verb[b] = id ++;
|
||||
flag = false;
|
||||
gofind(non[a],verb[b]);
|
||||
if(flag)
|
||||
putchar('Y');
|
||||
else
|
||||
putchar('M');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sscanf(s1,"%s%s%s%s%s%s",s[3],s[3],s[3],s[3],s[2],s[4]);
|
||||
string a,b;
|
||||
a = s[2];b = s[4];
|
||||
if(verb.find(a) == verb.end())
|
||||
verb[a] = id ++;
|
||||
sscanf(s1,"%s",s[3]);
|
||||
if(s[3][0] == 'a')
|
||||
{
|
||||
if(non.find(b) == non.end())
|
||||
non[b] = id ++;
|
||||
flag = false;
|
||||
gofind(verb[a],non[b]);
|
||||
if(flag)
|
||||
putchar('Y');
|
||||
else
|
||||
putchar('M');
|
||||
}
|
||||
else
|
||||
{
|
||||
if(verb.find(b) == verb.end())
|
||||
verb[b] = id ++;
|
||||
flag = false;
|
||||
gofind(verb[a],verb[b]);
|
||||
if(flag)
|
||||
putchar('Y');
|
||||
else
|
||||
putchar('M');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
putchar(10);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,170 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
const double eps = 1e-8;
|
||||
const double pi = acos(-1.0);
|
||||
int dcmp(double x) {
|
||||
if(fabs(x) < eps)
|
||||
return 0;
|
||||
return x > eps ? 1 : -1;
|
||||
}
|
||||
struct point {
|
||||
double x, y;
|
||||
point(double x = 0, double y = 0) :
|
||||
x(x), y(y) {
|
||||
}
|
||||
point operator-(const point &t) const {
|
||||
return point(x - t.x, y - t.y);
|
||||
}
|
||||
point operator*(const double &t) const {
|
||||
return point(x * t, y * t);
|
||||
}
|
||||
void in() {
|
||||
scanf("%lf%lf", &x, &y);
|
||||
}
|
||||
} p[4];
|
||||
double dis(point a, point b) {
|
||||
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
|
||||
}
|
||||
double angle(double a, double b, double c) {
|
||||
return acos((b * b + c * c - a * a) / (2 * b * c));
|
||||
}
|
||||
struct tri {
|
||||
point p[3];
|
||||
double d[3], jiao[3];
|
||||
void in() {
|
||||
int i;
|
||||
for(i = 0; i < 3; i++)
|
||||
p[i].in();
|
||||
for(i = 0; i < 3; i++)
|
||||
d[i] = dis(p[i], p[(i + 1) % 3]);
|
||||
sort(d, d + 3);
|
||||
for(i = 0; i < 3; i++)
|
||||
jiao[i] = angle(d[i], d[(i + 1) % 3], d[(i + 2) % 3]);
|
||||
}
|
||||
} t1, t2;
|
||||
double cross(point a, point b) {
|
||||
return a.x * b.y - a.y * b.x;
|
||||
}
|
||||
bool judge(int x, int y, int a, int b) {
|
||||
int f1 = dcmp(cross(p[y] - p[x], p[a] - p[x]));
|
||||
int f2 = dcmp(cross(p[y] - p[x], p[b] - p[x]));
|
||||
if(!f1 || !f2)
|
||||
return 0;
|
||||
if(f1 != f2)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
bool equal(point a, point b, point c, tri t) {
|
||||
point p[3];
|
||||
double d[3];
|
||||
p[0] = a;
|
||||
p[1] = b;
|
||||
p[2] = c;
|
||||
int i;
|
||||
for(i = 0; i < 3; i++)
|
||||
d[i] = dis(p[i], p[(i + 1) % 3]);
|
||||
sort(d, d + 3);
|
||||
for(i = 0; i < 3; i++)
|
||||
if(d[i] != t.d[i])
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
bool Cao(double a, double b, double c, double *dd) {
|
||||
double d[4];
|
||||
d[0] = a;
|
||||
d[1] = b;
|
||||
d[2] = c;
|
||||
sort(d, d + 3);
|
||||
for(int i = 0; i < 3; i++)
|
||||
if(dcmp(d[i] - dd[i]))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
typedef pair <double, double> pii;
|
||||
int main() {
|
||||
int i, j, cas, ca = 1;
|
||||
scanf("%d", &cas);
|
||||
while(cas--) {
|
||||
t1.in();
|
||||
t2.in();
|
||||
for(i = 0; i < 4; i++)
|
||||
p[i].in();
|
||||
printf("Case #%d: ", ca++);
|
||||
bool ok = 0;
|
||||
if(judge(0, 2, 1, 3)) {
|
||||
if(!ok && equal(p[0], p[1], p[2], t1)
|
||||
&& equal(p[0], p[3], p[2], t2))
|
||||
ok = 1;
|
||||
if(!ok && equal(p[0], p[1], p[2], t2)
|
||||
&& equal(p[0], p[3], p[2], t1))
|
||||
ok = 1;
|
||||
}
|
||||
if(judge(1, 3, 0, 2)) {
|
||||
if(!ok && equal(p[1], p[0], p[3], t1)
|
||||
&& equal(p[1], p[2], p[3], t2))
|
||||
ok = 1;
|
||||
if(!ok && equal(p[1], p[0], p[3], t2)
|
||||
&& equal(p[1], p[2], p[3], t1))
|
||||
ok = 1;
|
||||
}
|
||||
if(!ok) {
|
||||
int i, j, k;
|
||||
point q[4];
|
||||
int pos = -1;
|
||||
for(i = 0; i < 4; i++)
|
||||
if(!dcmp(
|
||||
cross(p[i] - p[(i + 2) % 4],
|
||||
p[(i + 1) % 4] - p[(i + 2) % 4]))) {
|
||||
pos = (i + 1) % 4;
|
||||
break;
|
||||
}
|
||||
loop: int tot = 0;
|
||||
if(pos != -1) {
|
||||
for(i = 0; i < 4; i++)
|
||||
if(i != pos) {
|
||||
q[tot++] = p[i];
|
||||
}
|
||||
for(i = 0; i < 3; i++)
|
||||
p[i] = q[i];
|
||||
double area1 = fabs(cross(t1.p[0] - t1.p[1], t1.p[0] - t1.p[2]))
|
||||
+ fabs(cross(t2.p[0] - t2.p[1], t2.p[0] - t2.p[2]));
|
||||
double area2 = fabs(cross(p[0] - p[1], p[2] - p[1]));
|
||||
if(!dcmp(area1 - area2)) {
|
||||
double dd[4];
|
||||
for(i = 0; i < 3; i++)
|
||||
dd[i] = dis(p[i], p[(i + 1) % 3]);
|
||||
sort(dd, dd + 3);
|
||||
for(i = 0; i < 3; i++)
|
||||
for(j = 0; j < 3; j++)
|
||||
if(!dcmp(t1.d[i] - t2.d[j])) {
|
||||
int x, y;
|
||||
vector <pii> la, lb;
|
||||
for(x = 0; x < 3; x++)
|
||||
if(x != i)
|
||||
la.push_back(
|
||||
make_pair(t1.d[x], t1.jiao[x]));
|
||||
for(x = 0; x < 3; x++)
|
||||
if(x != j)
|
||||
lb.push_back(
|
||||
make_pair(t2.d[x], t2.jiao[x]));
|
||||
for(x = 0; x < 2; x++)
|
||||
for(y = 0; y < 2; y++) {
|
||||
if(!dcmp(la[x].second + lb[y].second- pi)
|
||||
&& Cao(la[x].first, lb[y].first, la[x ^ 1].first + lb[y ^ 1].first, dd))
|
||||
ok = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(ok)
|
||||
puts("Yes");
|
||||
else
|
||||
puts("No");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
#include <iostream>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
using namespace std;
|
||||
const int N=10;
|
||||
int f1[65],f2[65],f3[65];
|
||||
class Trie
|
||||
{
|
||||
public:
|
||||
int v;
|
||||
int flag;
|
||||
Trie *next[N];
|
||||
Trie()
|
||||
{
|
||||
v=-1;
|
||||
memset(next,NULL,sizeof(next));
|
||||
}
|
||||
};
|
||||
Trie *root;
|
||||
void Insert(char *S,int ans)
|
||||
{
|
||||
int len=strlen(S);
|
||||
Trie *p=root;
|
||||
for(int i=0;i<len;i++)
|
||||
{
|
||||
int id=S[i]-'0';
|
||||
if(p->next[id]==NULL)
|
||||
p->next[id]=new Trie();
|
||||
p=p->next[id];
|
||||
if(p->v<0) p->v=ans;
|
||||
}
|
||||
}
|
||||
int Find(char *S)
|
||||
{
|
||||
Trie *p=root;
|
||||
int count;
|
||||
int len=strlen(S);
|
||||
for(int i=0;i<len;i++)
|
||||
{
|
||||
int id=S[i]-'0';
|
||||
p=p->next[id];
|
||||
if(p==NULL) return -1;
|
||||
else count=p->v;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
void Init()
|
||||
{
|
||||
int h;
|
||||
char str[65]="1";
|
||||
memset(f1,0,sizeof(f1));
|
||||
memset(f2,0,sizeof(f2));
|
||||
memset(f3,0,sizeof(f3));
|
||||
f1[0]=1;f2[0]=1;
|
||||
Insert(str,0);
|
||||
for(int i=2;i<100000;i++)
|
||||
{
|
||||
memset(str,0,sizeof(str));
|
||||
int r=0;
|
||||
for(int j=0;j<60;j++)
|
||||
{
|
||||
f3[j]=f1[j]+f2[j]+r;
|
||||
r=f3[j]/10;
|
||||
f3[j]%=10;
|
||||
}
|
||||
for(int j=59;j>=0;j--)
|
||||
if(f3[j])
|
||||
{
|
||||
h=j;
|
||||
break;
|
||||
}
|
||||
int k=0;
|
||||
for(int j=h;j>=0;j--)
|
||||
{
|
||||
str[k++]=f3[j]+'0';
|
||||
if(k>=40) break;
|
||||
}
|
||||
Insert(str,i);
|
||||
if(h>55)
|
||||
{
|
||||
for(int j=1;j<59;j++)
|
||||
f3[j-1]=f3[j];
|
||||
for(int j=1;j<59;j++)
|
||||
f2[j-1]=f2[j];
|
||||
}
|
||||
for(int j=0;j<60;j++)
|
||||
f1[j]=f2[j];
|
||||
for(int j=0;j<60;j++)
|
||||
f2[j]=f3[j];
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
root=new Trie();
|
||||
Init();
|
||||
char str[105];
|
||||
int t,i,j,k=1;
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
scanf("%s",str);
|
||||
printf("Case #%d: ",k++);
|
||||
int tmp=Find(str);
|
||||
printf("%d\n",tmp);
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue