#include <iostream> #include <map> #include <vector> using namespace std; int main(){ map<int,bool> m; //記錄已經出現的餘數 vector<int> v; //依序記錄餘數 int a,b,c,d; //a被除數, b除數, c次方, d餘數 cin >> a >> b >> c; d = a%b; //處理 a<b 的情形 while(!m[d]){ m[d]=true; v.push_back(d); d=(d*a)%b; } c = c%m.size(); cout<<v[c-1]; system("pause"); return 0; } /* 5 7 1 => 5 //(5^1)%7, 5%7 5 7 100 => 2 //(5^100)%7 */
2013年11月18日 星期一
5 的 100次方 mod 7 (更精簡版)
2013年11月11日 星期一
5 的 100次方 mod 7
#include <iostream>
#include <map>
using namespace std;
int main(){
map<int,bool> m; //記錄已經出現的餘數
int a,b,c,d;
cin >> a >> b>>c;
d = a;
while(!m[d]){
m[d]=true;
d=(d*a)%b;
}
//cout << m.size();
c = c%m.size();
d = a;
for(int i=0;i<c-1;i++){
d=(d*a)%b;
}
cout<<d;
system("pause");
return 0;
}/*
5 7 1 => 5
5 7 100 => 2
*/
#include <map>
using namespace std;
int main(){
map<int,bool> m; //記錄已經出現的餘數
int a,b,c,d;
cin >> a >> b>>c;
d = a;
while(!m[d]){
m[d]=true;
d=(d*a)%b;
}
//cout << m.size();
c = c%m.size();
d = a;
for(int i=0;i<c-1;i++){
d=(d*a)%b;
}
cout<<d;
system("pause");
return 0;
}/*
5 7 1 => 5
5 7 100 => 2
*/
循環節的長度
#include <iostream>
#include <map>
using namespace std;int main(){
map<int,bool> m;
int a,b;
cin >> a >> b;
a=a%b;
while(!m[a]){
m[a]=true;
a=(a*10)%b;
}
cout << m.size();
system("pause");
return 0;
}
#include <map>
using namespace std;int main(){
map<int,bool> m;
int a,b;
cin >> a >> b;
a=a%b;
while(!m[a]){
m[a]=true;
a=(a*10)%b;
}
cout << m.size();
system("pause");
return 0;
}
/*
注意用不是用商判斷,用餘數判斷 14143 99999 => 5
1 7 => 7
1231234 9999999 => 7
注意用不是用商判斷,用餘數判斷 14143 99999 => 5
1 7 => 7
1231234 9999999 => 7
Excel
=QUOTIENT(B2,99999)
=MOD(B2,99999)
*/
2013年8月30日 星期五
bridge and torch (not optimal solution)
#include <iostream> #include <algorithm> using namespace std; void sol1(); void print_array(int *A, int n){ cout << "{ "; for(int i=0;i<n;i++){ cout << A[i] << ' '; } cout << '}'; } void print_status(int *A, int n, int *B, int m, int cost){ cout << endl; print_array(A,n); print_array(B,m); cout << ":" << cost; cout << endl; } void sol2(){ int n; cin >> n; int *A= new int[n]; int *B=A+n; int m=0; int cost=0; for(int i=0;i<n;i++){ cin >> A[i]; } if (n<=0){ cout << "n must be larger than 0"; system("pause"); return ; }else if(n==1){ cout << "cost:" << A[0]; system("pause"); return ; }else if(n==2){ cout << "cost:" << max(A[0],A[1]); system("pause"); return ; } sort(A,A+n); print_status(A,n,B,m,cost); while(n>0){ //GO B--;B--; cost+=A[n-1]; swap(A[0],B[0]); n-=2;m+=2; print_status(A,n,B,m,cost); //BACK if (n>0){ cost+=B[0];//寫錯的地方 swap(A[0],B[0]); B++; n++;m--; print_status(A,n,B,m,cost); } } cout << "cost:" << cost << endl; } int main() { //sol1(); sol2(); system("pause"); return 0; } // SOURCE: http://hoyusun.blogspot.tw/2012/02/blog-post.html void sol1(){ int n; cin >> n; int *A= new int[n]; for(int i=0;i<n;i++){ cin >> A[i]; } int cost = 0; sort(A, A+n); for(n--; n >= 3; n -= 2){ int t1 = A[1] + A[0] + A[n] + A[1]; //AB->A->CD->B, 故B+A+D+B int t2 = A[n] + A[0] + A[n-1] + A[0]; //AD->A->AC->A, 故D+A+C+A cost += t1<t2?t1:t2; //取最小時間 } if(n == 2) cost += (A[2]+A[0]+A[1]); //若三個人時, AC->A->AB故C+A+B else if(n == 1) cost += A[1]; //若兩個人時, AB故B else cost += A[0]; //只有一個人, A故A cout << cost << endl; }
2013年8月17日 星期六
Diamond
#include <iostream> #include <string> using namespace std; void method1(){ int n; cin >> n; //being: init char string a char *a = new char[2*n]; for (int i=0;i<2*n-1;i++){ a[i]=' '; } a[2*n-1]='\0'; //end: init char string a for (int i=0;i<=n-1;i++){ a[n-i-1]='A'; a[n+i-1]='A'; cout << a << endl; } for (int i=n-1;i>=0;i--){ a[n-i-1]=' '; a[n+i-1]=' '; cout << a << endl; } } void method2(){ int n; cin >> n; string a=string(2*n,' '); //init string a for (int i=0;i<=n-1;i++){ a[n-i-1]='A'; a[n+i-1]='A'; cout << a << endl; } for (int i=n-1;i>=0;i--){ a[n-i-1]=' '; a[n+i-1]=' '; cout << a << endl; } } int main(){ method2(); system("pause"); }
2013年7月31日 星期三
訂閱:
文章 (Atom)