Codeforces Round #589 (Div. 2) (e、f没写)

https://codeforces.com/contest/1228/problem/A

A. Distinct Digits

超级简单嘻嘻,给你一个l和r然后寻找一个数,这个数要满足的条件是它的每一位的数字不相同,找出满足要求的最小的那个数输出,没有找到就输出-1;

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 bool check(int n){
 4     int vis[15]={0};
 5     while(n){
 6         if(!vis[n%10])  vis[n%10]=1;
 7         else return false;
 8         n/=10;
 9     }
10     return true;
11 }
12 int main()
13 {
14     int l,r;
15     cin>>l>>r;
16     for(int i = l;i <=r;++i){
17         if(check(i)){
18             cout<<i<<endl;return 0;
19         }
20     }
21     cout<<"-1"<<endl;
22     return 0;
23 }
AC代码

 https://codeforces.com/contest/1228/problem/B

B. Filling the Grid

也很简单,,自己读题没读清导致前面白wa了好几发。给一个n*m的grid,然后告诉你每一行每一列从边界处开始连续的黑块个数,然后求满足这个条件的有多少种情况,输出结果mod1e9+7。

然后要注意,如果没有方案数是输出0的。

因为h,w<=1e3,所以构建个二维数组就好了,在连续黑色块的位置标记1,后一位还有空位的话标-1,-1表示这个地方不能变黑,如果标1和-1重复了,那么方案数就是0,如果可以把这个基础的图构造出来的话,统计一下0的个数ans,输出2的ans次方就好了(因为0可填黑可填白)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const ll mod = 1e9+7;
 5 int mp[1005][1005];
 6 long long power(long long a,long long b)///a是底数,b是次幂
 7 {
 8     long long ans=1;
 9     for(;b!=0;b>>=1)
10     {
11         if(b&1) ans=(long long)ans*a%mod;
12         a=(long long)a*a%mod;
13     }
14     return ans;
15 }
16  
17 int main()
18 {
19    int n,m;
20    cin>>n>>m;
21    for(int i=1;i<=n;i++)
22    {
23        int a;cin>>a;
24        for(int j=1;j<=a;j++)
25        {
26            mp[i][j]=1;
27        }
28        mp[i][a+1]=-1;
29    }
30    int f=0;
31    for(int i=1;i<=m;i++)
32    {
33        int a;cin>>a;
34        for(int j=1;j<=a;j++)
35        {
36            if(mp[j][i]==-1) f=1;
37            mp[j][i]=1;
38        }
39        if(mp[a+1][i]==1) f=1;
40        mp[a+1][i]=-1;
41    }
42    if(f==1)
43     cout<<0<<endl;
44    else
45    {
46        int ans=0;
47        for(int i=1;i<=n;i++)
48         for(int j=1;j<=m;j++)
49         if(mp[i][j]==0)
50         ans++;
51        cout<<power(2,ans)<<endl;
52    }
53 }
AC代码

https://codeforces.com/contest/1230/problem/C

C. Primes and Multiplication

写这道题的时候疯狂挨骂emmm,lj好好反思。

根据题目定义把要求的东西展开,然后要用到的一个结论就是求1-n中质因子x的个数是求Σ(n/x^k)(k=1,2,3...)

还学到了个求质因子的方法? 害...不会的东西太多了

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const ll mod = 1e9+7;
 5 ll sq(ll a,ll b){
 6     ll cnt=1;
 7     while(b){
 8         if(b&1)cnt=cnt%mod*a%mod;
 9         a=a*a%mod;
10         b>>=1;
11     }
12     return cnt%mod;
13 }
14 vector<ll>s;
15 int main(){
16     ll ans= 1,x,n;
17         cin>>x>>n;
18     for(ll i = 2;i *i<= x;++i){
19         if(x%i==0)s.push_back(i);
20         while(x%i==0)x/=i;
21     }
22     s.push_back(x);
23     for(int i = 0;i< s.size();++i){
24         if(s[i]==1) continue;
25         ll sum=0,temp=s[i],a=s[i];
26         while(temp<=n){
27             sum+=n/temp;
28             if(temp>n/a)break;
29             temp*=a;    
30         }
31         
32         ans=ans*sq(a,sum)%mod;ans%=mod;
33     }
34     cout<<ans<<endl;
35     return 0;
36 }
AC代码

