http://acm.hdu.edu.cn/contests/contest_show.php?cid=808
只过了4题
1005:莫比乌斯反演/容斥原理,一开始TLE了两发,把求逆元long long改成int就过了,卡常
solved by lyy
#include
using namespace std;
#define ll long long
#define maxn 1000000
int n,m,p,t;
const int MAXN = 1000000;
bool check[MAXN+10];
int phi[MAXN+10];
int prime[MAXN+10];
int tot;
void phi_and_prime_table(int N)
{
memset(check,false,sizeof(check));
phi[1] = 1;
tot = 0;
for(int i = 2; i <= N; i++)
{
if( !check[i] )
{
prime[tot++] = i;
phi[i] = i-1;
}
for(int j = 0; j < tot; j++)
{
if(i * prime[j] > N)break;
check[i * prime[j]] = true;
if( i % prime[j] == 0)
{
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
else
{
phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
}
}
}
ll inv(int a,int m)
{
if(a == 1)return 1;
return inv(m%a,m)*(m-m/a)%m;
}
int pri[maxn+5];
int mu[maxn+5];
void getMu()
{
int i,j;
mu[1]=1;
for (i=2;i<=maxn;i++)
{
if (pri[i]==0)
{
pri[++pri[0]]=i;
mu[i]=-1;
}
for (j=1;j<=pri[0] && pri[j]<=maxn/i;j++)
{
pri[pri[j]*i]=1;
if (i%pri[j])
mu[i*pri[j]]=-mu[i];
else
{
mu[i*pri[j]]=0;
break;
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
phi_and_prime_table(MAXN);
getMu();
scanf("%d",&t);
while (t--)
{
scanf("%d%d%d",&n,&m,&p);
ll ans=0;
for (int i=1;i<=min(n,m);i++)
{
int k1=n/i;
int k2=m/i;
ll cnt=0;
for (int j=1;j<=min(k1,k2);j++)
{
if (mu[j]==1) cnt+=(ll)(k1/j)*(k2/j);
else if (mu[j]==-1) cnt-=(ll)(k1/j)*(k2/j);
}
cnt%=p;
cnt=cnt*i%p*inv(phi[i],p)%p;
ans=(ans+cnt)%p;
}
printf("%lld\n",ans);
}
return 0;
}
1001:优先队列+BFS,貌似题目/标程有问题???
solved by sdn
/*
ID: oodt
PROG:
LANG:C++
*/
#include
#include
#include
#include
#include
#include
#include
#include
1011:卡IO,用输入输出外挂还是T了,换fread就过了
solved by lyy and wyq
include
using namespace std;
#define ll long long
int t,n,k;
int v[10];
struct st
{
int id;
int data;
}a[8][100005];
int h[100005];
int cnt[10];
int b[8][100005];
namespace IO {
const int MX = 4e7;
char buf[MX]; int c, sz;
void begin() {
c = 0;
sz = fread(buf, 1, MX, stdin);
}
inline bool read(int &t) {
while(c < sz && buf[c] != '-' && (buf[c] < '0' || buf[c] > '9')) c++;
if(c >= sz) return false;
bool flag = 0; if(buf[c] == '-') flag = 1, c++;
for(t = 0; c < sz && '0' <= buf[c] && buf[c] <= '9'; c++) t = t * 10 + buf[c] - '0';
if(flag) t = -t;
return true;
}
}
bool cmp(st a,st b)
{
return a.data
1010:矩阵快速幂,队友想出来嫌比较烦就扔给我来写了
solved by wyq
#include
using namespace std;
#define ll long long
const ll mod=1000000007;
int C,D,P,n;
ll f[100005];
struct Matrix
{
long long m[3][3];
Matrix()
{
memset(m,0,sizeof(m));
for(int i=0;i<3;i++)
m[i][i] = 1LL;
}
};
Matrix Mul(Matrix a, Matrix b){
Matrix ans;
for(int i=0;i<3;++i){
for(int j=0;j<3;++j){
ans.m[i][j] = 0;
for(int k=0;k<3;++k){
ans.m[i][j] += a.m[i][k] * b.m[k][j];
ans.m[i][j] %= mod;
}
}
}
return ans;
}
struct mat2
{
ll m[3];
mat2(){}
mat2(ll a,ll b,ll c)
{
m[0]=a;
m[1]=b;
m[2]=c;
}
};
Matrix fastm(Matrix a,int b)
{
Matrix res;
while(b)
{
if(b&1)
res = Mul(res,a);
a = Mul(a,a);
b >>= 1;
}
return res;
}
Matrix e()
{
Matrix x;
x.m[0][0]=D;x.m[0][1]=C;x.m[0][2]=1;
x.m[1][0]=1;x.m[1][1]=0;x.m[1][2]=0;
x.m[2][0]=0;x.m[2][1]=0;x.m[2][2]=1;
return x;
}
mat2 mul(Matrix a,mat2 b)
{
mat2 x=mat2(0,0,0);
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
x.m[i]=(x.m[i]+a.m[i][j]*b.m[j])%mod;
}
}
return x;
}
void solve(mat2 &x,int l,int r)
{
if (l>r) return;
Matrix d=e();
d=fastm(d,r-l+1);
x=mul(d,x);
}
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%lld%lld%d%d%d%d",&f[1],&f[2],&C,&D,&P,&n);
for (int i=3;i<=100000;i++)
{
f[i]=(C*f[i-2]+D*f[i-1]+P/i)%mod;
}
if (n<=100000)
{
printf("%lld\n",f[n]);
continue;
}
else
{
ll a=f[99999];
ll b=f[100000];
int x=100000;
mat2 y(b,a,P/(x+1));
while (x=(x+1))
{
//cout<