关闭

ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) 题解(A-E)

标签: codeforces
275人阅读 评论(0) 收藏 举报
分类:

A A Serial Killer

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
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("A.in","r",stdin);
//  freopen(".out","w",stdout);
    string s1,s2;
    cin>>s1>>s2;
    cout<<s1<<' '<<s2<<endl;
    int n=read(); 
    For(i,n) {
        string s3,s4;
        cin>>s3>>s4;
        if (s3==s1) s1=s4;
        else s2=s4;
    cout<<s1<<' '<<s2<<endl;
    }


    return 0;
}

B Sherlock and his girlfriend

质数和合数染不同颜色就行
注意特判n=2,3的情况

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
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;
} 
#define MAXN (100000+10)
int p[MAXN],tot;
bool b[MAXN]={0};
void make_prime(int n)
{
    tot=0;
    Fork(i,2,n)
    {
        if (!b[i]) p[++tot]=i;
        For(j,tot)
        {
            if (i*p[j]>n) break;
            b[i*p[j]]=1;
            if (i%p[j]==0) break;  
        }
    }
}
int a[MAXN];
int main()
{
//  freopen("B.in","r",stdin);
//  freopen(".out","w",stdout);

    int n=1+read();
    make_prime(n+1);
    if (n<=3) cout<<"1\n";else cout<<"2\n";
    Fork(i,2,n-1) if (!b[i]) cout<<"1 ";else cout<<"2 ";
    if (!b[n]) cout<<"1\n";else cout<<"2\n";

    return 0;
}

C Molly’s Chemicals

经典题

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
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;
} 
#define MAXN (123456)
ll a[MAXN]={};
int n;
ll k;
vector<ll> v;
map<ll,int> S;
int main()
{
//  freopen("C.in","r",stdin);
//  freopen(".out","w",stdout);
    cin>>n>>k;
    For(i,n) cin>>a[i];

    Fork(i,2,n) {
        a[i]+=a[i-1];
    }
    ll p=1e16;
    ll c=1;
    v.pb(1);
    if (k==-1) v.pb(-1);
    else if (k!=1) {
        while(c<=p) {
            c*=k;
             v.pb(c);
        }
    }
    int sz=SI(v);
//  Rep(i,sz) cout<<v[i]<<' ';cout<<endl;
    ll ans=0;
    For(i,n) {
        if (S.count(a[i-1])) S[a[i-1]]++;else S[a[i-1]]=1;
        Rep(j,sz) {
            ll pp=a[i]-v[j];
            if (S.count(pp)) ans+=S[pp];
        }
    }
    cout<<ans<<endl;
    return 0;
}

D The Door Problem

经典题
以下解法均可解决问题:2-sat,并查集,dfs判连通,二分图染色。
判断一幅图有没有奇环的方法怎么有这么多
还有更狠的
这也可以系列
PS:n,m敲反了FST好气啊

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
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;
} 
#define MAXN (223456) 
class bingchaji
{
public:
    int father[MAXN],n,cnt;
    void mem(int _n)
    {
        n=cnt=_n;
        For(i,n) father[i]=i;
    }
    int getfather(int x) 
    {
        if (father[x]==x) return x;

        return father[x]=getfather(father[x]);
    }
    void unite(int x,int y)
    {
        x=getfather(x);
        y=getfather(y);
        if (x^y) {
            --cnt;
            father[x]=y;
        }
    }
    bool same(int x,int y)
    {
        return getfather(x)==getfather(y);
    }
}S;
int a[MAXN];
int f[MAXN][3]={0};
int main()
{
//  freopen("D.in","r",stdin);
//  freopen(".out","w",stdout);
    int n=read(),m=read();
    S.mem(2*m);
    For(i,n) a[i]=read();
    For(i,m) {
        int x=read();
        For(j,x) {
            int p=read();
            f[p][++f[p][0]]=i;
        }
    }
    n=m;
    For(p,n) {
        if (a[p]==0) S.unite(f[p][2],n+f[p][1]),S.unite(f[p][1],n+f[p][2]);
        else S.unite(f[p][2],f[p][1]),S.unite(n+f[p][1],n+f[p][2]);
    }
    bool fl=0;
    For(i,n) if (!a[i] &&S.same(i,n+i)) fl=1;
    if(fl) puts("NO");else puts("YES"); 
    return 0;
}

E The Holmes Children

