# TopCoder SRM691 Div2

## 题目及代码

### 300

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
typedef long long LL;
typedef vector <int>    VI;
typedef pair <int,int>  PII;
#define FOR(i,x,y)  for(int i = x;i < y;++ i)
#define IFOR(i,x,y) for(int i = x;i > y;-- i)
#define pb  push_back
#define mp  make_pair
#define fi  first
#define se  second
#define lrt rt<<1
#define rrt rt<<1|1
#define lson    rt<<1,l,mid
#define rson    rt<<1|1,mid+1,r

int in(){
int ans=0;
char last=' ',ch=getchar();
while(ch<'0' || ch>'9')last=ch,ch=getchar();
while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
if(last=='-')ans=-ans;
return ans;
}

void out(int a) {
if(a < 0) { putchar('-'); a = -a; }
if(a >= 10) out(a / 10);
putchar(a % 10 + '0');
}

const int maxn = 100;

class Plusonegame
{
public:
string getorder(string s)
{
int cnt = 0;
string ans = string("");
char p = '+';
char str[maxn];
int len = 0;
int slen = s.length();
FOR(i,0,slen){
if(s[i] == '+') {cnt ++;continue;}
str[len++] = s[i];
}
sort(str,str+len);
int cur = 0;
for(int i = 0;i < len;i++){
int t = str[i]-'0';
while(cnt && cur < t){
ans += p;
cur ++;
cnt --;
}
ans += str[i];
}
while(cnt){
ans += p;
cnt --;
}
return ans;
}

};  

### 500

ps:$ps:$由于本题可能有重边，所以需要特判一下。

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
typedef long long LL;
typedef vector <int>    VI;
typedef pair <int,int>  PII;
#define FOR(i,x,y)  for(int i = (int)x;i < (int)y;++ i)
#define IFOR(i,x,y) for(int i = (int)x;i > (int)y;-- i)
#define pb  push_back
#define mp  make_pair
#define fi  first
#define se  second
#define lrt rt<<1
#define rrt rt<<1|1
#define lson    rt<<1,l,mid
#define rson    rt<<1|1,mid+1,r

const int inf = 1e9+7;
const int N = 55;

int in(){
int ans=0;
char last=' ',ch=getchar();
while(ch<'0' || ch>'9')last=ch,ch=getchar();
while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
if(last=='-')ans=-ans;
return ans;
}

void out(int a) {
if(a < 0) { putchar('-'); a = -a; }
if(a >= 10) out(a / 10);
putchar(a % 10 + '0');
}

vector <int> G[N];
stack <int> s;
int pos[N],dfn[N],dfs_clock,low[N],n,m,ans,num[N];

void dfs(int u,int fa){
dfn[u] = low[u] = ++dfs_clock;
s.push(u);
FOR(i,0,G[u].size()){
int v = G[u][i];
if(!dfn[v]){
dfs(v,u);
low[u] = min(low[u],low[v]);
}
else if(v != fa){
low[u] = min(dfn[v],low[u]);
}
}
if(low[u] == dfn[u]){
ans++;
int cnt = 0;
while(!s.empty()){
int v = s.top();
cnt ++;
s.pop();
if(v == u) break;
}
num[ans-1] = cnt;
}
}

class Sunnygraphs2
{
public:
long long count(vector <int> a)
{
n = (int)a.size();
FOR(i,0,n)  G[i].clear();
memset(dfn,0,sizeof(dfn));
FOR(i,0,n)  low[i] = inf;
while(!s.empty())   s.pop();
dfs_clock = 0;  ans = 0;
memset(num,0,sizeof(num));
FOR(i,0,n){
int u = a[i];
if(a[u] == i && u < i){
num[ans++] = 2;
continue;
}
G[u].pb(i); G[i].pb(u);
}
FOR(i,0,n){
if(!dfn[i]) dfs(i,-1);
}
int tot = 0,tans = 0;
FOR(i,0,ans) if(num[i] >= 2)
tot += num[i],tans ++;
LL res = 1;
int ress = n-tot;
FOR(i,0,ans){
res *= ((1LL << num[i])-1);
}
res *= (1LL << ress);
if(tans == 1)    res ++;
return res;
}
};

### 900

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
typedef long long LL;
typedef vector <int>    VI;
typedef pair <int,int>  PII;
#define FOR(i,x,y)  for(int i = x;i < y;++ i)
#define IFOR(i,x,y) for(int i = x;i > y;-- i)
#define pb  push_back
#define mp  make_pair
#define fi  first
#define se  second
#define lrt rt<<1
#define rrt rt<<1|1
#define lson    rt<<1,l,mid
#define rson    rt<<1|1,mid+1,r

int in(){
int ans=0;
char last=' ',ch=getchar();
while(ch<'0' || ch>'9')last=ch,ch=getchar();
while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
if(last=='-')ans=-ans;
return ans;
}

void out(int a) {
if(a < 0) { putchar('-'); a = -a; }
if(a >= 10) out(a / 10);
putchar(a % 10 + '0');
}

const int maxn = 30;

LL lcm[maxn][maxn],nd[maxn][maxn][3];

LL Lcm(LL x,LL y)   {return x/__gcd(x,y)*y;}

void init(){
lcm[1][1] = 1;
FOR(i,1,maxn)   FOR(j,1,maxn)   lcm[i][j] = 1;
FOR(i,1,maxn){
lcm[i][i] = i;
FOR(j,i+1,maxn){
LL tem = __gcd(lcm[i][j-1],(LL)j);
lcm[i][j] = lcm[i][j-1]/tem*j;
}
}
FOR(i,2,maxn){
FOR(j,i+1,maxn){
nd[i][j][0] = Lcm(lcm[1][i-1],lcm[i+1][j-1]);
nd[i][j][1] = Lcm(lcm[1][i-1],lcm[i+1][j]);
nd[i][j][2] = lcm[1][j-1];
}
}
}

class Undiv2
{
public:

long long calc(int n){
LL ans = 0;
FOR(i,2,maxn){
FOR(j,i+1,maxn){
LL tem = 0;
tem = j*(n/nd[i][j][0]-n/nd[i][j][1]-n/nd[i][j][2]+n/lcm[1][j]);
ans += tem;
}
}
return ans;
}
long long getsum(int n)
{
init();
return calc(n);
}
};

• 本文已收录于以下专栏：

举报原因： 您举报文章：TopCoder SRM691 Div2 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)