GVKun编程网logo

Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems(矩阵快速幂)(矩阵快速幂是什么)

16

此处将为大家介绍关于EducationalCodeforcesRound60(RatedforDiv.2)D.MagicGems的详细内容,并且为您解答有关矩阵快速幂的相关问题,此外,我们还将为您介绍

此处将为大家介绍关于Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems的详细内容,并且为您解答有关矩阵快速幂的相关问题,此外,我们还将为您介绍关于Coloring Edges(有向图环染色)-- Educational Codeforces Round 72 (Rated for Div. 2)、Educational Codeforces Round 33 (Rated for Div. 2)、Educational Codeforces Round 35 (Rated for Div. 2)、Educational Codeforces Round 35 (Rated for Div. 2)A,B,C,D的有用信息。

本文目录一览:

Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems(矩阵快速幂)(矩阵快速幂是什么)

Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems(矩阵快速幂)(矩阵快速幂是什么)

题目传送门

题意:

一个魔法水晶可以分裂成m个水晶,求放满n个水晶的方案数(mol1e9+7)

 

思路:

线性dp,dp[i]=dp[i]+dp[i-m];

由于n到1e18,所以要用到矩阵快速幂优化

分享图片

注意初始化

 

代码:

 

分享图片

分享图片

#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
typedef long long ll;
#define MAX 105
const int N=105;//矩阵的大小
int T;
ll n,m;
ll add(ll a,ll b)
{
    a%=mod;
    b%=mod;
    return (a+b)%mod;
}
struct hh
{
    ll ma[N][N];
}a,res;
hh multi(hh a,hh b)
{
    hh tmp;
    memset(tmp.ma,0,sizeof(tmp.ma));
    for(int i=0;i<N;i++)
        for(int j=0;j<N;j++)
            for(int k=0;k<N;k++)
        {
            tmp.ma[i][j]=add(tmp.ma[i][j],a.ma[i][k]*b.ma[k][j]);
        }
   return tmp;
}
void fast_pow(hh a,long long k)
{
    memset(res.ma,sizeof(res.ma));
    for(int i=0;i<N;i++)res.ma[i][i]=1;
    while(k>0)
    {
        if(k&1) res=multi(res,a);
        a=multi(a,a);
        k>>=1;
    }
}
int main()
{
    while(~scanf("%lld%d",&n,&m))
    {
        for(int i=1;i<=m;i++) a.ma[i][i-1]=1;
        a.ma[1][1]=a.ma[1][m]=1;
        fast_pow(a,n);
        printf("%lld\n",res.ma[1][1]);
    }
    return 0;
}
View Code

Coloring Edges(有向图环染色)-- Educational Codeforces Round 72 (Rated for Div. 2)

Coloring Edges(有向图环染色)-- Educational Codeforces Round 72 (Rated for Div. 2)

题意:https://codeforc.es/contest/1217/problem/D

给你一个有向图,要求一个循环里不能有相同颜色的边,问你最小要几种颜色染色,怎么染色?

思路:

