2021CCPC湘潭全国邀请赛题解
Posted ZZXzzx0_0
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021CCPC湘潭全国邀请赛题解相关的知识,希望对你有一定的参考价值。
题目pdf下载
提取码: abcd
一共A了5题,j题签到题没出来,如果罚时少一点并且把J也写了的话,也许就金了
A. A+B Problem
题意:
给定a和b,求a+b,a+b如果大于1023或者小于-1024,就自动溢出
思路:模拟
比如1023+1=1024,1024>1023 就自动溢出成-1024,模拟即可
时间复杂度:O n
#include<bits/stdc++.h>
#define fer(i,a,b) for(re i = a ; i <= b ; ++ i)
#define re register int
#define pll pair<int,int>
#define x first
#define y second
#define sf(x) scanf("%d",&x)
#define sfl(x) scanf("%lld",&x)
typedef long long ll ;
using namespace std;
const int N = 1e6 + 10 , M = 1010 , inf = 0x3f3f3f3f , mod = 1e9 + 7 ;
int main()
int t ;
cin >> t ;
while(t--)
int a , b ;
cin >> a >> b ;
int k = a + b ;
if(k > 1023) k -= 2048 ;
else if(k < -1024) k += 2048 ;
cout << k << "\\n" ;
return 0;
C. Calculate
题意:
给定x1,x2,y1,y2
求
答案对1e9+7取模
思路:数论分块
一维数论整除分块
第4个式子的答案有点问题
对其每一个区间左端点为l,右端点为r,值为cnt
答案为(r-l+1)
×
\\times
× cnt
×
\\times
×
∑
i
=
x
1
x
2
⌊
i
x
1
⌋
\\sum_i=x1^x2 \\lfloor \\fracix1 \\rfloor
∑i=x1x2⌊x1i⌋
在考虑一下 求
∑
i
=
x
1
x
2
⌊
i
x
1
⌋
\\sum_i=x1^x2 \\lfloor \\fracix1 \\rfloor
∑i=x1x2⌊x1i⌋
可以发现,
当x1<=i<2*x1 ,i/x1等于1
当2*x1<=i<3*x1,i/x1等于2
...........
当n*x1<=i<(n+1)*x1 , i/x1等于n
因此答案为 x1 * (1 + 2 + ........ + n ) + 大于等于(n+1)*x1的部分暴力求
在考虑一下1式
答案为 x1 * (1^2 + 2^2 + ........ + n^2) + 大于等于(n+1)*x1的部分暴力求
中间括号的部分用公式o1求即可
时间复杂度:O t n \\sqrtn n
#include <iostream>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
ll qpow(ll a, ll b)
ll res = 1;
while (b)
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
return res;
ll inv6 = qpow(6, mod - 2);
ll inv2 = qpow(2, mod - 2);
ll cal_a2(ll x1, ll x2)
ll a = 0;
ll k = x2 / x1 - 1;
ll k2 = x2 / x1;
ll k1 = x2 % x1 + 1;
a = x1 * k % mod * (k + 1) % mod * (2 * k + 1) % mod * inv6 % mod;
a = (a + k1 % mod * k2 % mod * k2 % mod) % mod;
return a;
ll cal_b2(ll x1, ll x2)
ll b = 0;
for (ll l = x1, r; l <= x2; l = r + 1)
r = x2 / (x2 / l);
b += (r - l + 1) * (x2 / l) % mod * (x2 / l) % mod;
b %= mod;
return b;
ll cal_a(ll x1, ll x2)
ll a1 = 0;
ll k = x2 / x1 - 1;
ll k2 = x2 / x1;
ll k1 = x2 % x1 + 1;
a1 = (a1 + x1 * k % mod * (1 + k) % mod * inv2 % mod) % mod;
a1 = (a1 + k1 * k2 % mod) % mod;
return a1;
ll cal_b(ll x1, ll x2)
ll b = 0;
for (ll l = x1, r; l <= x2; l = r + 1)
r = x2 / (x2 / l);
b += (r - l + 1) * (x2 / l);
b %= mod;
return b;
ll cal_ab(ll x1, ll x2)
ll ab = 0;
for (ll l = x1, r; l <= x2; l = r + 1)
r = x2 / (x2 / l);
ll v = x2 / l;
ll f = cal_a(x1, r) - cal_a(x1, l - 1);
f = (f + mod) % mod;
ab = (ab + v * f % mod) % mod;
return ab;
int main()
int t;
cin >> t;
while (t--)
ll x1, x2, y1, y2;
cin >> x1 >> x2 >> y1 >> y2;
ll a1 = 0, b1 = 0, c1 = 0, d1 = 0;
//b d que
ll dx = x2 - x1 + 1;
ll dy = y2 - y1 + 1;
a1 = cal_a2(x1, x2) % mod;
b1 = cal_b2(x1, x2) % mod;
c1 = cal_a2(y1, y2) % mod;
d1 = cal_b2(y1, y2) % mod;
ll a = 0, b = 0, c = 0, d = 0;
a = cal_a(x1, x2);
c = cal_a(y1, y2);
b = cal_b(x1, x2);
d = cal_b(y1, y2);
ll ab = 0, cd = 0;
ab = cal_ab(x1, x2) * dy % mod;
cd = cal_ab(y1, y2) * dx % mod;
ll ans = 0;
ans = (ans + (((dy * a1 % mod + dy * b1 % mod) % mod + dx * c1 % mod) % mod + dx * d1 % mod) % mod) % mod;
ans = (ans + 2 * ab % mod) % mod;
ans = (ans + 2 * a * c % mod) % mod;
ans = (ans + 2 * a * d % mod) % mod;
ans = (ans + 2 * b * c % mod) % mod;
ans = (ans + 2 * b * d % mod) % mod;
ans = (ans + 2 * cd % mod) % mod;
ans %= mod;
printf("%lld\\n", ans);
return 0;
E. CCPC Strings
题意:
给一个整数n,会有2^n个不同的cp字符串
求每个不同字符串的ccpc子串的个数的总和
答案对1e9+7取膜
思路:bm算法求线性通项公式
先暴力预处理前20项
然后在用bm算法求出线性递推方程
o1输出即可
时间复杂度:20 * 2^20 + t
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
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; assert(b>=0); for(;b;b>>=1)if(b&1)res=res*a%mod;a=a*a%mod;return res;
// head
int _;
ll n;
namespace linear_seq
const int N=10010;
ll res[N],base[N],_c[N],_md[N];
vector<int> Md;
ll mul(ll *a,ll *b,int k)
rep(i,0,k+k) _c[i]=0;
rep(i,0,k) if (a[i]) rep(j,0,k) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
for (int i=k+k-1;i>=k;i--) if (_c[i])
rep(j,0,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
rep(i,0,k) a[i]=_c[i];
ll solve(ll n,VI a,VI b) // a 绯绘暟 b 鍒濆€?b[n+1]=a[0]*b[n]+...
// printf("%d\\n",SZ(b));
ll ans=0,pnt=0;
int k=SZ(a);
assert(SZ(a)==SZ(b));
rep(i,0,k) _md[k-1-i]=-a[i];_md[k]=1;
Md.clear();
rep(i,0,k) if (_md[i]!=0) Md.push_back(i);
rep(i,0,k) res[i]=base[i]=0;
res[0]=1;
while ((1ll<<pnt)<=n) pnt++;
for (int p=pnt;p>=0;p--)
mul(res,res,k);
if ((n>>p)&1)
for (int i=k-1;i>=0;i--) res[i+1]=res[i];res[0]=0;
rep(j,0,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
rep(i,0,k) ans=(ans+res[i]*b[i])%mod;
if (ans<0) ans+=mod;
return ans;
VI BM(VI s)
VI C(1,1),B(以上是关于2021CCPC湘潭全国邀请赛题解的主要内容,如果未能解决你的问题,请参考以下文章