比赛题解 【Educational Codeforces Round 13】
A. Johny Likes Numbers
简单计算
#include<cstdio>
int main(void)
{
int n,k;
scanf("%d%d",&n,&k);
printf("%d",n+(k-n%k));
return 0;
}
B. The Same Calendar
模拟,注意判断闰年
#include<cstdio>
inline bool leap(int y)
{
return y%400==0 || (y%4==0 && y%100!=0);
}
int main(void)
{
int y;
scanf("%d",&y);
int ans=y+1,d=0;
for(int nd=0; ; ++ans)
{
nd=(nd+365+leap(ans))%7;
if(leap(ans)!=leap(y)) continue;
if(nd==d)
{
printf("%d",ans);
return 0;
}
}
return 0;
}
C. Joty and Chocolate
1到n中,能被x整除的数的个数是
显然同时能被a和b整除的数,也就是能被
#include<cstdio>
#include<iostream>
typedef long long ll;
using namespace std;
ll gcd(ll a,ll b){ return b? gcd(b,a%b): a;}
ll lcm(ll a,ll b){ return a*b/gcd(a,b);}
int main(void)
{
ll n,a,b,p,q;
cin>>n>>a>>b>>p>>q;
cout<< n/a*p + n/b*q - n/lcm(a,b)*min(p,q);
return 0;
}
D. Iterated Linear Function
解法1
当
当
注意乘法溢出问题
#include<cstdio>
#include<iostream>
typedef long long ll;
const int mod = 1e9 + 7;
using namespace std;
inline ll pw(ll a,ll b)
{
b%=mod-1;
ll ans=1;
while(b)
{
if(b&1) ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
int main(void)
{
ll a,b,n,x;
cin>>a>>b>>n>>x;
if(a==1) cout<<(x+b*(n%mod))%mod;
else
{
ll pwa=pw(a,n);
cout<<( pwa*x + b*( (pwa-1)*pw(a-1,mod-2) %mod) )%mod;
}
return 0;
}
解法2
递推可以考虑使用矩阵
对于所有
所以
可以使用矩阵快速幂
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
ll c[3][3];
inline void mul(ll a[3][3],ll b[3][3])
{
for(int i=0; i<=1; ++i)
for(int j=0; j<=1; ++j)
{
c[i][j]=0;
for(int k=0; k<=1; ++k)
c[i][j] += a[i][k]*b[k][j] %mod;
c[i][j]%=mod;
}
for(int i=0; i<=1; ++i)
for(int j=0; j<=1; ++j) a[i][j]=c[i][j];
}
ll res[3][3], m[3][3];
void pw(ll a,ll b,ll n)
{
res[0][0]=res[1][1]=1;
m[0][0]=a; m[0][1]=b; m[1][1]=1;
while(n)
{
if(n&1ll) mul(res,m);
mul(m,m);
n>>=1ll;
}
}
int main(void)
{
ll a,b,n,x;
cin>>a>>b>>n>>x;
pw(a,b,n);
cout<< (x*res[0][0] + res[0][1]) %mod;
return 0;
}
E. Another Sith Tournament
n很小,所以考虑状压