如果没有环,那全是1;如果有环,那小到大的边为1,大到小的边为2。

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
  6 #include <bitset>
  7 //#include <G>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int,vector<int>,greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first,nth,last,compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 #define fo(a,b,c) for(register int a=b;a<=c;++a)
 24 #define fr(a,c) for(register int a=b;a>=c;--a)
 25 #define mem(a,b) memset(a,sizeof(a))
 26 #define pr printf
 27 #define sc scanf
 28 #define ls rt<<1
 29 #define rs rt<<1|1
 30 typedef long long ll;
 31 void swapp(int &a,int &b);
 32 double fabss(double a);
 33 int maxx(int a,int b);
 34 int minn(int a,int b);
 35 int Del_bit_1(int n);
 36 int lowbit(int n);
 37 int abss(int a);
 38 //const long long INF=(1LL<<60);
 39 const double E=2.718281828;
 40 const double PI=acos(-1.0);
 41 const int inf=(1<<30);
 42 const double ESP=1e-9;
 43 const int mod=(int)1e9+7;
 44 const int N=(int)1e6+10;
 45 
 46 int in[N];
 47 vector<vector<int> > G(N);
 48 
 49 bool top_sort(int n)
 50 {
 51     int cont=0;
 52     queue<int> q;
 53     for(int i=1;i<=n;i++)
 54         if(in[i]==0)
 55             q.push(i);
 56     while(!q.empty())
 57     {
 58         int x=q.front();
 59         q.pop();
 60         cont++;
 61         for(int i=0;i<G[x].size();i++)
 62         {
 63             in[G[x][i]]--;
 64             if(in[G[x][i]]==0)
 65                 q.push(G[x][i]);
 66         }
 67     }
 68     return (cont==n);
 69 }
 70 struct node
 71 {
 72     int u,v;
 73 }edge[N];
 74 
 75 int main()
 76 {
 77     int n,m;
 78     sc("%d%d",&n,&m);
 79     for(int i=1;i<=m;++i)
 80     {
 81         int u,v;
 82         sc("%d%d",&u,&v);
 83         edge[i]={u,v};
 84         in[v]++;
 85         G[u].push_back(v);
 86     }
 87     if(top_sort(n))
 88     {
 89         pr("1\n");
 90         for(int i=1;i<=m;++i)
 91             pr("1 ");
 92     }
 93     else
 94     {
 95         pr("2\n");
 96         for(int i=1;i<=m;++i)
 97             pr("%d ",edge[i].u>edge[i].v?1:2);
 98     }
 99     return 0;
100 }
101 
102 /**************************************************************************************/
103 
104 int maxx(int a,int b)
105 {
106     return a>b?a:b;
107 }
108 
109 void swapp(int &a,int &b)
110 {
111     a^=b^=a^=b;
112 }
113 
114 int lowbit(int n)
115 {
116     return n&(-n);
117 }
118 
119 int Del_bit_1(int n)
120 {
121     return n&(n-1);
122 }
123 
124 int abss(int a)
125 {
126     return a>0?a:-a;
127 }
128 
129 double fabss(double a)
130 {
131     return a>0?a:-a;
132 }
133 
134 int minn(int a,int b)
135 {
136     return a<b?a:b;
137 }

Educational Codeforces Round 33 (Rated for Div. 2)

Educational Codeforces Round 33 (Rated for Div. 2)

总结

以上是小编为你收集整理的Educational Codeforces Round 33 (Rated for Div. 2)全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

Educational Codeforces Round 35 (Rated for Div. 2)

Educational Codeforces Round 35 (Rated for Div. 2)

总结

以上是小编为你收集整理的Educational Codeforces Round 35 (Rated for Div. 2)全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

Educational Codeforces Round 35 (Rated for Div. 2)A,B,C,D

Educational Codeforces Round 35 (Rated for Div. 2)A,B,C,D

A. Nearest Minimums
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array of n integer numbers a0, a1, ..., an - 1. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occurs at least two times.

Input

The first line contains positive integer n (2 ≤ n ≤ 105) — size of the given array. The second line contains n integers a0, a1, ..., an - 1(1 ≤ ai ≤ 109) — elements of the array. It is guaranteed that in the array a minimum occurs at least two times.

Output

Print the only number — distance between two nearest minimums in the array.

Examples
input
2
3 3
output
1
input
3
5 6 5
output
2
input
9
2 1 3 5 4 1 2 3 1
output
3

 思路:水题

实现代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,minn = 1100000002,a[200000];
    cin>>n;
    for(int i = 0;i < n;i ++){
        cin>>a[i];
        minn = min(minn , a[i]);
    }
    int flag = 0;
    int maxx = 1100000002,ans=1;
    for(int i = 0;i < n;i ++){
        if(a[i] == minn){
            if(flag)
            maxx = min(ans,maxx);
            flag = 1;
            ans = 1;
        }
        else ans++;
    }
    cout<<maxx<<endl;
}
B. Two Cakes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

It''s New Year''s Eve soon, so Ivan decided it''s high time he started setting the table. Ivan has bought two cakes and cut them into pieces: the first cake has been cut into a pieces, and the second one — into b pieces.

