judge: 牛客

A-A Simple Math Problem

judge:牛客

题意

给你一个n,让你求出(sum_{i=1}^{n}sum_{j=1}^{i}[gcd(i,j)==1]f(j))
其中f(x)表示的是数位和,eg:f(122)=1+2+2=5。

题解

一眼可以看出是道反演题,但是仔细想想发现不是特别好维护,然后给的范围又有点误导,让人以为可以瞎搞过(实际上真的可以打表过或者容斥过),然后中间耽搁了很长时间,还写了个瞎搞的做法,不过没敢交,最后才发现转换一下就是一道经典的反演题。
首先题目让我们求的是(sum_{i=1}^{n}sum_{j=1}^{i}[gcd(i,j)==1]f(j)),我们需要转换成(sum_{i=1}^{n}f(i)sum_{j=i+1}^{n}[gcd(i,j)==1])
其实只是枚举策略的变换,求的答案还是一样,只不过第二个公式可以用反演求,而且还比较好求,第一个没有办法用反演做(其实是我不会)。
至于原因,我们需要对第一个公式有足够的了解,第一个公式让我们求的是所有比当前数小,且与当前数互质的数的数位和。
思维转换一下,我们把每个数单独进行考虑,每个数对答案产生的贡献就是比它大,且与它互质的数的个数乘以这个数的数位和(就是转换后的公式)。
至于第二个公式怎么求,可以参考我写的一篇题解,这个题让我们求的就是前半部分,只不过数位和变成了数位乘。

代码

#include <bits/stdc++.h>
#define PI atan(1.0)*4
#define rp(i,s,t) for (int i = (s); i <= (t); i++)
#define RP(i,t,s) for (int i = (t); i >= (s); i--)
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define ll long long
#define ull unsigned long long
#define mst(a,b) memset(a,b,sizeof(a))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define m_p make_pair
#define p_b push_back
#define ins insert
#define era erase
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define dg if(debug)
#define outval(a) cout << "Debuging...|" << #a << ": " << a << "n";
using namespace std;
int debug = 0;
ll gcd(ll a,ll b){
	return b?gcd(b,a%b):a;
}
ll lcm(ll a,ll b){
	return a/gcd(a,b)*b;
}
inline int read(){
	int s=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		s=s*10+ch-'0';
		ch=getchar();
	}
	return s*f;
}
const int N = 1e6+7;
int n;
ll phi[N],mu[N];
int prime[N];
int flag[N];
ll F[N];
ll G[N];
ll Num[N];
int num=0;
int f(int x){
	int ans=0;
	while(x) ans+=x%10,x/=10;
	return ans;
}
int g(int x){
	int ans=1;
	while(x) ans*=x%10,x/=10;
	return ans;
}
void init(){
	phi[1]=1;
	mu[1]=1;
	F[1]=G[1]=1;
	for (int i=2;i<=n;i++){
		F[i]=f(i);
		G[i]=g(i);
		if (flag[i]==0)//这代表i是质数 
		{
			prime[++num]=i;
			phi[i]=i-1;
			mu[i]=-1;
		}
		for (int j=1;j<=num&&prime[j]*i<=n;j++){
			flag[i*prime[j]]=1;
			if (i%prime[j]==0){
				phi[i*prime[j]]=phi[i]*prime[j];
				mu[i*prime[j]]=0;
				break;
			}
			phi[i*prime[j]]=phi[i]*(prime[j]-1),mu[i*prime[j]]=-mu[i];
		}
	}
    rp(i,1,n) for(int j=i;j<=n;j+=i) Num[i]+=F[j];
}
void solve(){
	n=read();
	init();
	ll ans1=0;
    ll ans2=0;
	rp(i,1,n){
		ll t=0;for(int j=i;j<=n;j+=i) t+=F[j];
		ans1+=mu[i]*t*(n/i);
	}
    rp(i,1,n) ans2+=F[i]*phi[i];
	cout<<ans1-ans2+1<<endl;
}
int main(){
	//ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef ONLINE_JUDGE
#else
	freopen("in.txt", "r", stdin);
	//debug = 1;
#endif
	time_t beg, end;
	//if(debug) beg = clock();
    solve();

	/*
	if(debug) {
		end = clock();
		printf("time:%.2fsn", 1.0 * (end - beg) / CLOCKS_PER_SEC);
	}
	*/
	return 0;
}

B-Apple

judge:牛客

题意

现有n个苹果,求出能否分给m个人,满足所有人的苹果总数都不一样。

题解

首先求出满足m个人的苹果数量都不同的最少苹果数。最实惠的方案当然是依次拥有1,2,3,...,m-1,m个苹果。需要花费(frac{m(m+1)}{2})个苹果。至于剩下的苹果,全部交给最后一个人即可,这样就满足了所有人的苹果数都不一样。

如果苹果总数小于(frac{m(m+1)}{2}),就说明无论怎么分都无法满足每个人的苹果总数都不一样。

代码