f(i)=ϕ(i),g(i)=i
原题转变为求ϕ(ϕ(ϕ(...ϕ(n)...)))

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
                        For(j,m-1) cout<<a[i][j]<<' ';\
                        cout<<a[i][m]<<endl; \
                        } 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
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;
} 
ll phi(ll n) {
    ll i,temp;
    temp=n;
    for(i=2;i*i<=n;i++)
    {
      if(n%i==0)
      {
          while(n%i==0) n=n/i;
          temp=temp/i*(i-1);
      }
      if(n<i+1)
          break;
    }
    if(n>1)
        temp=temp/n*(n-1);
    return temp;
}
ll n,k;
int main()
{
//  freopen("E.in","r",stdin);
//  freopen(".out","w",stdout);
    cin>>n>>k;
    for(ll i=1;i<=k;i+=2) {
        n=phi(n);
        if (n==1) break;
    }
    cout<<n%1000000007LL<<endl;
    return 0;
}
0
0
查看评论

ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) E. The Holmes Children

E. The Holmes Children time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard ...
  • mystery_guest
  • mystery_guest
  • 2017-03-04 18:40
  • 284

Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined)A+B

A. Gotta Catch Em’ All!time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputoutput:standard outputBash wants to become a...
  • qq_33183401
  • qq_33183401
  • 2017-01-16 17:12
  • 241

Codeforces Round #400 (Div. 1 + Div. 2, combined)A. A Serial Killer【模拟】

A. A Serial Killer time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Our belove...
  • mengxiang000000
  • mengxiang000000
  • 2017-02-24 12:00
  • 292

Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem(二分染色?/2-sat,好题)

题目链接 D. The Door Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard input output stan...
  • fouzhe
  • fouzhe
  • 2017-02-24 20:57
  • 283

Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) A. Gotta Catch Em' All!(水题)

题目链接:http://codeforces.com/contest/757/problem/AA. Gotta Catch Em’ All! time limit per test1 second memory limit per test256 megabytes inputstandar...
  • qq_32866009
  • qq_32866009
  • 2017-01-13 09:17
  • 200

解题报告:Codeforces Round #433 (Div. 2) E. Boredom ( 离线处理+树状数组)

题目链接 题意: n*n的矩阵,有n个不同行列的格子染色,染色的格子两两之间组成的矩阵定义为beautiful。 q组询问,每次给出一个矩阵,询问与它相交的beautiful的矩阵的数目 n,q 思路: 每次查询分成九个矩阵,只需要知道各个矩阵中的染色的点数,即可得出答案 其中...
  • qq_32570675
  • qq_32570675
  • 2017-09-07 09:56
  • 338

Codeforces Round #258 (Div. 2)-(A,B,C,D,E)

A:Game With Sticks 水题。。。每次操作,都会拿走一个横行,一个竖行。 所以一共会操作min(横行,竖行)次。 #include #include #include #include #include #include #include #include #pragma commen...
  • rowanhaoa
  • rowanhaoa
  • 2014-07-25 14:39
  • 1474

【解题报告】Codeforces Round #401 (Div. 2)

简略的解题报告。
  • TRiddle
  • TRiddle
  • 2017-02-25 17:55
  • 411

Codeforces Round #395 (Div. 2)(ABCD)

ps:打完这场cf才知道自己真的很菜,还是停留在AB题的水平,有时候CD其实很简单,但就是想不到,别人一眼看出而我就是想不到,有时候想到了点子上但就是突破不了 题目链接:  Codeforces Round #395 (Div. 2) A题: &...
  • qq_34731703
  • qq_34731703
  • 2017-02-03 12:50
  • 492

Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C. Felicity is Coming!组合学+集合

题意:给出n组数,每组gi个数,每个数属于1~m,每个数可以变化但变化前相同的数变化后依然相同,变化前不同的速变化后依然不同,且可能不变,但经过变化后每组的每种数的个数不会变化,求变化的总方案数。 组合学+集合 //这题是根据Codeforces的官方题解写的 把1~m分成一些集合,在同一个集合里的...
  • ProLightsfxjh
  • ProLightsfxjh
  • 2017-01-14 01:28
  • 569
    个人资料
    • 访问:985721次
    • 积分:20310
    • 等级:
    • 排名:第485名
    • 原创:1028篇
    • 转载:16篇
    • 译文:0篇
    • 评论:33条
    博客导航
    文章分类
    最新评论