Ivan knows that there will be n people at the celebration (including himself), so Ivan has set n plates for the cakes. Now he is thinking about how to distribute the cakes between the plates. Ivan wants to do it in such a way that all following conditions are met:

  1. Each piece of each cake is put on some plate;
  2. Each plate contains at least one piece of cake;
  3. No plate contains pieces of both cakes.

To make his guests happy, Ivan wants to distribute the cakes in such a way that the minimum number of pieces on the plate is maximized. Formally, Ivan wants to know the maximum possible number x such that he can distribute the cakes according to the aforementioned conditions, and each plate will contain at least x pieces of cake.

Help Ivan to calculate this number x!

Input

The first line contains three integers na and b (1 ≤ a, b ≤ 100, 2 ≤ n ≤ a + b) — the number of plates, the number of pieces of the first cake, and the number of pieces of the second cake, respectively.

Output

Print the maximum possible number x such that Ivan can distribute the cake in such a way that each plate will contain at least x pieces of cake.

Examples
input
5 2 3
output
1
input
4 7 10
output
3
Note

In the first example there is only one way to distribute cakes to plates, all of them will have 1 cake on it.

In the second example you can have two plates with 3 and 4 pieces of the first cake and two plates both with 5 pieces of the second cake. Minimal number of pieces is 3.

 

 思路:

水题

实现代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,b;
    cin>>n>>a>>b;
    int ans = 0;
    for(int i = 1;i <= n-1;i++){
        ans = max(ans,min(a/i,b/(n-i)));
    }
    cout<<ans<<endl;
}
C. Three Garlands
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mishka is decorating the Christmas tree. He has got three garlands, and all of them will be put on the tree. After that Mishka will switch these garlands on.

When a garland is switched on, it periodically changes its state — sometimes it is lit, sometimes not. Formally, if i-th garland is switched on during x-th second, then it is lit only during seconds xx + kix + 2kix + 3ki and so on.

Mishka wants to switch on the garlands in such a way that during each second after switching the garlands on there would be at least one lit garland. Formally, Mishka wants to choose three integers x1, x2 and x3 (not necessarily distinct) so that he will switch on the first garland during x1-th second, the second one — during x2-th second, and the third one — during x3-th second, respectively, and during each second starting from max(x1, x2, x3) at least one garland will be lit.

Help Mishka by telling him if it is possible to do this!

Input

The first line contains three integers k1, k2 and k3 (1 ≤ ki ≤ 1500) — time intervals of the garlands.

Output

If Mishka can choose moments of time to switch on the garlands in such a way that each second after switching the garlands on at least one garland will be lit, print YES.

Otherwise, print NO.

Examples
input
2 2 3
output
YES
input
4 2 3
output
NO
Note

In the first example Mishka can choose x1 = 1, x2 = 2, x3 = 1. The first garland will be lit during seconds 1, 3, 5, 7, ..., the second — 2, 4, 6, 8, ..., which already cover all the seconds after the 2-nd one. It doesn''t even matter what x3 is chosen. Our choice will lead third to be lit during seconds 1, 4, 7, 10, ..., though.

In the second example there is no way to choose such moments of time, there always be some seconds when no garland is lit.

 

 思路:

1 = 1/2 + 1/2 ,1 = 1/2+1/4+1/4, 差不多就这意思

实现代码:

#include<bits/stdc++.h>
using namespace std;
int isPrime(int n) {
    int i;
    for (i = 2; i * i <= n; ++i) {
        if (n % i == 0) return 0;
    }
    return 1;
}
int main()
{
    int   a[5];
    double ans = 0;
   for(int i = 0;i < 3;i ++)
    cin>>a[i];
    sort(a,a+3);
    for(int i = 2;i <= 1500;i++){
        if(isPrime(i)){
            ans = 0;
           for(int j = 0;j < 3;j ++){
            if(a[j]==1){
                cout<<"YES"<<endl; return 0;
            }
            if(a[j]%i==0){
                ans += 1.0/a[j];
            }
            if(ans == 1){
              cout<<"YES"<<endl;return 0;}
           }
        }
    }
        cout<<"NO"<<endl;
}
D. Inversion Counting
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array. An inversion in a permutation p is a pair of indices (i, j) such that i > j and ai < aj. For example, a permutation [4, 1, 3, 2] contains 4 inversions: (2, 1), (3, 1), (4, 1), (4, 3).

