模版整理:

晒素数

void init()
{
cas = ;
for (int i = ; i < MAXD ; i++) is_prime[i] = true;
is_prime[] = is_prime[] = false;
for (int i = ; i < MAXD ; i++)
{
if (is_prime[i])
{
prime[cas++] = i;
for (int j = i + i ; j < MAXD ; j += i)
is_prime[j] = false;
}
}
}

合数分解

int x = src[i];
int cnt = ,tot = ;
for (int j = ; j < cas && prime[j] * prime[j] <= x ; j++)
{
if (x % prime[j] == )
{
res[cnt++] = prime[j];
while (x % prime[j] == )
{
tot++;
x /= prime[j];
}
}
}
if (x > )
{
res[cnt++] = x;
tot++;
}

大素数筛。1-2^31内某个长度小于10W的区间的素数个数

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const int MAXN = ;
LL prime[MAXN / ];
bool is_prime[MAXN + ];
bool flag[MAXN + ] ;
int cas; void init()
{
for (int i = ; i < MAXN ; i++) is_prime[i] = true;
is_prime[] = false;
is_prime[] = false;
cas = ;
for (int i = ; i < MAXN ; i++)
{
if (is_prime[i])
{
prime[cas++] = i;
for (int j = i + i ; j < MAXN ; j += i)
is_prime[j] = false;
}
}
} int main()
{
init();
int T,kase = ;
scanf("%d",&T);
while (T--)
{
LL a,b;
scanf("%lld%lld",&a,&b);
if (b <= )
{
int cnt = ;
for (int i = a ; i <= b ; i++)
if (is_prime[i]) cnt++;
printf("Case %d: %d\n",kase++,cnt);
continue;
}
else
{
if (a <= ) a = ;
int sz = b - a;
for (int i = ; i <= sz ; i++) flag[i] = true;
for (int i = ; i < cas && prime[i] * prime[i] <= b ; i++)
{
int k = a / prime[i];
if (k * prime[i] < a) k++;
if (k <= ) k++;
while (k * prime[i] <= b)
{
flag[k * prime[i] - a] = false;
k++;
}
}
int cnt = ;
for (int i = ; i <= sz ; i++)
if (flag[i] == true) cnt++;
printf("Case %d: %d\n",kase++,cnt);
}
}
return ;
}

判断1-N中5的因子的个数,可以将代码5改编

int cnt = ;
while (mid)
{
cnt += mid / ;
mid /= ;
}

中国剩余定理

LL china(int n,int *a,int *m)
{
LL M = ,d,y,x = ;
for (int i = ; i < n ; i++) M *= m[i];
for (int i = ; i < n ; i++)
{
LL w = M / m[i];
gcd(m[i],w,d,d,y);
x = (x + y * w * a[i]) % M;
}
return (x + M) % M;
}

POJ 1061 青蛙的约会

欧几里德解方程

(y+nt)-(x+mt) = k * L;

(n - m)t + K'L = x - y;

求最小正整数解

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
LL gcd(LL a, LL b) {return a % b == ? b : gcd(b, a % b);}
LL ext_gcd(LL a,LL b,LL &x,LL &y)
{
if (a == && b == ) return -;
if (b == )
{
x = ;
y = ;
return a;
}
LL d = ext_gcd(b,a % b,y,x);
y -= a / b * x;
return d;
}
LL x,y,m,n,L; int main()
{
while (scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&m,&n,&L) != EOF)
{
LL a = n - m, b = L, c = x - y;
LL d = gcd(a,b);
if (c % d != )
{
puts( "Impossible" );
continue;
}
a /= d;
b /= d;
LL x,y;
ext_gcd(a,b,x,y);
//cout << a << " " << b << " " << x << " " << y << endl;
x *= c / d;
y *= c / d;
//cout << y << endl;
//cout << x << endl;
LL tmp = L / d;
x = (x % tmp + tmp) % tmp;
cout << x << endl;
}
return ;
}

Light Oj 1370 Bi-shoe and Phi-shoe

题意:找到欧拉函数值大于对应数的最小欧拉函数的数

首先素数的欧拉函数值为对应数字减去1

对于任意X,X满足大于等于prime[i],小于等于prime[i + 1],那么这个区间内的所有数字的欧拉函数值都是小于等于

