当前位置:Gxlcms > mysql > CodeforcesRound#232(Div.2)

CodeforcesRound#232(Div.2)

时间:2021-07-01 10:21:17 帮助过:58人阅读

Problems # Name A On Segment's Own Points standard input/output 1 s, 256 MB x1657 B On Corruption and Numbers standard input/output 1 s, 256 MB x925 C On Number of Decompositions into Multipliers standard input/output 1 s, 256 MB x181 D On

Problems

# Name
A

On Segment's Own Points

standard input/output

1 s, 256 MB

x1657
B

On Corruption and Numbers

standard input/output

1 s, 256 MB

x925
C

On Number of Decompositions into Multipliers

standard input/output

1 s, 256 MB

x181
D

On Sum of Fractions

standard input/output

2 s, 256 MB

x134
E

On Changing Tree

standard input/output

2 s, 256 MB

x34


A题:n个区间,你可以选择第一个区间上的位置,后面n-1行是被占掉的区间,求你最多能占多长的区间。

思路:n才100,直接暴力,把出现过的区间标记掉,最后去遍历一遍即可。

B题:你有l-r的硬币,要组合出x的钱,问能否组合。

思路:可以的区间为1*[l,r], 2 * [l,r], 3 * [l,r]....直到后面区间重合了之后都是一直可以的,所以用x / l求出i。然后乘上r判断n在不在区间内即可。

C题:m是a1*a2*a3..*an。问m有几种分解成n个数相乘的不同方法。

思路:先分解所有a的分解成质因子,然后等同于把质因子放入n个位置去,用隔板法,每个质因子的方法为C(n - 1 + k) (n - 1)种,k为该质因子个数。

D题:求出题目给定公式值。

思路:先推公式1/u(i) * 1/v(i) = 1/(v(i) - u(i)) * (1/v(i) - 1/u(i))。如此一来前面每一项等于(1/2 - 1/3) + (1/3 - 1/5) + (1/5 - 1/7).....(1/m - 1/n) = 1/2 - 1/n。然后关键就变成找出n的上下质数,这步用暴力枚举,直到是质数为止。然后求出总和即可。

E题:n个点的有根树,根为1,操作1在v结点添加,距离为i的子节点添加值为x - i * k。2为询问。

思路:树状数组,在添加的时候,先假设是从根添加,这样要多添加k * dep[v]。然后开2个树状数组一个记录sum和一个记录k。这样一来最后答案变为

sum - k * dep[v];

代码:

A:

#include 
#include 
#include 
using namespace std;
const int N = 105;
int n, i, vis[N], l, r, ll, rr;

int main() {
    scanf("%d", &n);
    scanf("%d%d", &ll, &rr);
    for (i = 2; i <= n; i++) {
        scanf("%d%d", &l, &r);
        for (int j = l; j < r; j++)
            vis[j] = 1;
    }
    int ans = 0;
    for (i = ll; i < rr; i++) {
        if (!vis[i])
            ans++;
    }
    printf("%d\n", ans);
    return 0;
}

B:

#include 
#include 

int t;
__int64 n, l, r, i;

bool solve() {
    if (n < l) return false;
    __int64 k = n / l;
    if (n <= r * k) return true;
    return false;
}

int main() {
    scanf("%d", &t);
    while (t--) {
        scanf("%I64d%I64d%I64d", &n, &l, &r);
        printf("%s\n", solve()?"Yes":"No");
    }
    return 0;
}

C:

#include 
#include 
#include 
#include 
using namespace std;

const int MOD = 1000000007;
const int N = 505;
const int MAXN = 20005;
int n, a, cnt = 0, num[MAXN], c[20005][1005];
map v;

