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

来源:互联网 发布:linux tail f 搜索 编辑:程序博客网 时间:2024/05/21 15:38
C. Molly's Chemicals
time limit per test
2.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an affection value, The i-th of them has affection value ai.

Molly wants Sherlock to fall in love with her. She intends to do this by mixing a contiguous segment of chemicals together to make a love potion with total affection value as a non-negative integer power of k. Total affection value of a continuous segment of chemicals is the sum of affection values of each chemical in that segment.

Help her to do so in finding the total number of such segments.

Input

The first line of input contains two integers, n and k, the number of chemicals and the number, such that the total affection value is a non-negative power of this number k. (1 ≤ n ≤ 1051 ≤ |k| ≤ 10).

Next line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — affection values of chemicals.

Output

Output a single integer — the number of valid segments.

Examples
input
4 22 2 2 2
output
8
input
4 -33 -6 -3 12
output
3
Note

Do keep in mind that k0 = 1.

In the first sample, Molly can get following different affection values:

  • 2: segments [1, 1][2, 2][3, 3][4, 4];

  • 4: segments [1, 2][2, 3][3, 4];

  • 6: segments [1, 3][2, 4];

  • 8: segments [1, 4].

Out of these, 24 and 8 are powers of k = 2. Therefore, the answer is 8.

In the second sample, Molly can choose segments [1, 2][3, 3][3, 4].

这题就是求,有多少个连续和,刚好是k的幂次。

思路:

我们此时需要一个long long数组来存下前缀和。用一个map来存某个和的值出现的次数。最后,我们再考虑下-1和1的情况就可以了。

可能大家会想到,如果处理出所有的区间和,那肯定是n^2,那我们可以以k的幂次来循环,然后内层循环套个n,这样复杂度就是log(1e14)*n

#include<cstdio>#include<cstdlib>#include<iostream>#include<stack>#include<queue>#include<algorithm>#include<string>#include<cstring>#include<cmath>#include<vector>#include<map>#include<set>#define eps 1e-8#define zero(x) (((x>0?(x):-(x))-eps)#define mem(a,b) memset(a,b,sizeof(a))#define memmax(a) memset(a,0x3f,sizeof(a))#define pfn printf("\n")#define ll __int64#define ull unsigned long long#define sf(a) scanf("%d",&a)#define sf64(a) scanf("%I64d",&a)#define sf264(a,b) scanf("%I64d%I64d",&a,&b)#define sf364(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)#define sf2(a,b) scanf("%d%d",&a,&b)#define sf3(a,b,c) scanf("%d%d%d",&a,&b,&c)#define sf4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)#define sff(a) scanf("%f",&a)#define sfs(a) scanf("%s",a)#define sfs2(a,b) scanf("%s%s",a,b)#define sfs3(a,b,c) scanf("%s%s%s",a,b,c)#define sfd(a) scanf("%lf",&a)#define sfd2(a,b) scanf("%lf%lf",&a,&b)#define sfd3(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)#define sfd4(a,b,c,d) scanf("%lf%lf%lf%lf",&a,&b,&c,&d)#define sfc(a) scanf("%c",&a)#define ull unsigned long long#define pp pair<int,int>#define debug printf("***\n")#define pi 3.1415927#define mod 1000000007const double PI = acos(-1.0);const double e = exp(1.0);const int INF = 0x7fffffff;;template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }template<class T> inline T Min(T a, T b) { return a < b ? a : b; }template<class T> inline T Max(T a, T b) { return a > b ? a : b; }bool cmpbig(int a, int b){ return a>b; }bool cmpsmall(int a, int b){ return a<b; }using namespace std;int main(){   // freopen("data.in","r",stdin);    //freopen("data.out" ,"w",stdout);    int n,k;    while(~sf2(n,k))    {        int i,a[100010];        for(i=0;i<n;i++)            sf(a[i]);        ll power=1;        map<ll,ll>mp;        ll cnt=0;        while(abs(power)<=1e14)        {            mp.clear();            mp[0]=1;            ll pos=0;            for(i=0;i<n;i++)            {                pos+=a[i];                cnt+=mp[pos-power];                mp[pos]++;            }            power*=k;            if(power==1)                break;        }        printf("%I64d\n",cnt);    }    return 0;}