prime[i],那么若想找到欧拉函数大于X的那个值就是大于X的第一个素数

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
/*对任意x,有prime[i]<=x<prime[i+1]必定有EulerPhi[x]<=prime[i],
要满足phi(p)>=x那么p必定为x后面的第一个素数,进行素数打表即可。*/
const int MAXN = ;
bool is_prime[MAXN];
void init()
{
for (int i = ; i < MAXN ; i++) is_prime[i] = true;
is_prime[] = is_prime[] = false;
for (int i = ; i < MAXN ; i++)
{
if (is_prime[i])
{
for (int j = i + i ; j < MAXN ; j += i)
is_prime[j] = false;
}
}
} int main()
{
init();
int T,kase = ;
scanf("%d",&T);
while (T--)
{
int n;
scanf("%d",&n);
LL sum = ;
for (int i = ; i <= n ; i++)
{
int x;
scanf("%d",&x);
for (int j = x + ; j < MAXN ; j++)
{
if (is_prime[j])
{
sum += j;
break;
}
}
}
printf("Case %d: %lld Xukha\n",kase++,sum);
}
return ;
}

Light Oj 1356 Prime Independence

题意:找到最大的子集使得子集内任何2个数之间都不是素数的倍数的关系

此题可以转换成最大独立集来求,不过好像直接用匈牙利算法会T,所以用HK算法

建图:X部为指数为奇数,Y部为指数为偶数,原音:同奇同偶必然不可能构成一个数是另一个数的素数倍数,

一个为奇数一个为偶数时可以试除一个素数看是否存在然后建图求最大独立集合

另外此题我一开始想简单部分奇部偶部来建图最后除以2.这样可能会T。

LightOj 1341 Aladdin and the Flying Carpet

给出整数 a 和 b ,求区间[b, a] 内的 a 的约数对的个数,a 的约数对(比如[2, 3] 与 [3, 2] 为同一对)。

合数分解后得出(a0 ^ p0) * (a1 ^ p1).....

那么约束对个数为(p0 + 1) * (p1 + 1) * (p2 + 1)......  / 2;

然后从1-B枚举判断A的因子对应减去

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const int MAXN = ;
bool is_prime[MAXN];
int cas,prime[MAXN];
LL a,b; void init()
{
cas = ;
for (int i = ; i < MAXN ; i++) is_prime[i] = true;
is_prime[] = is_prime[] = false;
for (int i = ; i < MAXN ; i++)
{
if (is_prime[i])
{
prime[cas++] = i;
for (int j = i + i ; j < MAXN ; j += i)
is_prime[j] = false;
}
}
} int tot,cnt;
int num[MAXN],res[MAXN]; int main()
{
init();
int T,kase = ;
scanf("%d",&T);
while (T--)
{
scanf("%lld%lld",&a,&b);
if (a <= b * b)
{
printf("Case %d: %d\n",kase++,);
continue;
}
tot = cnt = ;
LL x = a,sum = ;
int i = ;
while(prime[i] < x && i < cas)
{
int val = ;
if (x % prime[i] == )
{
while (x % prime[i] == )
{
x /= prime[i];
val++;
}
}
sum *= (val + );
i++;
}
if (x > )
{
//res[cnt++] = x;
sum *= ;
}
//for (int i = 0 ; i < tot ; i++) sum *= (num[i] + 1);
sum /= ;
for (int i = ; i < b ; i++)
if (a % i == ) sum--;
printf("Case %d: %lld\n",kase++,sum);
}
return ;
}

Light Oj 1336 Sigma Function

定义f(x)为x的所有因子的和,计算H(x)表示从1到X中f(x)为偶数的数字的个数

首先一个数可以表示成(ao ^ p0) * (a1 ^ p1) *......

那么这个数的因子的个数可以表示成(a0 ^ 0 + a0 ^ 1 + ....a0 ^ p0) * (a1 ^ 0 + .... a1 ^ p1) * .....

可见如果为偶数条件很麻烦,如果为奇数那么就是这个所有乘积项全都为奇数

于是问题可以反向来解决,变为统计因子数目为奇数的个数

于是有几个条件第一:如果底数是2,那么这一项构成的乘积项必然是奇数

       第二:如果底数是奇数,那么只有当当前项的次数为偶数的时候该项为奇数

于是判断H(x)我们用二分来判断位置确定答案,对于f(x)为奇数利用DFS操作

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const int MAXN = ;
const LL INF = 1e12;
bool is_prime[MAXN];
int cas,prime[MAXN];
LL a,b; void init()
{
cas = ;
for (int i = ; i < MAXN ; i++) is_prime[i] = true;
is_prime[] = is_prime[] = false;
for (int i = ; i < MAXN ; i++)
{
if (is_prime[i])
{
prime[cas++] = i;
for (int j = i + i ; j < MAXN ; j += i)
is_prime[j] = false;
}
}
} LL N;
LL val[];
LL num; void dfs(LL depth,LL sum)
{
val[num++] = sum;
if (depth >= cas) return;
for (LL i = depth ; i < cas ; i++)
{
if (i == )
{
if(sum <= INF / ) dfs(i,sum * 2LL);
else return;
}
else
{
LL res = (LL)prime[i] * (LL)prime[i];
if (sum <= INF / res) dfs(i,res * sum);
else return;
}
}
} LL calcu(LL N)
{
int pos = upper_bound(val,val + num,N)- val;
return pos;
} int main()
{
init();
num = ;
dfs(,);
sort(val,val + num);
int T,kase = ;
scanf("%d",&T);
while (T--)
{
LL N;
scanf("%lld",&N);
LL ret = calcu(N);
printf("Case %d: %lld\n",kase++,N - ret);
}
return ;
}

