日常上板子:link
例题
函数,如何快速求?
Step 1:如果有至少一个,那么对取模
Step 2:反之,改变求和方式。图是这样的:
转换成文字:
设
当然还要终止:
快乐递归。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod=998244353;
ll f(ll x,ll y,ll z,ll n){
if(!x)return (n+1)*(y/z)%mod;
if(x>=z||y>=z)return (n*(n+1)/2%mod*(x/z)+(n+1)*(y/z)+f(x%z,y%z,z,n))%mod;
return (n*((x*n+y)/z)-f(z,z-y-1,x,((x*n+y)/z)-1)%mod+mod)%mod;
}
int main(){
int T;cin>>T;while(T--){
ll a,b,c,d;cin>>d>>a>>b>>c;
cout<<f(a,b,c,d)<<" 0 0\n";
}
return 0;
}
//40 pts好到正题了。。。恶心的推柿子要来了
情况同f:
a=0:
习题
1
Fraction
诶我小学不做出来过吗
给定,求一个使最小的最简分数 满足 。
显然的,不妨设为真分数
然后取倒数。。。
然后没了。。。
感觉作为小奥也是送分题,最多绿(逃
code:
#include<bits/stdc++.h>
using namespace std;
void solve(int a,int b,int&p,int&q,int c,int d){
if(a<b&&c>d){
p=q=1;
return;
}
solve(d%c,c,q,p,b-a*(d/c),a);
q+=(d/c)*p;
return;
}
int main(){
int a,b,c,d,p,q;
while(cin>>a>>b>>c>>d){
solve(a,b,p,q,c,d);
cout<<p<<"/"<<q<<endl;
}
return 0;
}T2
就是和T1读入方式不同。。
为了证明和T1没区别,另开read()函数
特判0的情况,不然90pts
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(ll a,ll b,ll&p,ll&q,ll c,ll d){
if(a<b&&c>d){
p=q=1;
return;
}
solve(d%c,c,q,p,b-a*(d/c),a);
q+=(d/c)*p;
return;
}
int num;
void read(ll &a,ll &b,ll &c,ll &d){
char ch;ll x=1,y=0;
cin>>ch>>ch;
for(int i=1;i<=num;i++){
cin>>ch;
x*=10;
y=y*10+ch-'0';
}
if(y==0){
a=-2;return;
}
b=d=x*2;
a=y*2-1;c=y*2+1;
ll ab=__gcd(a,b);a/=ab,b/=ab;
ll cd=__gcd(c,d);c/=cd,d/=cd;
}
int main(){
ll a,b,c,d,p,q;
while(cin>>num){
read(a,b,c,d);
if(a==-2){
cout<<1<<endl;
continue;
}
solve(a,b,p,q,c,d);
cout<<min(b,q)<<endl;
}
return 0;
}