#include <bits/stdc++.h>
using namespace std;
inline int read() {
    int s = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); }
    return s * f;
}
void solve() {
    int n = read(), m = read();
    if (n >= m * (m + 1) / 2) puts("possible");
    else puts("impossible");
}
int main() {
    int T = read();
    while (T--) solve();
    return 0;
}

E-Color Sequence

judge:牛客

题意

定义一个合法的序列为序列里的每个元素都出现偶数次。

给定一个有n个元素的序列,序列元素最多有21种,编号在数值在[0-20],求出合法序列的个数。

题解

维护每个数字出现次数的前缀和,且我们只关心数字出现次数的奇偶性,所以我们只保存 (0)(1) 两个状态,一个二进制位即可保存一个数字的状态。将 (21) 个前缀和的对应位置归纳到一起,那么一个 (int) 类型的整数就可保存一组状态。

当一组状态为 (t) 时,维护每组状态出现的次数 (num[t])

假设 (a_i)(2),前一个位置的状态为 (t),那么将 (a_i) 加入 (t),只需要将 (t) 中的第 (i) 个位置取反,即 (t=toplus(1<<a_i))。然后将 (num[t]=num[t]+1)

假设将 (a_i) 加入后的状态为 (t),则前面有 (num[t]) 个位置可以作为起点,第 (i) 个位置作为终点,区间内所有元素的出现次数都为偶数。

[ans=sum_{i=1}^{n}{num[t_i]} ]

代码

#include <bits/stdc++.h>
#define _for(i, a) for(int i = 0, lennn = (a); i < lennn; ++i)
#define _rep(i, a, b) for(int i = (a), lennn = (b); i <= lennn; ++i)
using namespace std;
typedef long long LL;
const int maxn = 100005;
const int maxm = 5000005;
const int inf = 0x3f3f3f3f;