Light Oj 1282Leading and Trailing

求N的k次方的前三位和后三位

后三位就是简单的快速幂取模

前三位利用log来处理

N ^ k = 10 ^ T,这里T可以为小数,

可以变成10 ^ x * 10 ^ y;这里X表示整数部分,y表示小数部分,那么含义什么

x表示N^k的数字位数,y表示大小。那么实际上求前三位就是求y。

然后利用POW(10,y)进行相应的运算即可

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const int MOD = ;
LL pow_mod(LL val,LL cnt)
{
LL ret = ;
while (cnt)
{
if (cnt & ) ret = ret * val % MOD;
val = val * val % MOD;
cnt >>= ;
}
return ret;
} int main()
{
int T,kase = ;
scanf("%d",&T);
while (T--)
{
LL a,b;
cin >> a >> b;
double res = pow(10.0,fmod(b * log10(1.0 * a),));
printf("Case %d: %d %03lld\n",kase++,(int)(res * 100.0),pow_mod(a,b));
}
return ;
}

LightOj 1259 Goldbach`s Conjecture

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const int MAXN = ;
bool is_prime[MAXN];
int prime[MAXN / ],cas;
void init()
{
cas = ;
for (int i = ; i < MAXN ; i++) is_prime[i] = true;
is_prime[] = is_prime[] = false;
for (int i = ; i < MAXN ; i++)
{
if (is_prime[i])
{
prime[cas++] = i;
for (int j = i + i ; j < MAXN ; j += i)
is_prime[j] = false;
}
}
} int main()
{
init();
int T,kase = ;
scanf("%d",&T);
while (T--)
{
int n;
scanf("%d",&n);
int ret = ;
for (int i = ; i < cas && prime[i] <= n / ; i++)
{
if (is_prime[n - prime[i]]) ret++;
}
printf("Case %d: %d\n",kase++,ret);
}
return ;
}

LightOj 1245 Harmonic Number (II)

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
LL N;
int main()
{
int T,kase = ;
scanf("%d",&T);
while (T--)
{
scanf("%lld",&N);
LL ret = ;
for (LL i = ; i <= sqrt(1.0 * N) ; i++)
ret += N / i;
for (LL i = ; i <= sqrt(1.0 * N) ; i++)
ret += (N / i - N / (i + )) * i;
if (N / (LL)(sqrt(1.0 * N)) == (LL)sqrt(1.0 * N))
ret -= (LL)sqrt(1.0 * N);
printf("Case %d: %lld\n",kase++,ret);
}
return ;
}

LightOj 1236 Pairs Forming LCM

求N2循环中LCM==N的个数

对于N合数分解有N = a1 ^ p1 * a2 ^ p2 * ....

对于i,j的LCM = N

那么有i = b1 ^ q1 * b2 ^ q2 * ......

   j = c1 ^ x1 * c2 ^ x2  * ......

于是有p1 = max(q1,x1)那么就得到答案了。就是qi,xi的组合方案数,另外注意循环的方式和重复计算

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const int MAXN = ;
bool is_prime[MAXN];
int prime[MAXN / ];
int cas; void init()
{
cas = ;
for (int i = ; i < MAXN ; i++) is_prime[i] = true;
is_prime[] = is_prime[] = false;
for (int i = ; i < MAXN ; i++)
{
if (is_prime[i])
{
prime[cas++] = i;
for (int j = i + i ; j < MAXN ; j += i)
is_prime[j] = false;
}
}
} int cnt,tot;
LL N;
int num[MAXN / ]; int main()
{
init();
int T,kase = ;
scanf("%d",&T);
while (T--)
{
scanf("%lld",&N);
LL x = N;
tot = cnt = ;
LL sum = ;
for (int i = ; i < cas && prime[i] <= x ; i++)
{
if (x % prime[i] == )
{
tot++;
int val = ;
while (x % prime[i] == )
{
val++;
x /= prime[i];
}
sum *= * val + ;
}
}
if (x > )
{
sum *= ;
tot++;
}
sum = sum / + ;
printf("Case %d: %lld\n",kase++,sum);
}
return ;
}

LightOj 1234 Harmonic Number

计算1/1 + 1/2 + 1/3 + 1/4 + .......

这个题有三种处理方法

第一种如果直接开对应数组是开不下的,但是时间上是够的,于是可以适当优化空间,

所遇预先没50个存一组剩下的不满足50一组的直接暴力

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const int MAXN = 1e8;
double ans[MAXN / + ]; void init()
{
ans[] = 0.0;
double cur = 0.0;
for (int i = ; i <= MAXN ; i++)
{
cur += 1.0 / (double)i;
if (i % == ) ans[i / ] = cur;
}
} int main()
{
init();
int T,kase = ;
scanf("%d",&T);
while (T--)
{
int N;
scanf("%d",&N);
int st = N / ;
double ret = ans[st];
for (int i = st * + ; i <= N ; i++)
ret += 1.0 / (double)i;
printf("Case %d: %.10lf\n",kase++,ret);
}
return ;
}

第二种是离线处理这个很好。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const double eps = 1e-;
const int MAXN = ;
struct node
{
int idx,ask;
double ans;
friend bool operator < (const node &a,const node &b)
{
return a.ask < b.ask;
}
}src[MAXN]; int cmp(const node&a,const node &b)
{
return a.idx < b.idx;
} int main()
{
int T,kase = ;
scanf("%d",&T);
for (int i = ; i <= T ; i++) scanf("%d",&src[i].ask);
for (int i = ; i <= T ; i++) src[i].idx = i;
sort(src + ,src + + T);
double cur = 0.0;
int step = ;
for (int i = ; i <= 1e8 ; i++)
{
if (step > T) break;
cur = cur + 1.0 / (double)i;
while (src[step].ask == i)
{
src[step].ans = cur;
step++;
}
}
sort(src + ,src + + T ,cmp);
for (int i = ; i <= T ; i++)
{
printf("Case %d: %.10lf\n",kase++,src[i].ans + eps);
}
return ;
}

第三个是神奇的数学结论

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const int MAXN = ;
double ret[MAXN];
#define gamma 0.57721566490153286060651209008240243104215933593992
int N; int main()
{
ret[] = 0.0;
for (int i = ; i < MAXN ; i++) ret[i] = ret[i - ] + 1.0 / (double)i;
int T,kase = ;
scanf("%d",&T);
while (T--)
{
scanf("%d",&N);
printf("Case %d: ",kase++);
if (N < MAXN) printf("%.10lf\n",ret[N]);
else
{
printf("%.10lf\n",(log(N) + log(N + )) / + gamma);
}
}
return ;
}

LightOj 1220 Mysterious Bacteria

求一个数最多是一个数的几次幂

合数分解次数求GCD

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const int MAXN = ;
const int INF = 0x3f3f3f3f;
bool is_prime[MAXN];
int cas,prime[MAXN]; void init()
{
cas = ;
for (int i = ; i < MAXN ; i++) is_prime[i] = true;
is_prime[] = is_prime[] = false;
for (int i = ; i < MAXN ; i++)
{
if (is_prime[i])
{
prime[cas++] = i;
for (int j = i + i ; j < MAXN ; j += i)
is_prime[j] = false;
}
}
} int main()
{
init();
int T,kase = ;
scanf("%d",&T);
while (T--)
{
int val = ;
LL N;
scanf("%lld",&N);
LL x = N;
if (x < 0LL) x = -x;
for (int i = ; i < cas && prime[i] <= x ; i++)
{
int cnt = ;
if (x % prime[i] == )
{
while (x % prime[i] == )
{
cnt++;
x /= prime[i];
}
val = gcd(val,cnt);
}
}
if (x > )
{
val = ;
}
if (N < )
{
while (val % == ) val /= ;
}
printf("Case %d: %d\n",kase++,val);
}
return ;
}

LightOj 1214 Large Division

判断大数是不是能整除。一个是直接JAVA水,另一个利用同余定理做

JAVA:

import java.util.*;
import java.math.BigInteger;
import java.util.Scanner; public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
BigInteger a,b,d;
int T;
T = sc.nextInt();
int kase = ;
while (T-- != )
{
a = new BigInteger(sc.next());
b = new BigInteger(sc.next());
a = a.abs();
b = b.abs();
if (a.mod(b).equals(BigInteger.ZERO))
{
System.out.println("Case " + kase++ + ": divisible");
}
else
{
System.out.println("Case " + kase++ + ": not divisible");
}
}
}
}
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const int MAXN = ;
char str[MAXN];
LL m; int main()
{
int T,kase = ;
scanf("%d",&T);
while (T--)
{
scanf("%s%lld",str,&m);
LL cur = ;
int len = strlen(str);
for (int i = ; i < len ; i++)
{
if (str[i] == '-') continue;
cur = (cur * + str[i] - '') % m;
}
if (cur == ) printf("Case %d: divisible\n",kase++);
else printf("Case %d: not divisible\n",kase++);
}
return ;
}

LightOj 1213 Fantasy of a Summation

利用简单的方法计算相应的代码

感觉这个题有点期望的意思

一共有N^K中组合,每种组合K个数字,平均分给N个数字,然后一直乘到一起

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const int MAXN = ;
LL sum;
LL N,K,MOD;
LL src[MAXN]; LL pow_mod(LL x,int cnt)
{
LL ret = ;
while (cnt)
{
if (cnt & ) ret = ret * x % MOD;
x = x * x % MOD;
cnt >>= ;
}
return ret;
} int main()
{
int T,kase = ;
scanf("%d",&T);
while (T--)
{
scanf("%lld%lld%lld",&N,&K,&MOD);
for (int i = ; i <= N ; i++)
scanf("%lld",&src[i]);
LL ret = ;
for (int i = ; i <= N ; i++)
{
ret = (ret + src[i]) % MOD;
}
LL val = pow_mod(N,K - );
printf("Case %d: %lld\n",kase++,ret * val % MOD * K % MOD);
}
return ;
}

LightOj 1197 Help Hanzo

区间内素数的个数,大区间的。区间长度很少

大区间筛小区间的题目

利用预先筛根号N的素数然后做就行了。这个是个模版类的问题

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const int MAXN = ;
LL prime[MAXN / ];
bool is_prime[MAXN + ];
bool flag[MAXN + ] ;
int cas; void init()
{
for (int i = ; i < MAXN ; i++) is_prime[i] = true;
is_prime[] = false;
is_prime[] = false;
cas = ;
for (int i = ; i < MAXN ; i++)
{
if (is_prime[i])
{
prime[cas++] = i;
for (int j = i + i ; j < MAXN ; j += i)
is_prime[j] = false;
}
}
} int main()
{
init();
int T,kase = ;
scanf("%d",&T);
while (T--)
{
LL a,b;
scanf("%lld%lld",&a,&b);
if (b <= )
{
int cnt = ;
for (int i = a ; i <= b ; i++)
if (is_prime[i]) cnt++;
printf("Case %d: %d\n",kase++,cnt);
continue;
}
else
{
if (a <= ) a = ;
int sz = b - a;
for (int i = ; i <= sz ; i++) flag[i] = true;
for (int i = ; i < cas && prime[i] * prime[i] <= b ; i++)
{
int k = a / prime[i];
if (k * prime[i] < a) k++;
if (k <= ) k++;
while (k * prime[i] <= b)
{
flag[k * prime[i] - a] = false;
k++;
}
}
int cnt = ;
for (int i = ; i <= sz ; i++)
if (flag[i] == true) cnt++;
printf("Case %d: %d\n",kase++,cnt);
}
}
return ;
}

LightOj 1138 Trailing Zeroes (III)

最小的数阶乘末尾的0的个数

每个因子2*5可以出个末尾0,那么有从小到大的过程中,2的数量绝对够多,于是变成了从1到N中5的因子的个数

另外为了优化需要套上一个二分代码。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
LL Q;
const LL INF = 0x3f3f3f3f; bool judge(LL mid)
{
int cnt = ;
while (mid)
{
cnt += mid / ;
mid /= ;
}
return cnt >= Q;
} int main()
{
int T,kase = ;
scanf("%d",&T);
while (T--)
{
scanf("%lld",&Q);
LL L = ,R = INF;
LL ans = -;
while (L <= R)
{
LL mid = (L + R) / ;
if (judge(mid))
{
R = mid - ;
ans = mid;
}
else L = mid + ;
}
bool flag = true;
LL tmp = ans;
LL val = ;
while (tmp)
{
val += tmp / ;
tmp /= ;
}
if (val != Q) flag = false;
if (flag) printf("Case %d: %lld\n",kase++,ans);
else printf("Case %d: impossible\n",kase++);
}
return ;
}

接下来是几个大白的题目

Uva 11426 GCD - Extreme (II)

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
const int MAXN = ;
int phi[MAXN + ];
LL ret[MAXN + ]; void init()
{
memset(ret,,sizeof(ret));
for(int i = ; i <= MAXN ; i++) phi[i] = i;
for(int i = ; i <= MAXN; i ++)
{
if(phi[i] == i)
{
for(int j = i ; j <= MAXN ; j += i)
phi[j] = phi[j] / i * (i - );
}
for(int j = ; j * i <= MAXN ; j ++)
ret[j * i] += j * phi[i];
}
for (int i = ; i <= MAXN ; i++) ret[i] += ret[i - ];
} int main()
{
init();
int N;
while (scanf("%d",&N) != EOF) if (N == ) break;
else printf("%lld\n",ret[N]);
return ;
}

UVA 11754 Codefeat

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
void gcd(LL a,LL b,LL &d,LL &x,LL &y)
{
if (b == ) {d = a; x = ; y = ;}
else {gcd(b,a % b,d,y,x); y -= x * (a / b);}
}
LL china(int n,int *a,int *m)
{
LL M = ,d,y,x = ;
for (int i = ; i < n ; i++) M *= m[i];
for (int i = ; i < n ; i++)
{
LL w = M / m[i];
gcd(m[i],w,d,d,y);
x = (x + y * w * a[i]) % M;
}
return (x + M) % M;
}
const int MAXC = ;
const int MAXK = ;
const int LIMIT = ;
set<int>value[MAXC];
int C,X[MAXC],K[MAXC];
int Y[MAXC][MAXK];
void slove_enum(int S,int bc)
{
for (int c = ; c < C; c++) if (c != bc)
{
value[c].clear();
for (int i = ; i < K[c] ; i++) value[c].insert(Y[c][i]);
}
for (int t = ; S != ; t++)
{
for (int i = ; i < K[bc] ; i++)
{
LL n = (LL)X[bc] * t + Y[bc][i];
if (n == ) continue;
bool flag = true;
for (int c = ; c < C; c++)
{
if (c != bc)
{
if (value[c].count(n % X[c]) == ) {flag = false; break;}
}
}
if (flag) { printf("%lld\n",n); if (--S == ) break;}
}
}
}
int a[MAXC];
vector<LL>sol;
void dfs(int depth)
{
if (depth == C) sol.push_back(china(C,a,X));
else for (int i = ; i < K[depth]; i++)
{
a[depth] = Y[depth][i];
dfs(depth + );
}
}
void slove_china(int S)
{
sol.clear();
dfs();
sort(sol.begin(),sol.end());
LL M = ;
for (int i = ; i < C; i++) M *= X[i];
vector<LL>ans;
for (int i = ; S != ; i++)
{
for (int j = ; j < (int)sol.size(); j++)
{
LL n = M * i + sol[j];
if (n > )
{
printf("%lld\n",n);
if (--S == ) break;
}
}
}
}
int main()
{
int S;
while (scanf("%d%d",&C,&S) != EOF)
{
if (C == ) break;
LL tot = ;
int best = ;
for (int c = ; c < C ; c++)
{
scanf("%d%d",&X[c],&K[c]);
tot *= K[c];
for (int i = ; i < K[c]; i++) scanf("%d",&Y[c][i]);
sort(Y[c],Y[c] + K[c]);
if (K[c] * X[best] < K[best] * X[c]) best = c;
}
if (tot > LIMIT) slove_enum(S,best);
else slove_china(S);
printf("\n");
}
return ;
}

UVA 11916 Emoogle Grid

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
LL N,K,B,R;
const LL MOD = ;
const int MAXN = ;
int x[MAXN],y[MAXN];
LL M;
set<pair<int,int> >s; LL pow_mod(LL x,int cnt)
{
LL ret = ;
while (cnt)
{
if (cnt & ) ret = ret * x % MOD;
x = x * x % MOD;
cnt >>= ;
}
return ret;
} void ext_gcd(LL a,LL b,LL &d,LL &x,LL &y)
{
if (b == ) {d = a; x = ; y = ;}
else {ext_gcd(b,a % b,d,y,x); y -= x * (a / b);}
} LL inv(LL a,LL n)
{
LL d,x,y;
ext_gcd(a,n,d,x,y);
return d == ? (x + n) % n : -;
} LL log_mod(LL a,LL b,LL n)
{
LL m,v,e = ;
m = (LL)sqrt(n + 0.5);
v = inv(pow_mod(a,m),n);
map<LL,LL>x;
x[] = ;
for (int i = ; i < m ; i++)
{
e = e * a % m;
if (!x.count(e)) x[e] = (LL)i;
}
for (int i = ; i < m ; i++)
{
if (x.count(b)) return (LL)i * m + x[b];
b = b * v % n;
}
return -;
} LL getnum()
{
int tot = ;
for (int i = ; i <= B ; i++)
{
if (x[i] != M && !s.count(make_pair(x[i] + ,y[i]))) tot++;
}
tot += N;
for (int i = ; i <= B ; i++) if (x[i] == ) tot--;
return (pow_mod(K,tot) * pow_mod(K - ,(M * N - tot - B))) % MOD;
} LL calcu()
{
LL cnt = getnum();
printf("%d\n",cnt);
if (cnt == R) return M;
int num = ;
for (int i = ; i <= B ; i++) if (x[i] == M) num++;
cnt = (cnt * pow_mod(K,num)) % MOD;
cnt = (cnt * pow_mod(K - ,N - num)) % MOD;
if (cnt == R)return M + ;
//printf("%lld %lld\n",pow_mod(K - 1,N),pow_mod(K - 1,N));
return log_mod(pow_mod(K - ,N),R * pow_mod(K - ,N),MOD) + M + ;
}
int main()
{
int T,kase = ;
scanf("%d",&T);
while (T--)
{
scanf("%lld%lld%lld%lld",&N,&K,&B,&R);
R %= MOD;
s.clear();
M = ;
for (int i = ; i <= B ; i++)
{
scanf("%d%d",&x[i],&y[i]);
M = max(M,(LL)x[i]);
s.insert(make_pair(x[i],y[i]));
}
printf("Case %d: %lld\n",kase++,calcu());
}
return ;
}

POJ 2115 同青蛙的约会

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
LL gcd(LL a, LL b) {return a % b == ? b : gcd(b, a % b);}
LL ext_gcd(LL a,LL b,LL &x,LL &y)
{
if (a == && b == ) return -;
if (b == )
{
x = ;
y = ;
return a;
}
LL d = ext_gcd(b,a % b,y,x);
y -= a / b * x;
return d;
}
LL A,B,C,K; int main()
{
while (scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&K) != EOF)
{
if (A == && B == && C == && K == ) break;
LL a = C, b = (1LL << K), c = B - A;
LL d = gcd(a,b);
if (c % d != )
{
puts( "FOREVER" );
continue;
}
a /= d;
b /= d;
LL x,y;
ext_gcd(a,b,x,y);
//cout << a << " " << b << " " << x << " " << y << endl;
x *= c / d;
y *= c / d;
//cout << y << endl;
//cout << x << endl;
LL tmp = (1LL << K) / d;
x = (x % tmp + tmp) % tmp;
printf("%I64d\n",x);
}
return ;
}

HDU 2161 Prime太水了。

UVA 11827 太水了

UVA 10200 太水了

SGU 106 The equation

判断欧几里德可行解在固定区间内的个数

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
LL gcd(LL a, LL b) {return a % b == ? b : gcd(b, a % b);}
LL a,b,c,x1,x2,yy1,y2;
LL x0,yy0;
LL ext_gcd(LL a,LL b,LL &x,LL &y)
{
if (a == && b == ) return -;
if (b == )
{
x = ;
y = ;
return a;
}
LL d = ext_gcd(b,a % b,y,x);
y -= a / b * x;
return d;
} int main()
{
while (scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&x1,&x2,&yy1,&y2) != EOF)
{
c = -c;
if (c < )
{
a = -a;
b = -b;
c = -c;
}
if (a < )
{
a = -a;
LL tmp = -x1;
x1 = -x2;
x2 = tmp;
}
if (b < )
{
b = -b;
LL tmp = -yy1;
yy1 = - y2;
y2 = tmp;
}
if (a == && b == )
{
if (c == )
{
printf("%I64d\n",(x2 - x1 + ) * (y2 - yy1 + ));
}
printf("%d\n",);
continue;
}
else if (a == )
{
if (c % b == )
{
if (c / b >= yy1 && c / b <= y2)
{
printf("%I64d\n",x2 - x1 + );
}
else printf("%d\n",);
}
continue;
}
else if (b == )
{
if (c % a == )
{
if (c / a >= x1 && c / a <= x2)
{
printf("%I64d\n",y2 - yy1 + );
}
printf("%d\n",);
}
continue;
}
LL d = gcd(a,b);
if (c % d != )
{
puts("");
continue;
}
a = a / d;
b = b / d;
c = c / d;
ext_gcd(a,b,x0,yy0);
x0 *= c;
yy0 *= c;
// printf("%I64d %I64d %I64d %I64d %I64d\n",a,b,c,x0,yy0);
LL l1 = (LL)ceil((double)(x1 - x0) / (double)(b));
LL l2 = (LL)ceil((double)(yy0 - y2) / (double)(a));
LL r1 = (LL)floor((double)(x2 - x0) / (double)(b));
LL r2 = (LL)floor((double)(yy0 - yy1) / (double)(a));
// cout << l1 << " " << r1 << endl;
// cout << l2 << " " << r2 << endl;
LL l = max(l1,l2);
LL r = min(r1,r2);
if (l > r) puts("");
else printf("%I64d\n",r - l + 1LL);
}
return ;
}

POJ 2478 直接欧拉前N想和。

UVA 11752 The Super Powers

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL unsigned long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
set<LL>ans;
set<LL>::iterator it;
bool is_prime[]; void init()
{
for (int i = ; i < ; i++) is_prime[i] = true;
is_prime[] = is_prime[] = true;
for (int i = ; i < ; i++)
{
if (is_prime[i])
{
for (int j = i + i ; j < ; j += i)
is_prime[j] = false;
}
}
} int main()
{
init();
ans.clear();
ans.insert();
for (LL i = ; i < ( << ) ; i++)
{
LL limit = (LL)ceil(64.0 * log10(2.0) / log10(i));
//cout << i << " " << limit << endl;
// scanf("%*d");
LL cur = ;
for (int j = ; j < limit ; j++)
{
cur *= i;
if (is_prime[j] == false)
{
ans.insert(cur);
// cout << cur << endl;
// scanf("%*d");
}
}
}
int cnt = ;
for (it = ans.begin() ; it != ans.end() ; it++)
cout << *it << endl;
return ;
}

kuangbin 带你飞 数学基础的更多相关文章

  1. 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开

    [kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...

  2. KUANGBIN带你飞

    KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题    //201 ...

  3. Tarjan 联通图 Kuangbin 带你飞 联通图题目及部分联通图题目

    Tarjan算法就不说了 想学看这 https://www.byvoid.com/blog/scc-tarjan/ https://www.byvoid.com/blog/biconnect/ 下面是 ...

  4. 「kuangbin带你飞」专题十四 数论基础

    layout: post title: 「kuangbin带你飞」专题十四 数论基础 author: "luowentaoaa" catalog: true tags: mathj ...

  5. 「kuangbin带你飞」专题二十 斜率DP

    layout: post title: 「kuangbin带你飞」专题二十 斜率DP author: "luowentaoaa" catalog: true tags: mathj ...

  6. 「kuangbin带你飞」专题二十二 区间DP

    layout: post title: 「kuangbin带你飞」专题二十二 区间DP author: "luowentaoaa" catalog: true tags: - ku ...

  7. 「kuangbin带你飞」专题十九 矩阵

    layout: post title: 「kuangbin带你飞」专题十九 矩阵 author: "luowentaoaa" catalog: true tags: mathjax ...

  8. 「kuangbin带你飞」专题十八 后缀数组

    layout: post title: 「kuangbin带你飞」专题十八 后缀数组 author: "luowentaoaa" catalog: true tags: - kua ...

  9. 「kuangbin带你飞」专题十七 AC自动机

    layout: post title: 「kuangbin带你飞」专题十七 AC自动机 author: "luowentaoaa" catalog: true tags: - ku ...

随机推荐

  1. linux学习总结----对象

    内置对象: Date new Date() --->系统当前时间 var d=new Date() d.getFullYear() getMonth() getDay() getDate() g ...

  2. Docker容器-入门级

    1.1 容器简介 1.1.1 什么是 Linux 容器 Linux容器是与系统其他部分隔离开的一系列进程,从另一个镜像运行,并由该镜像提供支持进程所需的全部文件.容器提供的镜像包含了应用的所有依赖项, ...

  3. CentOS环境配置Hadoop(一)

    配置Linux开发环境(hadoop-2.6.4) 一.准备工具 VMware-workstation-10.0.1注册机 CentOS-6.5-x86_64-bin-DVD1 jdk-7u79-li ...

  4. LeetCode 410——分割数组的最大值

    1. 题目 2. 解答 此题目为 今日头条 2018 AI Camp 5 月 26 日在线笔试编程题第二道--最小分割分数. class Solution { public: // 若分割数组的最大值 ...

  5. Browser-Solidity的本地安装及使用介绍

    Browser-Solidity的本地安装及使用介绍 正所谓工欲善其事必先利其器,巧妇也难为无米之炊,所以在学习智能合约之前,必须要先把工具准备好.Browser-Solidity 是 Ethereu ...

  6. remix-ide的三种使用方式

    如何实现一个hello word语句输出 这里写图片描述 {{uploading-image-404522.png(uploading...)}} 代码演示运行方式 VS code IDE,代码编写, ...

  7. zuoyebiji

  8. Flink之状态之状态获取

    1.什么是状态 对于任何一个操作,都可以被看成是一个函数,比如y=f(x),如果对于同一个x的任何一次输入,得到的y都是相同的,则可以认为这个函数是无状态,否则,这个函数就是有状态的.Flink的一大 ...

  9. Luogu3952 NOIP2017时间复杂度

    搞一个栈模拟即可.对比一下和一年前考场上的代码233 //2018.11.8 #include<iostream> #include<cstdio> #include<c ...

  10. 后缀数组 模板题 hdu1403(最长公共(连续)子串)

    好气啊,今天没有看懂后缀树和后缀自动机 只能写个后缀数组发泄一下了orz #include <cstdio> #include <cstring> *; int wa[N], ...