QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#306910 | #5547. Short Function | NYCU_CartesianTree | WA | 1ms | 3856kb | C++20 | 3.7kb | 2024-01-17 16:07:35 | 2024-01-17 16:07:35 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define pb push_back
const int mol=998244353;
bool vis[100005];
vector<vector<int>>mat,ori,tot;
void go(int id){
if(id==1) tot=ori;
else tot = mat;
vector<vector<int>>iu;
for(int i=0;i<2;i++){
vector<int>tt;
for(int j=0;j<2;j++){
int sum=0;
for(int k=0;k<2;k++){
sum+=mat[i][k]*tot[k][j]%(mol-1);
sum%=mol-1;
}
tt.pb(sum);
}
iu.pb(tt);
}
mat=iu;
}
int ff1(int g,int h){
if(h==0) return 1;
if(h==1) return g;
if(h%2) return g*ff1(g,h-1)%(mol-1);
else {
int t=ff1(g,h/2);
return t*t%(mol-1);
}
}
int ff(int g,int h){
if(h==0) return 1;
if(h==1) return g;
if(h%2) return g*ff(g,h-1)%mol;
else {
int t=ff(g,h/2);
return t*t%(mol);
}
}
vector<int>a(100005);
int n,k;
void dg(int ans,int t){
t++;
if(t==n+1) t=1;
for(int i=0;i<n;i++){
cout<<ans<<" ";
ans*=ff(a[i],mol-2);
ans%=mol;
ans*=a[t-1];
ans%=mol;
t++;
if(t==n+1) t=1;
}
}
int dis[100005];//走幾圈了
int preans[100005];
int haha[100005];// 走到目標之前走幾步
void solve(){
cin>>n>>k;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<n;i++){
if(i==0) preans[i]=a[i];
else preans[i]=preans[i-1]*a[i]%mol;
}
int t=1;int lap=0;
int cnt=0;
while(vis[t]==0){
dis[t]=lap;
haha[t]=cnt;
vis[t]=1;
t+=t;lap+=lap;
lap%=(mol-1);
cnt++;
k--;
if(t>n) t-=n,lap++;
if(k==0) break;
}
lap%=(mol-1);
// return;
cnt-=haha[t],lap-=dis[t];
if(lap<0) lap+=mol-1;
// cout<<cnt<<" "<<lap<<"\n";
// cout<<vis[t]<<"\n";
// cout<<t<<"t\n";
int ans=preans[t-1];
if(!vis[t]){
ans*=ff(preans[n-1],lap);ans%=mol;
dg(ans,t);
return;
}
int o1=ff1(2,cnt);
vector<int>b={o1,0};
ori.pb(b);
b={lap,1};
ori.pb(b);
mat=ori;
// b={lap,1};
// mat.pb(b);mat.p b(b);
// k+=dis[t];
// k-=dis[t],k+=cnt;
k+=cnt;
int total=k/(cnt);// 總週期
total--;
// cout<<k<<" "<<cnt<<" "<<lap<<" "<<ans<<"\n";
int le=k%cnt;
// cout<<k<<" "<<le<<"\n";
vector<int>qq;
while(total>1){
if(total%2==0) total>>=1,qq.pb(2);
else qq.pb(1),total--;
}
reverse(qq.begin(),qq.end());
for(int ii:qq){
go(ii);
}
int laap=(mat[0][0]*lap%(mol-1)+mat[1][0])%(mol-1);// 總圈數
if(total==0) laap=lap;
ans*=ff(preans[n-1],laap);ans%=mol;
// return;
cout<<le<<"l\n";
lap=laap;
while(le){
le--,ans*=ff(preans[n-1],lap);ans%=mol;
// cerr<<lap<<" "<<t<<" "<<ans<<"\n";
lap+=lap;
lap%=mol-1;
int pp=t;
t+=t;
if(t>n) t-=n,lap++;
if(t<pp){
ans*=preans[t-1]*preans[n-1]%mol*ff(preans[pp-1],mol-2)%mol;
ans%=mol;
}
else{
ans*=preans[t-1]*ff(preans[pp-1],mol-2)%mol;
ans%=mol;
}
// cerr<<ans<<"ans\n";
ans%=mol;
}
dg(ans,t);
return;
b=a;
for(int i=0;i<k;i++){
a=b;
for(int j=0;j<n;j++) b[j]=a[j]*a[(j+(1LL<<i))%n]%mol;
int sum=0;
for(int j=0;j<n;j++) cout<<b[j]<<" ",sum+=b[j];
cout<<"\n"<<sum<<"\n";
}
//100000
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3856kb
input:
5 2 1 2 3 4 5
output:
24 120 60 40 30
result:
ok 5 number(s): "24 120 60 40 30"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
8 3 12 5 16 14 10 6 9 2
output:
14515200 14515200 14515200 14515200 14515200 14515200 14515200 14515200
result:
ok 8 numbers
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 3656kb
input:
6 10 3 7 8 2 9 5
output:
1l 56347321 169041963 833775940 811788154 844769833 639990479
result:
wrong output format Expected integer, but "1l" found