void getnum(int x) {
	for (int i = 2; i * i <= x; i++) {
		while (x % i == 0) {
			if (v.count(i)) {
				num[v[i]]++;
				x /= i;
			}
			else {
				v[i] = ++cnt;
				num[v[i]]++;
				x /= i;
			}
		}
	}	
	if (x == 1) return;
	if (v.count(x)) {
		num[v[x]]++;
	}
	else {
		v[x] = ++cnt;
		num[v[x]]++;
	}
}

void init() {
	c[0][0] = 1;
	for (int i = 1; i <= 17000; i++)
		for (int j = 0; j <= i && j <= 1000; j++)
			if (j == 0 || j == i) c[i][j] = 1;
			else c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % MOD;
}

__int64 cal(int k) {
	return c[k + n - 1][n - 1];
}

__int64 solve() {
	__int64 ans = 1;
	for (int i = 1; i <= cnt; i++) 
		ans = ans * cal(num[i]) % MOD;
	return ans;
}

int main() {
	init();
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a);
		getnum(a);
	}
	printf("%I64d\n", solve());
	return 0;
}

D:

#include 
#include 

const int MAXN = 100005;
int t;
__int64 n, l, r, prime[MAXN], vis[MAXN], pn = 0;

void init() {
	for (int i = 2; i <= 100000; i++) {
		if (vis[i]) continue;
		prime[pn++] = i;
		for (int j = i; j <= 100000; j += i)
			vis[j] = 1;
	}
}

bool judge(int x) {
	for (int i = 0; i < pn; i++) {
		if (x % prime[i] == 0 && x != prime[i])
			return false;
	}
	return true;
}

__int64 find_l(__int64 x) {
	while (1) {
		if (judge(x))
			return x;
		x--;
	}
}

__int64 find_r(__int64 x) {
	x++;
	while (1) {
		if (judge(x))
			return x;
		x++;
	}
}

__int64 gcd(__int64 a, __int64 b) {
	if (b == 0) return a;
	return gcd(b, a%b);
}
void print(__int64 l, __int64 r) {
	__int64 zi = (l - 2) * r + (n - l + 1) * 2, mu = l * r * 2;
	printf("%I64d/%I64d\n", zi / gcd(zi, mu), mu / gcd(zi, mu));
}

int main() {
	init();
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		l = find_l(n); r = find_r(n);
		print(l, r);
	}
	return 0;
}

E:

#include 
#include 
#include 
using namespace std;
const int N = 300005;
const int MOD = 1000000007;
int n, Q, i, nod, vis[N];
__int64 kbit[N], sbit[N], cnt = 0, l[N], r[N], dep[N];
vector g[N];
void dfs(int u, __int64 d) {
    vis[u] = 1; dep[u] = d;
    cnt++; l[u] = cnt;
    for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if (vis[v]) continue;
        dfs(v, d + 1);
    }
    r[u] = cnt;
}

void Is(__int64 value, int x, __int64 *num) {
    while (x <= N) {
        num[x] = (num[x] + value) % MOD;
        x += (x&(-x));
    }
}

__int64 Sum(int x, __int64 *num) {
    __int64 ans = 0;
    while (x > 0) {
        ans = (ans + num[x]) % MOD;
        x -= (x&(-x));
    }
    return ans;
}

int main() {
    scanf("%d", &n);
    for (i = 2; i <= n; i++) {
        scanf("%d", &nod);
        g[nod].push_back(i);
    }
    dfs(1, 0);
    scanf("%d", &Q);
    while (Q--) {
        int type, v;
        scanf("%d%d", &type, &v);
        if (type == 1) {
            __int64 x, k;
            scanf("%I64d%I64d", &x, &k);
            __int64 val = (x + dep[v] * k) % MOD;
            Is(k, l[v], kbit);
            Is(-k, r[v] + 1, kbit);
            Is(val, l[v], sbit);
            Is(-val, r[v] + 1, sbit);
        }
        else printf("%I64d\n", ((Sum(l[v], sbit) - dep[v] * Sum(l[v], kbit)) % MOD + MOD) % MOD);
    }
    return 0;
}

人气教程排行