https://codeforces.com/contest/1228/problem/D

D. Complete Tripartite

给n个点m条边,然后构造三个顶点集,其中任一顶点集内的顶点两两之间没有边,然后该顶点集的任一顶点都和不属于这个顶点集的所有点有边。

如果可以就输出每个顶点所属的集合,不可以就输出-1;

emmmmmm,然后按照题意写就好了,先全部放在集合1,然后和集合1有边的就去集合2,然后集合2内部有边的就放到集合3去,再检查集合内部有没有连接的边。

如果都没问题了之后再检查每个点是否和其他不属于它集合的点有边,那么就想办法检查一遍和她连接的点在另外两个集合的个数是不是 等于另外两个集合的顶点数

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N = 1e5+3;
 4 vector<int>s[N];
 5 int pre[N],cnt1=0,cnt2=0,cnt3=0,temp1,temp2,temp3;
 6 struct ac{
 7     int x,y;
 8 }a[350000];
 9 int main()
10 {
11     int n,m;
12     cin>>n>>m;
13     for(int i = 1;i <=n;++i)pre[i]=1;cnt1=n;
14     for(int i = 0;i <m;++i){
15         cin>>a[i].x>>a[i].y;
16         s[a[i].x].push_back(a[i].y);s[a[i].y].push_back(a[i].x);
17     }
18 
19     for(int i = 1;i <= n; ++i){
20         if(s[i].size()<2){cout<<"-1"<<endl;return 0;}
21         for(int j = 0;j < s[i].size();++j){
22             if(pre[i]==1)
23                 if(pre[s[i][j]]==1)pre[s[i][j]]=2,cnt1--,cnt2++;
24         }
25     }
26     for(int i = 1;i <= n;++i){
27         for(int j = 0;j <s[i].size();++j){
28             if(pre[i]==2)
29                 if(pre[s[i][j]]==2)pre[s[i][j]]=3,cnt2--,cnt3++;
30         }
31     }
32     if(cnt1==0||cnt2==0||cnt3==0){
33         cout<<"-1"<<endl;return 0;
34     }
35     
36     for(int i = 1;i <=n;++i){
37         temp1=temp2=temp3=0;
38         for(int j = 0;j < s[i].size();++j){
39             if(pre[i]==pre[s[i][j]]){
40                 cout<<"-1"<<endl;return 0;
41             }
42             if(pre[i]==1){
43                 if(pre[s[i][j]]==2)temp2++;
44                 else if(pre[s[i][j]]==3)temp3++;
45             }
46             else if(pre[i]==2){
47                 if(pre[s[i][j]]==1)temp1++;
48                 else if(pre[s[i][j]]==3)temp3++;
49             }
50             else if(pre[i]==3){
51                 if(pre[s[i][j]]==2)temp2++;
52                 else if(pre[s[i][j]]==1)temp1++;
53             }    
54         }
55         if(pre[i]==1){
56                 if(temp2<cnt2||temp3<cnt3){
57                         cout<<"-1"<<endl;return 0;
58                 }
59             }
60         else if(pre[i]==2){
61                 if(temp1<cnt1||temp3<cnt3){
62                         cout<<"-1"<<endl;return 0;
63                 }
64             }
65         else if(pre[i]==3){
66                 if(temp2<cnt2||temp1<cnt1){
67                         cout<<"-1"<<endl;return 0;
68                 }
69             }    
70     }
71     for(int i = 1;i <=n;++i)cout<<pre[i]<<" ";cout<<endl;
72 }
AC代码

emmmm我也不清楚为啥这个题这么暴力的写都能过。。可能这就是div3吧(真实(然而我写了很久

猜你喜欢

转载自www.cnblogs.com/h404nofound/p/11617631.html
今日推荐