H 最小公倍数
#include <iostream>
using namespace std;
typedef unsigned long long ll;
ll gcd(ll a, ll b)
{
if(!b) return a;
else
return gcd(b,a%b);
}
int main()
{
ll a,b;
cin >> a >> b;
ll tmp = gcd(a,b);
a /= tmp;
b /= tmp;
cout << (a*b)*tmp<<endl;
return 0;
}
D
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
#include<queue>
using namespace std;
#define N 1005
#define inf 0x7ffff
#define ll long long
int main()
{
ll n;
cin>>n;
ll sum=0;ll ans=0;
for(int i=1;i<=n;i++)
{
ll an=i;
while(an%5==0)
{
sum++;
an=an/5;
}
ans+=sum;
}
cout<<ans<<endl;
return 0;
}
A 容斥原理
#include <stdio.h>
#define LL long long
const int maxn =25;
LL l,r,k;
LL a[maxn];
LL sum=0;
void dfs(LL x,LL now,LL c)
{
if(x>r) return;
if(c&1){
sum+=r/x-(l-1)/x;
}
else
{
sum-=r/x-(l-1)/x;
}
for(LL i=now+1;i<=k;i++)
{
dfs(x*a[i],i,c+1);
}
}
int main()
{
while(~scanf("%llu%llu%llu",&l,&r,&k)){
sum=0;
for(LL i=1;i<=k;i++)
scanf("%llu",&a[i]);
for(LL i=1;i<=k;i++)
dfs(a[i],i,1);
printf("%llu\n",(r-l+1)-sum);
}
}
C
题目
#include<math.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define ll long long
ll f[100],x,m,k;
int a[100]={-1,1,0,0,2,10,4,40,92,352,724,2680,14200,73712,365596};
int main(void)
{
bool flag=0;
f[1]=f[2]=1;
for(int i=3;i<=100;i++)
f[i]=f[i-1]+f[i-2];
scanf("%lld%lld",&x,&m);
for(int i=1;i<=80;i++)
if(f[i]==x)
flag=1;
if(flag)
{
ll ans=1e18,sm1,sm2;
for(int i=2;i<=100;i++)
{
if(m%i) continue;
sm1=sm2=0;
while(m%i==0) sm1++,m/=i;
ll tmp=x;
while(tmp)
sm2+=tmp/i,tmp/=i;
ans=min(ans,sm2/sm1);
}
printf("%lld\n",ans);
}
else
{
ll z=x%min(13ll,m+1)+1;
printf("%d\n",a[z]);
}
return 0;
}
求n 的阶乘在 m 进制下末尾 0 的个数
这个里面 n 的阶乘可以分解成多少个 prime[i] 的个数的求解 嗯QAQ 很棒
void dfs(int x) {
if(x == n + 1) {
ans ++;
return ;
}
rep(i,1,n) {
if(vis[1][i] == 0 && vis[2][x+i+n] == 0 && vis[3][x-i+n] == 0) {
vis[1][i] = vis[2][x+i+n] = vis[3][x-i+n] = 1;
dfs(x + 1);
vis[1][i] = vis[2][x+i+n] = vis[3][x-i+n] = 0;
}
}
}
这个求八皇后的代码也挺好的呀