1002 Graph Theory Class
题意:给n个结点1,2,…n,i和j之间的边权是lcm(i+1,j+1),问最小生成树边权和
解题思路:让所有结点变成2,3,4…n+1.这样把其中所有质数向2连边,所有合数向它的因子连边。这样答案为[3,n+1]数字和+[3,n+1]质数和。然后套min25板子就行,难点是需要知道min25板子(
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
int k;
typedef long long LL;
namespace Min25 {
int prime[N], id1[N], id2[N], flag[N], ncnt, m;
LL g[N], sum[N], a[N], T, n;
inline int ID(LL x) {
return x <= T ? id1[x] : id2[n / x];
}
inline LL calc(LL x) {
return x * (x + 1) / 2 - 1;
}
inline void init() {
//for(int i=0;i<N;++i) prime[i]=id1[i]=id2[i]=flag[i]=g[i]=sum[i]=a[i]=0;
ncnt=0;m=0;
T = sqrt(n + 0.5);
for (int i = 2; i <= T; i++) {
if (!flag[i]) prime[++ncnt] = i, sum[ncnt] = sum[ncnt - 1] + i;
for (int j = 1; j <= ncnt && i * prime[j] <= T; j++) {
flag[i * prime[j]] = 1;
if (i % prime[j] == 0) break;
}
}
for (LL l = 1; l <= n; l = n / (n / l) + 1) {
a[++m] = n / l;
if (a[m] <= T) id1[a[m]] = m; else id2[n / a[m]] = m;
g[m] = calc(a[m]) % k;
}
for (int i = 1; i <= ncnt; i++)
for (int j = 1; j <= m && (LL)prime[i] * prime[i] <= a[j]; j++)
g[j] = (g[j] - (((LL)prime[i]%k) * (g[ID(a[j] / prime[i])] - sum[i - 1])%k) +k)%k;
}
inline LL solve(LL x) {
if (x <= 1) return x;
return n = x, init(), g[ID(n)];
}
}
int main() {
int T;
scanf("%d",&T);
while(T--)
{
LL n; scanf("%lld%d", &n,&k);
LL tmp=Min25::solve(n+1);
tmp=(tmp-2+k)%k;
tmp=(tmp+(((3+n+1)%k)*((n-1)%k))/2)%k;
cout<<tmp<<endl;
}
//printf("%lld\n", Min25::solve(n));
}
1003 Express Mail Taking
水题
#include<bits/stdc++.h>
#define LL long long
#define inf 0x3f3f3f3f
#define all(x) (x).begin(),(x).end()
#define PII pair<int,int>
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
#ifdef LOCAL
const int maxn=30;
#else
const int maxn=1e6+5;
#endif
int a[maxn];
int n,m,k;
void init()
{
scanf("%d%d%d",&n,&m,&k);
FOR(i,0,m) scanf("%d",a+i);
sort(a,a+m);
}
void sol()
{
int pos=1;
LL ans=0;
for(int i=m-1;i>=0;--i)
{
ans+=abs(pos-k);
ans+=abs(a[i]-k);
pos=a[i];
}
ans+=abs(pos-1);
cout<<ans<<'\n';
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
sol();
}
return 0;
}
1005 Lunch
题意:有n个数字,每次操作可以选择一个数字x,把它变成k个x/k(k是x的因子)。选择的x不可以是1.当全场只有1则输了
解题思路:
对于一个l来说,它由若干个质因子组成。对于奇数质因子,比如t,取了之后,t被拆成t个l/t,因为t是奇数,其实相当于变成了1个l/t的情况。所以l最多操作它的(奇数质因子个数)次,会变成 2 x 2^x 2x的形式。而变成 2 x 2^x 2x的时候,只要x不为0,那么不管怎么取都会变成必败态。也就是说, 2 x 2^x 2x对应了nim博弈种只剩下1个石头的状态。
那么(奇数质因子个数+ [l为偶数])就是当前局面可以操作的最多次数,对应nim博弈中的石子个数。可以理解为,如果取k = 偶数,相当于把整块石子拿完,取奇数则取多少个奇数质因子对应了取多少个石子。
然后就转化成了nim博弈问题,求异或和就好了
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
int isp[maxn], p[maxn];
int main(void) {
int N = 100010;
for (int i=2;i<=N;i++) {
if (isp[i] == 0) p[++p[0]] = i;
for (int j=1;j<=p[0] && i*p[j]<=N;j++) {
isp[i*p[j]]=1;
if (i%p[j]==0) break;
}
}
int T; scanf("%d",&T);
while (T--) {
int n; scanf("%d",&n);
int ans = 0;
for (int i=1;i<=n;i++) {
int l; scanf("%d",&l);
int g=0;
if (l%2==0) {
g++;
while (l%2==0) l/=2;
}
for (int j=2;p[j]*p[j]<=l;j++) if (l%p[j]==0) {
g++, l/=p[j];
while (l%p[j] == 0) g++, l/=p[j];
}
if (l > 1) g++;
ans ^= g;
}
if (ans) printf("W\n");
else printf("L\n");
}
return 0;
}
1006 Robotic Class
不是我写的,当我正在翻看它长长的题面的时候小伙伴已经开始敲起了代码(
据说是直接模拟题意。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n, K[505];
int a[505][2020],b[505][2020],c[505][2020],d[505][2020];
ll gao(int t, ll x, int cc) {
if (t == n) return x;
int pos = lower_bound(a[t],a[t]+K[t],x) - a[t];
if (pos < K[t] && x == a[t][pos] && cc == 1) pos++;
return gao(d[t][pos], c[t][pos]*(ll)x+b[t][pos], c[t][pos]*cc);
}
int main(void) {
//freopen("f.in","r",stdin);
int T; scanf("%d",&T);
for (int cas=1;cas<=T;cas++) {
printf("Case #%d: ",cas);
scanf("%d",&n);
for (int i=1;i<n;i++) {
scanf("%d",&K[i]);
for (int j=0;j<=K[i];j++) {
scanf("%d%d%d",&c[i][j],&b[i][j],&d[i][j]);
if (j < K[i]) scanf("%d",&a[i][j]);
}
}
int f=1;
for (int i=n-1;i>=1;i--) {
for (int j=0;j<K[i];j++) {
ll left = gao(i, a[i][j], -1);
ll right = gao(i, a[i][j], 1);
ll mid = gao(i, a[i][j], 0);
if (left != right || left != mid) {
f=0;
break;
}
}
if (f == 0) break;
}
if (f) printf("YES\n");
else printf("NO\n");
}
return 0;
}
1007 CCPC Training Class
题意:对于字符串S,定义Border(S)为S最长的不为S的前缀使得该前缀是S的后缀。
定义D(S) = D(Border(S))+1,当S为空则D(S) = 0
给一个字符串,可以重排它们。设重排出来的串是 t 1 t 2 t 3 . . . t n t_1t_2t_3...t_n t1t2t3...tn,设T[i] = t 1 t 2 . . . t i t_1t_2...t_i t1t2...ti,求 M a x ( D ( T [ i ] ) ) ( 1 < = i < = n ) Max(D(T[i])) \ (1<=i<=n) Max(D(T[i])) (1<=i<=n)最大可以是多少
解题思路:一看榜单这么多人过,看样例猜一个出现最多次的字母次数,敢交敢过。
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define lowbit(x) ((x)&(-(x)))
#define mid ((l+r)>>1)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define fors(i, a, b) for(int i = (a); i < (b); ++i)
using namespace std;
int a[26];
const int maxn = 2e5 + 50;
char s[maxn];
int main()
{
int T; cin>>T;
int ca = 0;
while(T--){
scanf("%s", s);
memset(a,0,sizeof a);
int ans = 0;
int len = strlen(s);
fors(i,0,len) ans = max(ans, ++a[s[i]-'a']);
printf("Case #%d: %d\n",++ca, ans);
}
}
1010 Reports
真 · 签到
1011 3x3 Convolution
题意:给一个n * n的矩阵,然后和另一个3 * 3的卷积核一直做卷积,a[i][j]以(i,j)位置作为3 * 3的左上角来卷积
解题思路:如果不是左上角为1的卷积核,则一定会产生流失导致最终全部为0.
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define lowbit(x) ((x)&(-(x)))
#define mid ((l+r)>>1)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define fors(i, a, b) for(int i = (a); i < (b); ++i)
using namespace std;
const int maxn = 55;
int a[maxn][maxn];
int b[3][3];
int n;
int main()
{
int T; cin>>T;
while(T--){
scanf("%d", &n);
fors(i,0,n)
fors(j,0,n) scanf("%d", &a[i][j]);
int f = 1;
fors(i,0,3) fors(j,0,3){
scanf("%d", &b[i][j]);
if(i==0&&j==0){
if(b[i][j] == 0) f = 0;
}else{
if(b[i][j] > 0) f = 0;
}
}
if(f){
fors(i,0,n)
fors(j,0,n) {
printf("%d", a[i][j]);
if(j==n-1) printf("\n");