結果
| 問題 | 
                            No.28 末尾最適化
                             | 
                    
| コンテスト | |
| ユーザー | 
                             myanta
                         | 
                    
| 提出日時 | 2017-05-04 01:08:49 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,318 bytes | 
| コンパイル時間 | 616 ms | 
| コンパイル使用メモリ | 51,236 KB | 
| 実行使用メモリ | 11,680 KB | 
| 最終ジャッジ日時 | 2024-09-14 07:12:38 | 
| 合計ジャッジ時間 | 7,109 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 1 TLE * 1 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:159:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  159 |                         scanf("%d%d%d%d", &seed, &n, &k, &b);
      |                         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            
            ソースコード
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
using ll=long long;
using ui=unsigned;
const ll MOD=100000009;
struct en_t
{
	int c[3];
	en_t operator+(const en_t& rhs)
	{
		return (en_t){c[0]+rhs.c[0], c[1]+rhs.c[1], c[2]+rhs.c[2]};
	}
	int cmp(const en_t& rhs)
	{
		if(c[0]==rhs.c[0] && c[1]==rhs.c[1] && c[2]==rhs.c[2]) return  0;
		if(c[0]<=rhs.c[0] && c[1]<=rhs.c[1] && c[2]<=rhs.c[2]) return -1;
		if(c[0]>=rhs.c[0] && c[1]>=rhs.c[1] && c[2]>=rhs.c[2]) return  1;
		return 2;
	}
};
int calc(ll x, int b)
{
	int i;
	for(i=0;x%b==0;i++) x/=b;
	return i;
}
int min(int a, int b)
{
	if(a<b) return a;
	return b;
}
int min_u(int &m, int v)
{
	if(m>v)
	{
		m=v;
		return 1;
	}
	return 0;
}
int solve(int seed, int n, int k, int b)
{
	vector<ll> x(n+1);
	vector<en_t> c(n+1);
	vector<int> e, rm;
	vector<vector<en_t> > dp;
	en_t en={{0,0,0}}, tc;
	int t, is_need_add, ret=999999;
	dp.resize(k+1);
	dp[0].push_back(en);
	t=b;
	for(int i=2;i<=t;i++)
	{
		int j;
		for(j=0;t%i==0;j++)
		{
			t/=i;
		}
		if(j)
		{
			en.c[e.size()]=j;
			e.push_back(i);
		}
	}
	x[0]=seed;
	for(int i=1;i<=n;i++)
	{
		x[i]=1+(x[i-1]*x[i-1]+x[i-1]*12345)%MOD;
	}
	for(int i=0;i<=n;i++)
	{
		for(ui j=0;j<e.size();j++)
		{
			int k;
			for(k=0;x[i]%e[j]==0;k++)
			{
				x[i]/=e[j];
			}
			c[i].c[j]=k;
		}
	}
	for(int i=0;i<=n;i++)
	{
		for(int j=min(k-1, i);j>=0;j--)
		{
			for(ui k=0;k<dp[j].size();k++)
			{
				tc=dp[j][k]+c[i];
				is_need_add=1;
				rm.clear();
				for(ui l=0;l<dp[j+1].size();l++)
				{
					int r=tc.cmp(dp[j+1][l]);
					if(r==-1)
					{
//						rm.push_back(l);
						dp[j+1][l]=dp[j+1].back();
						l--;
						dp[j+1].pop_back();
					}
					else if(r==1 || r==0) is_need_add=0;
				}
/*
				for(auto it=rm.rbegin();it!=rm.rend();++it)
				{
					dp[j+1].erase(dp[j+1].begin()+*it);
				}
*/
				if(is_need_add) dp[j+1].push_back(tc);
			}
		}
	}
	for(auto tc: dp[k])
	{
		int w;
//		printf("dp %d %d %d\n", tc.c[0], tc.c[1], tc.c[2]);
		w=tc.c[0]/en.c[0];
		for(ui i=1;i<e.size();i++)
		{
			min_u(w, tc.c[i]/en.c[i]);
		}
		min_u(ret, w);
	}
	return ret;
}
int main(void)
{
	int i, q, seed, n, k, b;
	while(scanf("%d", &q)==1)
	{
		for(i=0;i<q;i++)
		{
			scanf("%d%d%d%d", &seed, &n, &k, &b);
			printf("%d\n", solve(seed, n, k, b));
		}
	}
	return 0;
}
            
            
            
        
            
myanta