You are given a permutation a of size n and m queries to it. Each query is represented by two indices l and r denoting that you have to reverse the segment [l, r] of the permutation. For example, if a = [1, 2, 3, 4] and a query l = 2, r = 4 is applied, then the resulting permutation is [1, 4, 3, 2].

After each query you have to determine whether the number of inversions is odd or even.

Input

The first line contains one integer n (1 ≤ n ≤ 1500) — the size of the permutation.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of the permutation. These integers are pairwise distinct.

The third line contains one integer m (1 ≤ m ≤ 2·105) — the number of queries to process.

Then m lines follow, i-th line containing two integers liri (1 ≤ li ≤ ri ≤ n) denoting that i-th query is to reverse a segment [li, ri] of the permutation. All queries are performed one after another.

Output

Print m lines. i-th of them must be equal to odd if the number of inversions in the permutation after i-th query is odd, and even otherwise.

Examples
input
3
1 2 3
2
1 2
2 3
output
odd
even
input
4
1 2 4 3
4
1 1
1 4
1 4
2 3
output
odd
odd
odd
even
Note

The first example:

  1. after the first query a = [2, 1, 3], inversion: (2, 1);
  2. after the second query a = [2, 3, 1], inversions: (3, 1), (3, 2).

The second example:

  1. a = [1, 2, 4, 3], inversion: (4, 3);
  2. a = [3, 4, 2, 1], inversions: (3, 1), (4, 1), (3, 2), (4, 2), (4, 3);
  3. a = [1, 2, 4, 3], inversion: (4, 3);
  4. a = [1, 4, 2, 3], inversions: (3, 2), (4, 2).

思路:和 l,r 的值没关系,只和他们之间的距离有关系

实现代码:

#include<bits/stdc++.h>
using namespace std;
const int M = 2e5+100;
int a[M],t[M];
void merge_sort(int x, int y, int & ans){
    if(y-x>1){
        int m = x+(y-x)/2;
        int p=x, q=m, i=x;
        merge_sort(x, m, ans);
        merge_sort(m ,y, ans);
        while(p<m || q<y){
            if(q>=y || p<m && a[p]<=a[q]){
                t[i++] = a[p++];
            }
            else{
                t[i++] = a[q++];
                ans += m-p;
            }
        }
        for(int j=x; j<y; j++){
            a[j] = t[j];
        }
    }
}

int main(){
    int n,q,l,r;
    cin>>n;
    for(int i = 0;i < n; i++){
        cin>>a[i];
    }
    int ans = 0;
    merge_sort(0,n,ans);
    if(ans % 2 == 0)
        ans = 0;
    else
        ans = 1;
    cin>>q;
    while(q--){
        cin>>l>>r;
        int len = r - l + 1;
        len/=2;
        if(len % 2 == 0){
            if(ans == 0) cout<<"even"<<endl;
            else cout<<"odd"<<endl;
        }
        else{
            if(ans == 0){ ans = 1; cout<<"odd"<<endl;}
            else{ ans = 0; cout<<"even"<<endl;}
        }
    }
    return 0;
}

 

我们今天的关于Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems矩阵快速幂的分享已经告一段落,感谢您的关注,如果您想了解更多关于Coloring Edges(有向图环染色)-- Educational Codeforces Round 72 (Rated for Div. 2)、Educational Codeforces Round 33 (Rated for Div. 2)、Educational Codeforces Round 35 (Rated for Div. 2)、Educational Codeforces Round 35 (Rated for Div. 2)A,B,C,D的相关信息,请在本站查询。

本文标签:

上一篇【Codeforces Round 1129】[Alex Lopashev Thanks-Round] (Div. 1)(code geass:z of the recapture)

下一篇盒子布局、标签特性display、浮动、定位position(盒子的浮动有哪些设置)