D. The Door Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Moriarty has trapped n people in n distinct rooms in a hotel. Some rooms are locked, others are unlocked. But, there is a condition that the people in the hotel can only escape when all the doors are unlocked at the same time. There are m switches. Each switch control doors of some rooms, but each door is controlled by exactly two switches.

You are given the initial configuration of the doors. Toggling any switch, that is, turning it ON when it is OFF, or turning it OFF when it is ON, toggles the condition of the doors that this switch controls. Say, we toggled switch 1, which was connected to room 12 and 3 which were respectively locked, unlocked and unlocked. Then, after toggling the switch, they become unlocked, locked and locked.

You need to tell Sherlock, if there exists a way to unlock all doors at the same time.

Input

First line of input contains two integers n and m (2 ≤ n ≤ 1052 ≤ m ≤ 105) — the number of rooms and the number of switches.

Next line contains n space-separated integers r1, r2, ..., rn (0 ≤ ri ≤ 1) which tell the status of room doors. The i-th room is locked if ri = 0, otherwise it is unlocked.

The i-th of next m lines contains an integer xi (0 ≤ xi ≤ n) followed by xi distinct integers separated by space, denoting the number of rooms controlled by the i-th switch followed by the room numbers that this switch controls. It is guaranteed that the room numbers are in the range from 1 to n. It is guaranteed that each door is controlled by exactly two switches.

Output

Output "YES" without quotes, if it is possible to open all doors at the same time, otherwise output "NO" without quotes.

Examples
input
3 31 0 12 1 32 1 22 2 3
output
NO
input
3 31 0 13 1 2 31 22 1 3
output
YES
input
3 31 0 13 1 2 32 1 21 3
output
NO
Note

In the second example input, the initial statuses of the doors are [1, 0, 1] (0 means locked, 1 — unlocked).

After toggling switch 3, we get [0, 0, 0] that means all doors are locked.

Then, after toggling switch 1, we get [1, 1, 1] that means all doors are unlocked.

It can be seen that for the first and for the third example inputs it is not possible to make all doors unlocked.

题目描述:有n道门,m个开关,每道门由m个开关中的两个控制,若门的状态是0(关),那么改变控制这道门的一个开关之后,门的状态就变为1(开),若门的状态是1(开),那么改变控制这道门的一个开关之后,门的状态就变为0(关)。 
现已知n道门的初始状态,以及m个开关分别控制哪些门,问能不能通过按下某些开关,使得全部门的状态都变为1。 

思路:这题我是用2SAT来处理的,难点在于如何建边才能满足2SAT的条件。我们可以将一个开关分成两种状态,开是一种,关是一种,这样肯定满足了2SAT的条件,然后关键在于题目的要求,如果某个灯初始状态为0,则控制其的两个开关x,y需满足(!x ^ y) v (x ^ !y),但这不是2SAT的形式,我们进一步化:((!x ^ y) v x) ^ ((!x ^ y) v !y)  -> ((!x v x) ^(y  v x)) ^ ((!x ^ y) v !y)...最后的结果就是(x v y) ^ (!x v !y),然后我们按照这个结果建边即可。

总结:对于2SAT问题,化为(* v *) ^ (* v *) 这样的形式,然后进行建边,跑一遍2SAT即可。

