codeforces round 319 div2 題解

A:水題。

#include <bits/stdc++.h> 
using namespace std;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
#define Fast_IO ios_base::sync_with_stdio(0);cin.tie(0)
int n,m;
int k;
int main()
{
	cin>>n>>k;
	int res = 0;
	rep(i, 1, n)
	{
		if(k%i == 0)
		{
			int t = k/i;
			if(t <= n)
				res++;
		}
	}
	cout<<res<<endl;
	return 0;
}

B:01揹包即可? 貌似必須剪下枝

#include <bits/stdc++.h> 
using namespace std;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
#define Fast_IO ios_base::sync_with_stdio(0);cin.tie(0)
int n,m;
bool a[1010];
bool b[1010];
int main()
{
	cin>>n>>m;
	memset(a, false, sizeof(a));
	bool res = false;
	rep(i, 1, n)
	{
		int t;
		scanf("%d", &t);
		if(res) continue;
		for(int j=1; j<m; j++){
				if(a[j]){
				int tmp = (j + t%m) %m;
				b[tmp] = true;
			} 
		}
		b[t%m] = true;
		for(int j=0; j<m; j++) a[j] = b[j]?true:a[j];
		if(a[0] == true)
			res = true;
		memset(b, false, sizeof(b));
	}
	if(res) puts("YES");
	else puts("NO");
	return 0;
}

C:也是漲知識, 只要搞出所有質數的指數項就可以了。

#include <bits/stdc++.h> 
using namespace std;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
#define Fast_IO ios_base::sync_with_stdio(0);cin.tie(0)
int n;
int cnt;
int prime[10010];
const int MAXN = 1010;
int vis[MAXN];
void init()
{
	for(int i = 2; i<=1000; i++)
		if(!vis[i])
		{
			prime[cnt++] = i;
			for(int k= i*i; k<MAXN; k+=i) 
				vis[k] = 1;
		}
} 
vector<int> ans;
int main()
{
	cin>>n;
	init();
	for(int i=0; i<cnt&&prime[i] <= n; i++)
	{
		int m = prime[i];
		while(m<=n)
		{
			ans.push_back(m);
			m*=prime[i];
		}
	}
	int size = ans.size();
	cout<<size<<endl;
	for(int i=0; i<size; i++)
		printf("%d ", ans[i]);
	return 0;
}

D:構造題。三種情況:

1. 當存在長度爲1的置換的時候。那麼直接把這個點向其它點連一條邊就可以了。

2. 當存在長度爲2的置換的時候。那麼必須把其他環拆開連到這兩個點上, 而且,其他環長度必須是偶數。

3. 其他情況爲NO

#include <bits/stdc++.h>
using namespace std;
int n;
int a[100100];
bool hs[100100];
int c[2];
int main(){
    cin>>n;
    int flag = -1;
    for(int i=1; i<=n; i++){
        scanf("%d", &a[i]);
        if(a[i] == i)
            flag = i;
    }
    if(flag!=-1) {
        cout<<"YES"<<endl;
        for(int i=1; i<=n; i++){
            if(i != flag)
                printf("%d %d\n", flag, i);
        }
        return 0;
    }
    bool tt = false;
    for(int i=1; i<=n; i++){
        if(hs[i]) continue;
        int t = i;
        int len = 1;
        hs[t] = true;
        while(a[t] != i){
            len++;
            t = a[t];
            hs[t] = true;
        }
        if(len&1){
            cout<<"NO"<<endl;
            return 0;
        }
        if(len == 2)
        {
            tt = true;
            c[0] = t;
            c[1] = i;
        }
    }
    if(tt == false){
        cout<<"NO"<<endl;
        return 0;
    }
    memset(hs, false, sizeof(hs));
    cout<<"YES"<<endl;
    cout<<c[0]<<" "<<c[1]<<endl;
    for(int i=1; i<=n; i++){
        if(hs[i]||i==c[0]||i==c[1]) continue;
        int t = i;
        hs[t] = true;
        int tmp = 0;
        printf("%d %d\n", c[tmp], t);
        while(a[t] != i){
            t = a[t];
            hs[t] = true;
            tmp ^= 1;
            printf("%d %d\n", c[tmp], t);
        }
    }
    return 0;
}
E: 分塊。。 類似莫隊。 好巧妙。。

#include <bits/stdc++.h>
using namespace std;
struct node{
    int x,y,id;
}t[1000100];
const int B = 1000;
bool cmp(node a, node b){
    if(a.x/1000 == b.x/1000) return ((a.x/1000)&1)?a.y < b.y:a.y>b.y;
    return a.x/1000 < b.x/1000;
}
int main(){
    int n;
    cin>>n;
    for(int i=1; i<=n; i++)
        scanf("%d %d", &t[i].x, &t[i].y), t[i].id = i;
    sort(t+1, t+n+1, cmp);
    for(int i=1; i<=n; i++){
        printf("%d ", t[i].id);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章