inline int read() {
    int x(0), f(1); char ch(getchar());
    while (ch<'0' || ch>'9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}
int n;
int mp[maxm];

void sol() {
    int t = 0;
    int ans = 0;
    ++mp[t];
    _rep(i, 1, n) {
        int x = read();
        t ^= (1 << x);
        ans += mp[t];
        ++mp[t];
    }
    printf("%dn", ans);
}

int main() {
    n = read();
    sol();
    return 0;
}

G-Mathematical Practice

judge:牛客

题意

题意不详...

题解

队友一眼看出答案是 ((m+1)^n)...

代码

#include <bits/stdc++.h>
#define _for(i, a) for(register int i = 0, lennn = (a); i < lennn; ++i)
#define _rep(i, a, b) for(register int i = (a), lennn = (b); i <= lennn; ++i)
using namespace std;
typedef long long LL;
const int mod = 998244353;

inline int read() {
    int x(0), f(1); char ch(getchar());
    while (ch<'0' || ch>'9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

LL quickPow(LL x, LL n, LL mod) {
    LL ans = 1;
    while(n) {
        if(n & 1) ans *= x, ans %= mod;
        x *= x, x %= mod;
        n >>= 1;
    }
    return ans;
}

int main() {
    LL n = read(), m = read();
    printf("%lldn", quickPow(m + 1, n, mod));
    return 0;
}

H-Sequence

judge:牛客

题意

给你一个每个元素都互不相同的长度为 (n) 的序列.

定义两种操作:

  1. 选择一个元素 (a_x), 令 (a_x=y).
  2. 选择一个元素 (a_x), 找出最小值为 (a_x) 的子串的个数.

题解

考虑分块维护每个块的最小值.

  1. 当执行操作1时, 令 (a_x=y), 同时重新计算 (a_x) 所在的块的最小值.
  2. 当执行操作2时, 分别找出左右两边连续大于等于 (a_x) 的元素的个数, 例如134562中4左右分别有1和2个不小于4的元素. 那么可以计算出包含4的子串的数目为 (1+2+1times 2+1=6), 当左边有 (cl) 个元素,右边有 (cr) 个元素时,包含 (a_x) 的字串的数目为 (cl+cr+cltimes cr+1).

代码

#include <bits/stdc++.h>
#define _for(i, a) for(LL i = 0, lennn = (a); i < lennn; ++i)
#define _rep(i, a, b) for(LL i = (a), lennn = (b); i <= lennn; ++i)
using namespace std;
typedef long long LL;
const LL maxn = 100005;

inline LL read() {
    LL x(0), f(1); char ch(getchar());
    while (ch<'0' || ch>'9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

LL a[maxn], k[maxn], len;
LL n, m;

LL getN(LL pos) {
    return (pos - 1) / len + 1;
}

void init() {
    len = sqrt(n) + 1;
    len = min(len, n);
}

LL getLValOfThis(LL pos) {
    LL nu = getN(pos);
    for(LL i = pos - 1; i > (nu - 1) * len; --i) if(a[i] < a[pos]) return i;
    return -1;
}
LL getRValOfThis(LL pos) {
    LL nu = getN(pos);
    for(LL i = pos + 1; i <= nu * len; ++i) if(a[i] < a[pos]) return i;
    return -1;
}
LL getLVal(LL pos) {
    LL nu = getN(pos);
    for(LL i = nu - 1; i > 0; --i) if(k[i] < a[pos]) return i;
    return -1;
}
LL getRVal(LL pos) {
    LL nu = getN(pos), mn = getN(n);
    for(LL i = nu + 1; i <= mn; ++i) if(k[i] < a[pos]) return i;
    return -1;
}

void sol() {
    init();
    _rep(i, 1, n) a[i] = read();
    LL mn = getN(n);
    _rep(i, 1, mn) k[i] = LLONG_MAX;
    _rep(i, 1, n) k[getN(i)] = min(k[getN(i)], a[i]);
    _for(i, m) {
        LL op = read();
        if(op == 1) {
            LL x = read(), val = read();
            LL nu = getN(x);
            a[x] = val;
            k[nu] = LLONG_MAX;
            for(LL i = (nu - 1) * len + 1; i <= nu * len; ++i) k[nu] = min(k[nu], a[i]);
        }
        else {
            LL pos = read();
            LL l = pos - 1, r = pos + 1;
            LL cl = 0, cr = 0;
            LL LThis = getLValOfThis(pos);
            if(LThis != -1) cl = pos - LThis - 1;   // 本块内找到
            else {                                  // 本块内未找到,从下一块开始找
                LL nu = getLVal(pos);
                if(nu == -1) cl = pos - 1;          // 左边没有比a[pos]更小的
                else {                              // 左边有比a[pos]更小的
                    cl = pos - nu * len - 1;
                    for(LL i = min(pos - 1, nu * len); i > 0 && a[i] > a[pos]; --i) {
                        cl = pos - i;
                    }
                }
            }
            LL RThis = getRValOfThis(pos);
            if(RThis != -1) cr = RThis - pos - 1;   // 本块内找到
            else {                                  // 本块内未找到,从下一块开始找
                LL nu = getRVal(pos);
                if(nu == -1) cr = n - pos;          // 右边没有比a[pos]更小的
                else {                              // 右边有比a[pos]更小的
                    cr = (nu - 1) * len - pos;
                    for(LL i = max(pos + 1, (nu - 1) * len + 1); i <= n && a[i] > a[pos]; ++i) {
                        cr = i - pos;
                    }
                }
            }
            LL ans = cl + cr + cl * cr + 1;
            printf("%lldn", ans);
        }
    }
}

int main() {
    n = read(), m = read();
    sol();
    return 0;
}

I-Simple Math Problem

judge:牛客

题意

给你一个 (ntimes n) 的矩阵,其数字的排布方式如下:

0   1   3   6   10
2   4   7   11  15
5   8   12  16  19
9   13  17  20  22
14  18  21  23  24

求出第 (x) 行第 (y) 列的元素的值. 注意((0≤x≤1000000000, 0≤y≤1000000000, 1≤n≤1000000001)).

题解

旋转一下矩阵:

        0
      2   1
    5   4   3
  9   8   7   6
14  13  12  11  10
  18  17  16  15
    21  20  19
      23  22
        24

那么第 (x) 行第 (y) 列就对应第 (x+y-1) 行. 假设 (r=x+y-1).

  1. (x+y<=n) 时, 先算出前 (r-1) 行的数字个数 (1+2+...+r-1), 等差数列求和: (frac{r(r-1)}{2}), 再加上 (r) 行剩下的 (x) 个数, 由于题目从0开始,所以为 (frac{(x+y-1)(x+y-2)}{2}+x-1).
  2. (x+y-1>n) 时, 同样利用上面的规律算出下面得数字个数,然后拿 (ntimes n) 减去个数即可. 假设 (x' = n - x + 1, y' = n - y + 1) , (a[x][y]) 后面的数字个数为 (frac{(x'+y'-1)(x'+y'-2)}{2}+x'-1), 那么 (a[x][y]) 就是正向的第 (ntimes n-frac{(x'+y'-1)(x'+y'-2)}{2}-x'+1) 个数, 从0开始就是 (ntimes n-frac{(x'+y'-1)(x'+y'-2)}{2}-x')

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

inline int read() {
    int x(0), f(1); char ch(getchar());
    while (ch<'0' || ch>'9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

int main() {
    LL n = read(), x = read() + 1, y = read() + 1, ans = 0;
    if(x + y <= n + 1) {
        ans = (x + y - 1) * (x + y - 2) / 2 + x - 1;
    }
    else {
        LL _x = n - x + 1, _y = n - y + 1;
        ans = (_x + _y - 1) * (_x + _y - 2) / 2 + _x;
        ans = n * n - ans;
    }
    printf("%lldn", ans);
    return 0;
}

K-Travel Expense

judge:牛客

待补。。

M-Zoos's Animal Codes

judge:牛客

题意

签到题

每个园子有个园子号,每个动物有个动物号,合起来就是动物编号.

给出园子号和动物号,求出动物编号.

题解

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    string a, b;
    cin >> a >> b;
    cout << a << b;
}
内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/zjtdddg/p/14287824.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!