Codeforces Round #338 (Div.2)

                                A. Bulbs
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya wants to turn on Christmas lights consisting of m bulbs. Initially, all bulbs are turned off. There are n buttons, each of them is connected to some set of bulbs. Vasya can press any of these buttons. When the button is pressed, it turns on all the bulbs it's connected to. Can Vasya light up all the bulbs?

If Vasya presses the button such that some bulbs connected to it are already turned on, they do not change their state, i.e. remain turned on.

Input

The first line of the input contains integers n and m (1 ≤ n, m ≤ 100) — the number of buttons and the number of bulbs respectively.

Each of the next n lines contains xi (0 ≤ xi ≤ m) — the number of bulbs that are turned on by the i-th button, and then xi numbers yij(1 ≤ yij ≤ m) — the numbers of these bulbs.

Output

If it's possible to turn on all m bulbs print "YES", otherwise print "NO".

Sample test(s)
input
3 4
2 1 4
3 1 3 1
1 2
output
YES
input
3 3
1 1
1 2
1 1
output
NO
 1 #include <bits/stdc++.h>
 2 
 3 int main()
 4 {
 5     int i,j,k;
 6     int n,m,cnt=0;
 7     int vis[105]={0};
 8     scanf("%d %d",&n,&m);
 9     for(i=1;i<=n;i++)
10     {
11         int x,y;
12         scanf("%d",&x);
13         for(j=1;j<=x;j++)
14         {
15             scanf("%d",&y);
16             if(vis[y]==0)
17             {
18                 vis[y]=1;
19                 cnt++;
20             }
21         }
22     }
23     if(cnt==m)
24         printf("YES\n");
25     else
26         printf("NO\n");
27 }
View Code
                              B. Longtail Hedgehog
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n points connected by m segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions:

  1. Only segments already presented on the picture can be painted;
  2. The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment;
  3. The numbers of points from the beginning of the tail to the end should strictly increase.

Masha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is the endpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get.

Note that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications.

Input

First line of the input contains two integers n and m(2 ≤ n ≤ 100 000, 1 ≤ m ≤ 200 000) — the number of points and the number segments on the picture respectively.

Then follow m lines, each containing two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi) — the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points.

Output

Print the maximum possible value of the hedgehog's beauty.

Sample test(s)
input
8 6
4 5
3 5
2 5
1 2
2 8
6 7
output
9
input
4 6
1 2
1 3
1 4
2 3
2 4
3 4
output
12
Note

The picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3·3 = 9.

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int vis[100005],num[100005];
 5 vector <int> edg[100005];
 6 int n,m;
 7 
 8 int main()
 9 {
10     int i,j;
11     int x,y;
12     int u,v;
13     scanf("%d %d",&n,&m);
14     fill(vis,vis+(n+2),1);
15     fill(num,num+(n+2),0);
16     for(i=1;i<=m;i++)
17     {
18         scanf("%d %d",&x,&y);
19         if(y<x)
20             edg[x].push_back(y);
21         else
22             edg[y].push_back(x);
23         num[x]++,num[y]++;
24     }
25     for(i=1;i<=n;i++)
26     {
27         int u=i;
28         for(j=0;j<edg[u].size();j++)
29         {
30             v=edg[u][j];
31             if(vis[v]+1>vis[u])
32             {
33                 vis[u]=vis[v]+1;
34             }
35         }
36     }
37     long long ans=0;
38     for(i=1;i<=n;i++)
39     {
40         if(1ll*vis[i]*num[i]>ans)
41             ans=1ll*vis[i]*num[i];
42     }
43     printf("%I64d\n",ans);
44 }
View Code
                             C. Running Track
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.

First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.

Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.

Input

First line of the input contains the string s — the coating that is present in the shop. Second line contains the string t — the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.

Output

The first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.

If the answer is not -1, then the following n lines should contain two integers xi and yi — numbers of ending blocks in the corresponding piece. If xi ≤ yi then this piece is used in the regular order, and if xi > yi piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.

Sample test(s)
input
abc
cbaabc
output
2
3 1
1 3
input
aaabrytaaa
ayrat
output
3
1 1
6 5
8 7
input
ami
no
output
-1
Note

In the first sample string "cbaabc" = "cba" + "abc".

In the second sample: "ayrat" = "a" + "yr" + "at".

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     char a[2108],b[2108];
 7     int i,j;
 8     int ans=0,ansx[2105],ansy[2105];
 9     scanf("%s",b);