#include<cstdio>#include<cstdlib>#include<iostream>#include<stack>#include<queue>#include<algorithm>#include<string>#include<cstring>#include<cmath>#include<vector>#include<map>#include<set>#define eps 1e-8#define zero(x) (((x>0?(x):-(x))-eps)#define mem(a,b) memset(a,b,sizeof(a))#define memmax(a) memset(a,0x3f,sizeof(a))#define pfn printf("\n")#define ll __int64#define ull unsigned long long#define sf(a) scanf("%d",&a)#define sf64(a) scanf("%I64d",&a)#define sf264(a,b) scanf("%I64d%I64d",&a,&b)#define sf364(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)#define sf2(a,b) scanf("%d%d",&a,&b)#define sf3(a,b,c) scanf("%d%d%d",&a,&b,&c)#define sf4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)#define sff(a) scanf("%f",&a)#define sfs(a) scanf("%s",a)#define sfs2(a,b) scanf("%s%s",a,b)#define sfs3(a,b,c) scanf("%s%s%s",a,b,c)#define sfd(a) scanf("%lf",&a)#define sfd2(a,b) scanf("%lf%lf",&a,&b)#define sfd3(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)#define sfd4(a,b,c,d) scanf("%lf%lf%lf%lf",&a,&b,&c,&d)#define sfc(a) scanf("%c",&a)#define ull unsigned long long#define pp pair<int,int>#define debug printf("***\n")#define pi 3.1415927#define mod 1000000007const double PI = acos(-1.0);const double e = exp(1.0);const int INF = 0x7fffffff;;template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }template<class T> inline T Min(T a, T b) { return a < b ? a : b; }template<class T> inline T Max(T a, T b) { return a > b ? a : b; }bool cmpbig(int a, int b){ return a>b; }bool cmpsmall(int a, int b){ return a<b; }using namespace std;#define MAXN 100010int door[MAXN];vector<int>flag[2*MAXN];struct twosat{    vector<int>maps[2*MAXN];    bool vis[2*MAXN];    int vis_pos[2*MAXN];    int n,k;    bool dfs(int x)    {        //printf("%d\n",x);        if(vis[x^1]) return false; //如果i和i+1同处一个强联通分量,则肯定是不可行的        if(vis[x]) return true; //判断环        vis_pos[k++]=x;        vis[x]=true;        for(int i=0;i<maps[x].size();i++)            if(!dfs(maps[x][i]))                return false;        return true;    }    void init(int n)    {        this->n=n;        for(int i=0;i<2*n;i++)            maps[i].clear();        mem(vis,0);    }    void addedge(int x,int xval,int y,int yval)//边的输入就是按照2-SAT对称性    {        x=x*2+xval;        y=y*2+yval;        maps[x].push_back(y^1);        maps[y].push_back(x^1);    }    bool solve()    {        for(int i=0;i<2*n;i+=2)        {            if(!vis[i]&&!vis[i^1])            {                k=0;                if(!dfs(i))                {                    while(k>0)                        vis[vis_pos[--k]]=false;                    if(!dfs(i^1))//如果第i个人不能组成这样的集合,且与他同组的第i^1个人也不能组成,则答案不存在                        return false;                }            }        }        return true;    }};int main(){    //freopen("data.in","r",stdin);    //freopen("data.out" ,"w",stdout);    int n,m;    while(~sf2(n,m))    {        int i,j;        for(i=0;i<n;i++)            sf(door[i]);        for(i=0;i<2*n;i++)            flag[i].clear();        twosat pro;        for(i=0;i<m;i++)        {            int posnum;            sf(posnum);            for(j=0;j<posnum;j++)            {                int pos;                sf(pos);                flag[pos-1].push_back(i);            }        }        pro.init(m);        for(i=0;i<n;i++)        {            if(!door[i])            {                pro.addedge(flag[i][0],0,flag[i][1],0);                pro.addedge(flag[i][0],1,flag[i][1],1);            }            else            {                pro.addedge(flag[i][0],1,flag[i][1],0);                pro.addedge(flag[i][0],0,flag[i][1],1);            }        }        pro.solve()?printf("YES\n"):printf("NO\n");    }    return 0;}



0 0
原创粉丝点击