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很小,所以考虑状压
表示,集合中的人参加过比赛,现在在台上,Jedi Ivan获胜的最大概率
转移时就枚举一个没参加过的人,分别考虑打败和打败的情况
计算答案时,枚举初始的两个人即可
#include<cstdio>
#include<algorithm>
typedef long double db;
const int MAXN = 18 + 5;
const int MAXS = (1<<18) + 5;
using namespace std;
inline void chk_max(db &a,db b){ if(a<b) a=b;}
int n,maxs;
db a[MAXN][MAXN];
db dp[MAXS][MAXN];
db gao(int s,int i)
{
if(dp[s][i]>=-0.5) return dp[s][i];
if(s==maxs) return dp[s][i]=!i;
db &res=dp[s][i];
for(int j=0; j<n; ++j)
if(((s>>j)&1)==0)
chk_max(res, a[i][j]*gao(s|(1<<j), i)+a[j][i]*gao(s|(1<<j), j));
return res;
}
int main(void)
{
scanf("%d",&n);
if(n==1)
{
printf("1");
return 0;
}
for(int i=0; i<n; ++i)
for(int j=0; j<n; ++j)
{
double p;
scanf("%lf",&p);
a[i][j]=p;
}
maxs=(1<<n)-1;
for(int s=0; s<=maxs; ++s)
for(int i=0; i<n; ++i)
dp[s][i]=-1;
db ans=0;
for(int i=0; i<n; ++i)
for(int j=0; j<i; ++j)
{
int s=(1<<i)|(1<<j);
chk_max(ans, a[i][j]*gao(s,i)+a[j][i]*gao(s,j));
}
printf("%.20lf",(double)ans);
return 0;
}