0924解题报告

T1    

题目描述

你有 nm 颗糖果。如果有 n 个小朋友来你家,你需要分给他们每人 m 颗糖;如果来的是m 个小朋友,你就要分给每人 n 颗。你打算把糖果分装在 k 个盒子里,每个盒子分别放几颗糖由你来决定。分糖果时,盒子是不能被拆开的。(小朋友们拿到的只能是装着糖果的盒子,而不是散装的糖果)你希望最小化盒子的数量 k,使得不论来了 n 个还是 m 个小朋友,你都能按照要求给他们分糖果。

一个很魔性的题,不知道怎么就A了。。。(只是在gcd中加了点东西)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<ctime>
 6 #include<cmath>
 7 #include<algorithm>
 8 using namespace std;
 9 int n,m,t,ans;
10 void gcd(int a,int b)
11 {
12     if(b==0)  return;
13     gcd(b,a%b);
14     ans+=b*(a/b);
15 }
16 void Gcd(int a,int b)
17 {
18     if(b==0)  return;
19     for(int i=1;i<=a/b*b;i++)  printf("%d ",b);
20     Gcd(b,a%b);
21 }
22 inline int read()
23 {
24     int x=0,f=1;  char ch=getchar();
25     while(!isdigit(ch))  {if(ch=='-')  f=-1;  ch=getchar();}
26     while(isdigit(ch))  {x=x*10+ch-'0';  ch=getchar();}
27     return x*f;
28 }
29 int main()
30 {
31     freopen("candy.in","r",stdin);
32     freopen("candy.out","w",stdout);
33     n=read();  m=read();  t=read();
34     if(t==0)
35     {
36         if(n<m)  swap(n,m);
37         gcd(n,m);
38         printf("%d
",ans);
39     }
40     else 
41     {
42         if(n<m)  swap(n,m);
43         gcd(n,m);
44         printf("%d
",ans);
45         Gcd(n,m);
46         printf("
");
47     }
48     return 0;
49 }

T2    

题目描述

Byteotia 由 n 座城市组成。城市之间连有 m 条道路,道路可以双向通行。其中第 i 条道路连接 ui; vi 两座城市,通过这条道路需要花费 ti 个小时。城市和道路都从 1 开始编号。Byteotia 的计时方法比较奇特,一天共有 K 个小时。我们假定一天的起始时刻是 0 点整。Byteasar 打算在某天的 0 点整从城市 x 出发,前往城市 y。旅途中只能沿着道路行走,而不允许原地休息。Byteasar 不在乎自己的旅行花费了多少天,他只希望到达 y 的时刻在一天中尽可能早,即如果在某天的 T 点整 (0  T < K) 到达城市 y,他希望使得 T 尽可能小。为了达到这一目标,Byteasar 的旅行路径中允许多次经过同一条道路,也允许多次经过同一个城市(包括 x; y)。如果多次经过 y,最后一次到达 y 的时刻才算作到达时刻。Byteasar 可能有多组旅行计划,他想寻求你的帮助。Byteotia 的计时方法也常常改变,所以你需要对每一组 xj ; yj ;Kj 求出最小的 T。

问题实质是求出 x 到 y 的路径,路径长度模 K 后最小。

于是随便用二分图搞一搞,然而wa了几组,不知道为什么。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<ctime>
 6 #include<cmath>
 7 #include<algorithm>
 8 using namespace std;
 9 #define MAXN 50010
10 struct node{int y,next,v;}e[MAXN*2];
11 int n,m,k,len,flag,f[MAXN],Link[MAXN],g[MAXN],c[MAXN],check[MAXN];
12 inline int read()
13 {
14     int x=0,f=1;  char ch=getchar();
15     while(!isdigit(ch))  {if(ch=='-')  f=-1;  ch=getchar();}
16     while(isdigit(ch))  {x=x*10+ch-'0';  ch=getchar();}
17     return x*f;
18 }
19 int find(int x)  {return f[x]==x?x:f[x]=find(f[x]);}
20 int gcd(int a,int b)  {return b==0?a:gcd(b,a%b);}
21 void insert(int x,int y,int v)  {e[++len].next=Link[x];  Link[x]=len;  e[len].y=y;  e[len].v=v;}
22 bool color(int x,int cl)
23 {
24     c[x]=cl;
25     for(int i=Link[x];i;i=e[i].next)
26     {
27         if(!c[e[i].y])  
28         {
29             if(!color(e[i].y,3-cl))  return 0;
30         }
31         else if(c[e[i].y]==cl)  return 0;
32     }
33     return 1;
34 }
35 int main()
36 {
37     freopen("pod.in","r",stdin);
38     freopen("pod.out","w",stdout);
39     n=read();  m=read();  k=read();
40     for(int i=1;i<=n;i++)  f[i]=i;
41     for(int i=1;i<=m;i++)
42     {
43         int x=read(),y=read(),v=read();
44         if(rand()%2)  swap(x,y);
45         insert(x,y,v);  insert(y,x,v);
46         x=find(x);  y=find(y);
47         f[x]=y;  g[y]=gcd(gcd(g[y],g[x]),v);
48     }
49     for(int i=1;i<=n;i++)
50         if(!c[i])
51             if(!color(i,1))  check[find(i)]=1;
52     for(int i=1;i<=k;i++)
53     {
54         int x=read(),y=read(),mo=read();
55         int d=gcd(g[find(y)],mo);
56         if(find(x)!=find(y))  printf("NIE
");
57         else if((mo/d)&1)printf("0
");
58         else if(check[find(x)])  printf("0
");
59         else if(c[x]==c[y])  printf("0
");
60         else printf("%d
",d);
61     }
62     return 0;
63 }

T3

最近 JC 同学刚学会 gcd,于是迷上了与 gcd 有关的问题。今天他又出了一道这样的题目,
想要考考你,你能顺利完成吗?
给定一个长度为 n 的字符串 s[1::n],串仅包含小写字母。对于区间 [l; r],你需要回答 s[l::r]
中有多少个长度为 3 的子序列组成了"gcd",即有多少组 (i; j; k) 满足 l  i < j < k  r; s[i] =
'g'; s[j] = 'c'; s[k] = 'd'。

一个奇怪的前缀和维护。(各种前缀和,各种恶心)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<algorithm>
using namespace std;
int n,sl[80010],sr[80010],ssl[80010],ssr[80010],slr[80010],su[80010];
char ch[80010];
inline int read()
{
    int x=0,f=1;  char ch=getchar();
    while(!isdigit(ch))  {if(ch=='-')  f=-1;  ch=getchar();}
    while(isdigit(ch))  {x=x*10+ch-'0';  ch=getchar();}
    return x*f;
}
int main()
{
    freopen("gcd.in","r",stdin);
    freopen("gcd.out","w",stdout);
    scanf("%s",ch+1);  n=strlen(ch+1);
    for(int i=1;i<=n;i++)  sl[i]=sl[i-1]+(ch[i]=='g');
    for(int i=n;i;i--)     sr[i]=sr[i+1]+(ch[i]=='d');
    for(int i=1;i<=n;i++)
    {
        slr[i]=slr[i-1]+(ch[i]=='c')*sl[i]*sr[i];
        ssl[i]=ssl[i-1]+(ch[i]=='c')*sl[i];
        ssr[i]=ssr[i-1]+(ch[i]=='c')*sr[i];
        su[i]=su[i-1]+(ch[i]=='c');
    }
    int q=read();
    while(q--)
    {
        int x=read(),y=read();
        int ans=slr[y]-slr[x-1]-(ssr[y]-ssr[x-1])*sl[x-1]-(ssl[y]-ssl[x-1])*sr[y+1]+(su[y]-su[x-1])*sl[x-1]*sr[y+1];
        ans&=(~(1<<31));
        printf("%d
",ans);
    }
    return 0;
}

考试前被Cydiater坑了一把,没有在现场参加考试,只能在家颓废,最后只交了一道题,还好,长者保佑,A掉了。

然而写题时,T2和T3都没有想出正解,由此可见我还是很弱,与此同时Cydiater现场A掉2题。

原文地址:https://www.cnblogs.com/chty/p/5905887.html