题目链接:1256
求逆元用拓展欧几里德--详情请看 点击打开链接
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
LL extend_gcd(LL a,LL b,LL &x,LL &y)
{
if (b==0)
{
x=1;y=0;
return a;
}
else
{
LL t=extend_gcd(b,a%b,y,x);
y-=x*(a/b);
return t;
}
}
int main()
{
LL n,m,x,y;
scanf("%lld%lld",&m,&n);
extend_gcd(m,n,x,y);
x=(x%n+n)%n;
printf("%lld\n",x);
}
题目链接: 1119
题解与 LightOJ - 1067 数论<100000左右的组合数取模求法《逆元》> 一样
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
LL dis[2000100],ni[2000100];
LL mod=1000000007;
LL po(LL xx,LL k)
{
LL lp=1,hu=xx;
while (k)
{
if (k%2==1)
lp=(lp*hu)%mod;
hu=(hu*hu)%mod;
k/=2;
}
return lp;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int da=n+m-2;
int xi=n-1;
dis[0]=ni[0]=1;
for (int i=1;i<=da;i++)
{
dis[i]=dis[i-1]*i%mod;
}
ni[xi]=po(dis[xi],mod-2);
ni[da-xi]=po(dis[da-xi],mod-2);
LL ans=dis[da]*ni[xi]%mod*ni[da-xi]%mod;
printf("%lld\n",ans);
return 0;
}
题目链接:1013
快速幂+逆元
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
LL mod=1000000007;
LL extend(LL a,LL b,LL &x,LL &y)
{
if (b==0)
{
x=1;y=0;
return a;
}
else
{
LL t=extend(b,a%b,y,x);
y-=x*(a/b);
return t;
}
}
LL pp(LL x,LL k)
{
LL lp=1,hu=x;
while (k)
{
if (k%2)
{
lp=lp*hu%mod;
}
hu=(hu*hu)%mod;
k/=2;
}
return lp;
}
int main()
{
int n;scanf("%d",&n);
LL p=pp(3,n+1);
p-=1;
LL x,y,a=2;
extend(a,mod,x,y);
x=(x%mod+mod)%mod;
LL ans=p*x%mod;
printf("%lld\n",ans);
return 0;
}
题目链接:1083
简单dp
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
LL map[600][600],dp[600][600];
int main()
{
int n;scanf("%d",&n);
memset(map,0,sizeof(map));
memset(dp,0,sizeof(dp));
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
scanf("%lld",&map[i][j]);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
dp[i][j]=max(dp[i-1][j],dp[i][j-1])+map[i][j];
printf("%lld\n",dp[n][n]);
return 0;
}
题目链接:1014
枚举----无语--
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long i,a,p;
int main()
{
scanf("%lld%lld",&p,&a);
bool fafe=true;
for (i=1;i<=p;i++)
{
if (i*i%p==a)
{
if (fafe)
{
printf("%lld",i);
fafe=false;
}
else
printf(" %lld",i);
}
}
if (fafe)
printf("No Solution\n");
else
printf("\n");
return 0;
}
题解:树状数组求和
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define low(x) x&-x
#define LL long long
int n;
LL shu[50050];
void ADD(int ii,LL xx)
{
for (ii;ii<=n;ii+=low(ii))
shu[ii]+=xx;
}
LL QUERY(int ii)
{
LL lp=0;
for (ii;ii>0;ii-=low(ii))
lp+=shu[ii];
return lp;
}
int main()
{
scanf("%d",&n);
LL a;memset(shu,0,sizeof(shu));
for (int i=1;i<=n;i++)
{
scanf("%lld",&a);
ADD(i,a);
}
int m;scanf("%d",&m);
int b,c;
while (m--)
{
scanf("%d%d",&b,&c);
printf("%lld\n",QUERY(b+c-1)-QUERY(b-1));
}
return 0;
}