10     scanf("%s",a);
11     int la=strlen(a),lb=strlen(b);
12     bool flag=true;
13     for(i=0;i<la;i++)
14     {
15         int mxnum=0,mxj,flg;
16         for(j=0;j<lb;j++)
17         {
18             if(a[i]==b[j])
19             {
20                 int num=1;
21                 while(a[i+num]==b[j+num] && i+num<la && j+num<lb)
22                 {
23                     num++;
24                 }
25                 if(num>mxnum)
26                     mxnum=num,mxj=j,flg=1;
27                 num=1;
28                 while(a[i+num]==b[j-num] && i+num>=0 && j-num>=0)
29                 {
30                     num++;
31                 }
32                 if(num>mxnum)
33                     mxnum=num,mxj=j,flg=2;
34             }
35         }
36         if(mxnum==0)
37         {
38             flag=false;
39             break;
40         }
41         else
42         {
43             ans++;
44             if(flg==1)
45             {
46                 ansx[ans]=mxj,ansy[ans]=mxj+mxnum-1;
47             }
48             else
49             {
50                 ansx[ans]=mxj,ansy[ans]=mxj-mxnum+1;
51             }
52             i=i+mxnum-1;
53         }
54 
55     }
56     if(flag)
57     {
58         printf("%d\n",ans);
59         for(i=1;i<=ans;i++)
60         {
61             printf("%d %d\n",ansx[i]+1,ansy[i]+1);
62         }
63     }
64     else
65     {
66         printf("-1\n");
67     }
68 }
View Code
                                D. Multipliers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ayrat has number n, represented as it's prime factorization pi of size m, i.e. n = pp2·...·pm. Ayrat got secret information that that the product of all divisors of n taken modulo 109 + 7 is the password to the secret data base. Now he wants to calculate this value.

Input

The first line of the input contains a single integer m (1 ≤ m ≤ 200 000) — the number of primes in factorization of n.

The second line contains m primes numbers pi (2 ≤ pi ≤ 200 000).

Output

Print one integer — the product of all divisors of n modulo 109 + 7.

Sample test(s)
input
2
2 3
output
36
input
3
2 3 2
output
1728
Note

In the first sample n = 2·3 = 6. The divisors of 6 are 1, 2, 3 and 6, their product is equal to 1·2·3·6 = 36.

In the second sample 2·3·2 = 12. The divisors of 12 are 1, 2, 3, 4, 6 and 12. 1·2·3·4·6·12 = 1728.

 

ans: Let d(x) be a number of divisors of x, and f(x) be the product of divisors. Let x = p1α1p2α2... pnαn, thend(x) = (α1 + 1)·(α2 + 1)... (αn + 1)

. There is  pairs of divisors of type , and if x is a perfect square we have one more divisor : .

for a prime m and a ≠ 0 the statement  (little Fermat theorem)

We can see that , if a and b are co prime.

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const long long mod1=1e9+7;
 4 const long long mod2=1e9+6;
 5 
 6 int m,y;
 7 int cnt[200005];
 8 
 9 long long pow_mod(long long x,long long n,long long p)    {
10     long long ret = 1;
11     while (n)   {
12         if (n & 1)  {
13             ret =ret * x % p;
14         }
15         x =x * x % p;
16         n >>= 1;
17     }
18     return ret;
19 }
20 
21 int main()
22 {
23     int m;
24     int i,j,k;
25     scanf("%d",&m);
26     memset(cnt,0,sizeof(cnt));
27     while(m--)
28     {
29         scanf("%d",&y);
30         cnt[y]++;
31     }
32 
33     long long d=1,ans=1;
34     for(i=1;i<=200000;i++)
35     {
36         if(cnt[i]>0)
37         {
38             long long fp=pow_mod(1ll*i,1ll*(cnt[i]+1)*cnt[i]/2,mod1);
39             ans=pow_mod(1ll*ans,1ll*(cnt[i]+1),mod1)*pow_mod(fp,d,mod1)%mod1;
40             d=d*(1ll*(cnt[i]+1))%mod2;
41         }
42     }
43 
44     printf("%I64d\n",ans);
45 }
View Code
                                E. Hexagons
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Ayrat is looking for the perfect code. He decided to start his search from an infinite field tiled by hexagons. For convenience the coordinate system is introduced, take a look at the picture to see how the coordinates of hexagon are defined:

Ayrat is searching through the field. He started at point (0, 0) and is moving along the spiral (see second picture). Sometimes he forgets where he is now. Help Ayrat determine his location after n moves.

Input

The only line of the input contains integer n (0 ≤ n ≤ 1018) — the number of Ayrat's moves.

Output

Print two integers x and y — current coordinates of Ayrat coordinates.

Sample test(s)
input
3
output
-2 0
input
7
output
3 2

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6         long long n,i;
 7         long long x0[10]={2,1,-1,-2,-1,1,2},y0[10]={0,2,2,0,-2,-2,0};
 8         long long dx[10]={-1,-2,-1,1,2,1}  ,dy[10]={2,0,-2,-2,0,2}  ;
 9         //for(i=0;i<=6;i++)
10             //printf("%I64d %I64d\n",x0[i],y0[i]);
11         scanf("%I64d",&n);
12         if(n==0)
13         {
14             printf("0 0\n");
15             return 0;
16         }
17         long long l=0,r=577350270;
18         while(l<=r)
19         {
20             long long mid=(l+r)/2;
21             if(mid*(mid+1)*3>=n)
22             {
23                 r=mid-1;
24             }
25             else
26             {
27                 l=mid+1;
28             }
29         }
30 
31         n=n-(l-1)*l*3;
32         long long a=n/l,b=n%l;
33         long long ansx,ansy;
34         ansx=x0[a]*l,ansy=y0[a]*l;
35 
36         ansx=ansx+b*dx[a],ansy=ansy+b*dy[a];
37 
38         printf("%I64d %I64d\n",ansx,ansy);
39 }
View Code

 

posted @ 2016-01-15 16:08  cyd2014  阅读(180)  评论(0编辑  收藏  举报