您的位置:首页 > 其它

codeforces round 319 div2 题解

2015-09-15 17:34 459 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces