A题我只能说我tm居然连多个数的最小公倍数都不会求!!!!
每次短除的数一定要除完!
每个数都要除短除的那个数!
正解暴力容斥。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
bool flag[20],exist[20];
int n,a[20],tail;
long long m;
long long ans;
using namespace std;
void check()
{
int tmp_a[20],sum=0,tmp=1;
for (int i=1;i<=n;i++)
{
tmp_a[i]=a[i];
if (flag[i]) sum++;
}
for (int i=10;i;i--)
{
int tmp_sum=0;
for (int j=1;j<=n;j++)
if (flag[j]&&(tmp_a[j]%i==0))
tmp_sum++;
if (tmp_sum>1)
{
for (int j=1;j<=n;j++)
if (flag[j]&&(tmp_a[j]%i==0))
tmp_a[j]=tmp_a[j]/i;
tmp*=i;
}
}
for (int i=1;i<=n;i++)
if (flag[i])
tmp*=tmp_a[i];
if (sum==0) return;
if (sum&1) ans=ans+(m/tmp);
else ans=ans-(m/tmp);
}
void dfs(int x)
{
if (x>n)
{
check();
return;
}
flag[x]=true;
dfs(x+1);
flag[x]=false;
dfs(x+1);
}
int main()
{
int x;
while (scanf("%d%lld",&tail,&m)!=EOF)
{
n=0;
memset(a,0,sizeof(a));
memset(flag,0,sizeof(flag));
memset(exist,0,sizeof(exist));
ans=0;
for (int i=1;i<=tail;i++)
{
scanf("%d",&x);
if (exist[x]) continue;
exist[x]=true;
a[++n]=x;
}
dfs(1);
printf("%lld\n",ans);
}
return 0;
}
E题数位DP,正解好像姿势很简单,但是我用一种很扭曲的方式过了,题目问从l-r有多少个数重复数字不超过2个,反正数最长就是18位,假设第i位为x,暴力枚举1-(x-1)的所有情况,暴力枚举只要枚举哪些用两次就行了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int sum[1100];
long long get_sum_1,get_sum_2;
char num[1100];
bool check(char s[],int len)
{
get_sum_1=0;
get_sum_2=0;
for (int i=0;i<=9;i++)
sum[i]=2;
for (int i=0;i<len;i++)
{
sum[s[i]-'0']--;
if (sum[s[i]-'0']<0) return false;
}
for (int i=0;i<=9;i++)
{
if (sum[i]==1)
get_sum_1++;
if (sum[i]==2)
get_sum_2++;
}
return true;
}
long long c(long long x,long long y)
{
long long now=1;
for (long long i=1;i<=x;i++)
now=now*(y-i+1)/i;
return now;
}
long long A(long long x,long long y)
{
long long now=1;
for (long long i=y;i>y-x;i--)
now=now*i;
return now;
}
long long count(long long x)
{
long long now=0;
for (long long i=0;i<=x/2;i++)
{
long long tmp=c(i,get_sum_2);
for (long long j=1;j<=i;j++)
tmp*=c(2,x-j*2+2);
if (get_sum_1+get_sum_2-i<x-2*i) continue;
tmp*=A(x-2*i,get_sum_1+get_sum_2-i);
now+=tmp;
}
return now;
}
long long work(char s[],int len)
{
memset(num,0,sizeof(num));
long long ans=0;
get_sum_2=9;
get_sum_1=1;
for (long long i=1;i<len;i++)
ans+=9*count(i-1);
for (int i=0;i<len;i++)
{
for (int j=0;j<=s[i]-'0'-1;j++)
{
if ((j==0)&&(i==0)) continue;
num[i]=j+'0';
if (check(num,i+1))
ans+=count(len-i-1);
}
num[i]=s[i];
if (!check(num,i+1)) return ans;
}
ans++;
return ans;
}
char s1[1100],s2[1100];
int main()
{
freopen("exchange.in","r",stdin);
freopen("exchange.out","w",stdout);
int len;
scanf("%s %s",s1,s2);
len=strlen(s1);
long long l=work(s1,len);
len=strlen(s2);
long long r=work(s2,len);
len=strlen(s1);
if (check(s1,len)) printf("%I64d",r-l+1);
else printf("%I64d",r-l);
return 0;
}